#include <bits/stdc++.h>
using namespace std;
const int N = 505;
int md;
int dp[N][N][4][3];
int new_dp[N][N][4][3];
int add(int x, int y) {
return x + y < md ? x + y : x + y - md;
}
void ckadd(int& x, int y) {
x = add(x, y);
}
int mul(int x, int y) {
return x * 1LL * y % md;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n >> md;
dp[0][0][1][0] = 1;
for (int pos = 1; pos <= 2 * n; pos++) {
for (int bal = 0; bal <= n; bal++) {
for (int bef = 0; bef <= n; bef++) {
for (int rd = 0; rd < 4; rd++) {
for (int b = 0; b < 3; b++) {
if (dp[bal][bef][rd][b] == 0) {
continue;
}
int open = bal;
int close = open - bal;
{
// take open bracket
int new_bal = bal + 1;
int new_bef = bef;
int new_rd = rd;
int new_b = (b == 0 ? 2 : b);
if (b == 0) {
new_rd--;
}
ckadd(new_dp[new_bal][new_bef][new_rd][new_b], dp[bal][bef][rd][b]);
}
if (bal > 0) {
// take close bracket
if (b != 0) {
// match last bad greedy bracket
if (b == 1) {
int new_bal = bal - 1;
int new_bef = bef - 1;
int new_rd = rd;
int new_b = 0;
ckadd(new_dp[new_bal][new_bef][new_rd][new_b], dp[bal][bef][rd][b]);
} else {
int new_bal = bal - 1;
int new_bef = open - 1;
int new_rd = min(3, rd + 1);
int new_b = 0;
ckadd(new_dp[new_bal][new_bef][new_rd][new_b], dp[bal][bef][rd][b]);
}
}
// match with bracket that is not last bad greedy bracket
{
// match with bracket before last opt greedy
int new_bal = bal - 1;
int new_bef = bef - 1;
int new_rd = rd;
int new_b = b;
int ft = bef - (b == 1);
ckadd(new_dp[new_bal][new_bef][new_rd][new_b], mul(dp[bal][bef][rd][b], ft));
}
{
// match with bracket after last opt greedy
int new_bal = bal - 1;
int new_bef = open - 1;
int new_rd = min(3, rd + 1);
int new_b = (b != 0 ? 1 : 0);
int ft = (open - bef) - (b == 2);
ckadd(new_dp[new_bal][new_bef][new_rd][new_b], mul(dp[bal][bef][rd][b], ft));
}
}
}
}
}
}
for (int bal = 0; bal <= n; bal++) {
for (int bef = 0; bef <= n; bef++) {
for (int rd = 0; rd < 4; rd++) {
for (int b = 0; b < 3; b++) {
dp[bal][bef][rd][b] = new_dp[bal][bef][rd][b];
new_dp[bal][bef][rd][b] = 0;
}
}
}
}
}
int res = 0;
for (int bef = 0; bef <= n; bef++) {
for (int rd = 2; rd < 4; rd++) {
ckadd(res, dp[0][bef][rd][0]);
}
}
cout << res << '\n';
return 0;
}
Compilation message
festival2.cpp: In function 'int main()':
festival2.cpp:38:17: warning: unused variable 'close' [-Wunused-variable]
38 | int close = open - bal;
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2508 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2508 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2508 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
608 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
800 KB |
Output is correct |
17 |
Correct |
1 ms |
456 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2508 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
608 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
800 KB |
Output is correct |
17 |
Correct |
1 ms |
456 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1657 ms |
11384 KB |
Output is correct |
20 |
Correct |
1550 ms |
12636 KB |
Output is correct |
21 |
Correct |
1601 ms |
12860 KB |
Output is correct |
22 |
Correct |
9 ms |
3000 KB |
Output is correct |
23 |
Correct |
61 ms |
2212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2508 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
608 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
800 KB |
Output is correct |
17 |
Correct |
1 ms |
456 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1657 ms |
11384 KB |
Output is correct |
20 |
Correct |
1550 ms |
12636 KB |
Output is correct |
21 |
Correct |
1601 ms |
12860 KB |
Output is correct |
22 |
Correct |
9 ms |
3000 KB |
Output is correct |
23 |
Correct |
61 ms |
2212 KB |
Output is correct |
24 |
Runtime error |
25 ms |
596 KB |
Execution killed with signal 11 |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2508 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
608 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
800 KB |
Output is correct |
17 |
Correct |
1 ms |
456 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1657 ms |
11384 KB |
Output is correct |
20 |
Correct |
1550 ms |
12636 KB |
Output is correct |
21 |
Correct |
1601 ms |
12860 KB |
Output is correct |
22 |
Correct |
9 ms |
3000 KB |
Output is correct |
23 |
Correct |
61 ms |
2212 KB |
Output is correct |
24 |
Runtime error |
25 ms |
596 KB |
Execution killed with signal 11 |
25 |
Halted |
0 ms |
0 KB |
- |