#include <algorithm>
#include <cstring>
#include <cstdio>
using ll = long long;
constexpr int MOD = 1e9 + 7;
inline void add(int &x, const int &y) { (x += y) >= MOD ? (x -= MOD) : 0; }
int n, arr[305], any[305][305], dp_l[155][305][305][2];
namespace solver_1 { int dp_r[305][305][305][2]; bool have; }
namespace solver_2 { int dp_r[155][305][605][3]; bool have; }
inline void init_any()
{
memset(any, 0, sizeof(any));
any[n][0] = 1;
for (int i = n - 1; i >= 0; i--)
{
for (int j = 0; j <= n; j++)
{
if (!any[i + 1][j])
continue;
if (arr[i] != +1 && j < n)
add(any[i][j + 1], any[i + 1][j]);
if (arr[i] != -1 && j > 0)
add(any[i][j - 1], any[i + 1][j]);
}
}
}
inline void init_dp_l()
{
memset(dp_l, 0, sizeof(dp_l));
for (int a = 0; a < n / 2; a++)
{
auto dp = dp_l[a];
dp[0][0][!a] = 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= a; j++)
{
for (int x = 0; x < 2; x++)
{
if (!dp[i][j][x])
continue;
if (arr[i] != -1 && j < a)
add(dp[i + 1][j + 1][x || j + 1 == a], dp[i][j][x]);
if (arr[i] != +1 && j > 0)
add(dp[i + 1][j - 1][x], dp[i][j][x]);
}
}
}
}
}
inline int solve_1()
{
using namespace solver_1;
if (have)
memset(dp_r, 0, sizeof(dp_r));
else
have = true;
for (int a = 0; a < n; a++)
{
auto dp = dp_r[a];
dp[n][0][!a] = 1;
for (int i = n - 1; i >= 0; i--)
{
for (int j = 0; j <= n; j++)
{
if (!dp[i + 1][j][0])
continue;
if (arr[i] != +1 && j < n)
add(dp[i][j + 1][0], dp[i + 1][j][0]);
if (arr[i] != -1 && j > 0)
add(dp[i][j - 1][0], dp[i + 1][j][0]);
}
if (arr[i] != -1 && a < n)
add(dp[i][a][1], dp[i + 1][a + 1][0]);
if (arr[i] != +1 && a > 0)
add(dp[i][a][1], dp[i + 1][a - 1][0]);
for (int j = 0; j <= std::min(a * 2, n - 1); j++)
{
if (!dp[i + 1][j][1])
continue;
if (arr[i] != +1 && j >= a)
add(dp[i][j + 1][1], dp[i + 1][j][1]);
if (arr[i] != -1 && j >= a + 2)
add(dp[i][j - 1][1], dp[i + 1][j][1]);
}
}
}
int res = 0;
for (int a = 0; a <= n / 2; a++)
{
for (int b = 1; a + b <= n / 2; b++)
{
for (int pos = 0; pos < n; pos += 2)
{
if (arr[pos] == 1)
continue;
int val = a >= b ? any[pos + 1][2 * b - 1] :
dp_r[a + b][pos + 1][2 * b - 1][1];
res = (res + (ll)dp_l[a][pos][0][1] * val) % MOD;
}
}
}
return res;
}
inline int solve_2(int t)
{
using namespace solver_2;
if (have)
memset(dp_r, 0, sizeof(dp_r));
else
have = true;
for (int a = 0; a < n / 2; a++)
{
auto dp = dp_r[a];
dp[n][n][!(a + t)] = 1;
for (int i = n - 1; i >= 0; i--)
{
for (int j = 0; j <= n; j++)
{
if (!dp[i + 1][j + n][0])
continue;
if (arr[i] != +1 && j < n)
add(dp[i][j + 1 + n][j + 1 == a + t], dp[i + 1][j + n][0]);
if (arr[i] != -1 && j > 0)
add(dp[i][j - 1 + n][0], dp[i + 1][j + n][0]);
}
for (int j = 0; j <= n; j++)
{
if (!dp[i + 1][j + n][1])
continue;
if (arr[i] != +1)
add(dp[i][j + 1 + n][1], dp[i + 1][j + n][1]);
if (arr[i] != -1)
add(dp[i][j - 1 + n][1 + !j], dp[i + 1][j + n][1]);
}
for (int j = -n; j <= std::min(a * 2, n - 1); j++)
{
if (!dp[i + 1][j + n][2])
continue;
if (arr[i] != +1 && j < a * 2)
add(dp[i][j + 1 + n][2], dp[i + 1][j + n][2]);
if (arr[i] != -1 && j > -n)
add(dp[i][j - 1 + n][2], dp[i + 1][j + n][2]);
}
}
}
int res = 0;
for (int a = 0; a <= n / 2; a++)
{
for (int b = -n / 2 + 1; b < n / 2; b++)
{
if (a + b < 0 || a + (b < 0 ? -b : b) >= n / 2)
continue;
for (int pos = 0; pos < n; pos += 2)
{
if (arr[pos] == 1)
continue;
res = (res + (ll)dp_l[a][pos][0][1] *
dp_r[a + b][pos + 1][b * 2 - 1 + n][2]) % MOD;
}
}
}
return res;
}
char str[305];
int main()
{
// freopen("JOISC2018-D3T3.in", "r", stdin);
scanf("%d%s", &n, str);
for (int i = 0; i < n; i++)
arr[i] = str[i] == '(' ? 1 : str[i] == ')' ? -1 : 0;
init_any();
int ans = any[0][0];
init_dp_l();
add(ans, solve_1());
add(ans, solve_2(0));
std::reverse(arr, arr + n);
for (int i = 0; i < n; i++)
arr[i] = -arr[i];
init_any();
init_dp_l();
add(ans, solve_1());
add(ans, solve_2(1));
printf("%d\n", ans);
return 0;
}
Compilation message
securitygate.cpp: In function 'int main()':
securitygate.cpp:169:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%s", &n, str);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
562 ms |
671480 KB |
Output is correct |
2 |
Correct |
585 ms |
671576 KB |
Output is correct |
3 |
Correct |
610 ms |
671584 KB |
Output is correct |
4 |
Correct |
525 ms |
671532 KB |
Output is correct |
5 |
Correct |
467 ms |
671480 KB |
Output is correct |
6 |
Correct |
474 ms |
671480 KB |
Output is correct |
7 |
Correct |
486 ms |
671512 KB |
Output is correct |
8 |
Correct |
579 ms |
671592 KB |
Output is correct |
9 |
Correct |
627 ms |
671728 KB |
Output is correct |
10 |
Correct |
550 ms |
671480 KB |
Output is correct |
11 |
Correct |
513 ms |
671568 KB |
Output is correct |
12 |
Correct |
577 ms |
671580 KB |
Output is correct |
13 |
Correct |
589 ms |
671564 KB |
Output is correct |
14 |
Correct |
504 ms |
671480 KB |
Output is correct |
15 |
Correct |
566 ms |
671512 KB |
Output is correct |
16 |
Correct |
535 ms |
671480 KB |
Output is correct |
17 |
Correct |
515 ms |
671496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
562 ms |
671480 KB |
Output is correct |
2 |
Correct |
585 ms |
671576 KB |
Output is correct |
3 |
Correct |
610 ms |
671584 KB |
Output is correct |
4 |
Correct |
525 ms |
671532 KB |
Output is correct |
5 |
Correct |
467 ms |
671480 KB |
Output is correct |
6 |
Correct |
474 ms |
671480 KB |
Output is correct |
7 |
Correct |
486 ms |
671512 KB |
Output is correct |
8 |
Correct |
579 ms |
671592 KB |
Output is correct |
9 |
Correct |
627 ms |
671728 KB |
Output is correct |
10 |
Correct |
550 ms |
671480 KB |
Output is correct |
11 |
Correct |
513 ms |
671568 KB |
Output is correct |
12 |
Correct |
577 ms |
671580 KB |
Output is correct |
13 |
Correct |
589 ms |
671564 KB |
Output is correct |
14 |
Correct |
504 ms |
671480 KB |
Output is correct |
15 |
Correct |
566 ms |
671512 KB |
Output is correct |
16 |
Correct |
535 ms |
671480 KB |
Output is correct |
17 |
Correct |
515 ms |
671496 KB |
Output is correct |
18 |
Correct |
499 ms |
671428 KB |
Output is correct |
19 |
Correct |
531 ms |
671608 KB |
Output is correct |
20 |
Correct |
462 ms |
671480 KB |
Output is correct |
21 |
Correct |
511 ms |
671480 KB |
Output is correct |
22 |
Correct |
621 ms |
671580 KB |
Output is correct |
23 |
Correct |
558 ms |
671496 KB |
Output is correct |
24 |
Correct |
581 ms |
671480 KB |
Output is correct |
25 |
Correct |
577 ms |
671432 KB |
Output is correct |
26 |
Correct |
509 ms |
671448 KB |
Output is correct |
27 |
Correct |
521 ms |
671468 KB |
Output is correct |
28 |
Correct |
496 ms |
671524 KB |
Output is correct |
29 |
Correct |
565 ms |
671608 KB |
Output is correct |
30 |
Correct |
564 ms |
671652 KB |
Output is correct |
31 |
Correct |
603 ms |
671480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
562 ms |
671480 KB |
Output is correct |
2 |
Correct |
585 ms |
671576 KB |
Output is correct |
3 |
Correct |
610 ms |
671584 KB |
Output is correct |
4 |
Correct |
525 ms |
671532 KB |
Output is correct |
5 |
Correct |
467 ms |
671480 KB |
Output is correct |
6 |
Correct |
474 ms |
671480 KB |
Output is correct |
7 |
Correct |
486 ms |
671512 KB |
Output is correct |
8 |
Correct |
579 ms |
671592 KB |
Output is correct |
9 |
Correct |
627 ms |
671728 KB |
Output is correct |
10 |
Correct |
550 ms |
671480 KB |
Output is correct |
11 |
Correct |
513 ms |
671568 KB |
Output is correct |
12 |
Correct |
577 ms |
671580 KB |
Output is correct |
13 |
Correct |
589 ms |
671564 KB |
Output is correct |
14 |
Correct |
504 ms |
671480 KB |
Output is correct |
15 |
Correct |
566 ms |
671512 KB |
Output is correct |
16 |
Correct |
535 ms |
671480 KB |
Output is correct |
17 |
Correct |
515 ms |
671496 KB |
Output is correct |
18 |
Correct |
499 ms |
671428 KB |
Output is correct |
19 |
Correct |
531 ms |
671608 KB |
Output is correct |
20 |
Correct |
462 ms |
671480 KB |
Output is correct |
21 |
Correct |
511 ms |
671480 KB |
Output is correct |
22 |
Correct |
621 ms |
671580 KB |
Output is correct |
23 |
Correct |
558 ms |
671496 KB |
Output is correct |
24 |
Correct |
581 ms |
671480 KB |
Output is correct |
25 |
Correct |
577 ms |
671432 KB |
Output is correct |
26 |
Correct |
509 ms |
671448 KB |
Output is correct |
27 |
Correct |
521 ms |
671468 KB |
Output is correct |
28 |
Correct |
496 ms |
671524 KB |
Output is correct |
29 |
Correct |
565 ms |
671608 KB |
Output is correct |
30 |
Correct |
564 ms |
671652 KB |
Output is correct |
31 |
Correct |
603 ms |
671480 KB |
Output is correct |
32 |
Correct |
526 ms |
671488 KB |
Output is correct |
33 |
Correct |
487 ms |
671484 KB |
Output is correct |
34 |
Correct |
561 ms |
671444 KB |
Output is correct |
35 |
Correct |
501 ms |
671608 KB |
Output is correct |
36 |
Correct |
611 ms |
671536 KB |
Output is correct |
37 |
Correct |
655 ms |
671524 KB |
Output is correct |
38 |
Correct |
614 ms |
671608 KB |
Output is correct |
39 |
Correct |
575 ms |
671520 KB |
Output is correct |
40 |
Correct |
642 ms |
671544 KB |
Output is correct |
41 |
Correct |
619 ms |
671548 KB |
Output is correct |
42 |
Correct |
652 ms |
671560 KB |
Output is correct |
43 |
Correct |
672 ms |
671528 KB |
Output is correct |
44 |
Correct |
550 ms |
671708 KB |
Output is correct |
45 |
Correct |
537 ms |
671464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
562 ms |
671480 KB |
Output is correct |
2 |
Correct |
585 ms |
671576 KB |
Output is correct |
3 |
Correct |
610 ms |
671584 KB |
Output is correct |
4 |
Correct |
525 ms |
671532 KB |
Output is correct |
5 |
Correct |
467 ms |
671480 KB |
Output is correct |
6 |
Correct |
474 ms |
671480 KB |
Output is correct |
7 |
Correct |
486 ms |
671512 KB |
Output is correct |
8 |
Correct |
579 ms |
671592 KB |
Output is correct |
9 |
Correct |
627 ms |
671728 KB |
Output is correct |
10 |
Correct |
550 ms |
671480 KB |
Output is correct |
11 |
Correct |
513 ms |
671568 KB |
Output is correct |
12 |
Correct |
577 ms |
671580 KB |
Output is correct |
13 |
Correct |
589 ms |
671564 KB |
Output is correct |
14 |
Correct |
504 ms |
671480 KB |
Output is correct |
15 |
Correct |
566 ms |
671512 KB |
Output is correct |
16 |
Correct |
535 ms |
671480 KB |
Output is correct |
17 |
Correct |
515 ms |
671496 KB |
Output is correct |
18 |
Correct |
499 ms |
671428 KB |
Output is correct |
19 |
Correct |
531 ms |
671608 KB |
Output is correct |
20 |
Correct |
462 ms |
671480 KB |
Output is correct |
21 |
Correct |
511 ms |
671480 KB |
Output is correct |
22 |
Correct |
621 ms |
671580 KB |
Output is correct |
23 |
Correct |
558 ms |
671496 KB |
Output is correct |
24 |
Correct |
581 ms |
671480 KB |
Output is correct |
25 |
Correct |
577 ms |
671432 KB |
Output is correct |
26 |
Correct |
509 ms |
671448 KB |
Output is correct |
27 |
Correct |
521 ms |
671468 KB |
Output is correct |
28 |
Correct |
496 ms |
671524 KB |
Output is correct |
29 |
Correct |
565 ms |
671608 KB |
Output is correct |
30 |
Correct |
564 ms |
671652 KB |
Output is correct |
31 |
Correct |
603 ms |
671480 KB |
Output is correct |
32 |
Correct |
526 ms |
671488 KB |
Output is correct |
33 |
Correct |
487 ms |
671484 KB |
Output is correct |
34 |
Correct |
561 ms |
671444 KB |
Output is correct |
35 |
Correct |
501 ms |
671608 KB |
Output is correct |
36 |
Correct |
611 ms |
671536 KB |
Output is correct |
37 |
Correct |
655 ms |
671524 KB |
Output is correct |
38 |
Correct |
614 ms |
671608 KB |
Output is correct |
39 |
Correct |
575 ms |
671520 KB |
Output is correct |
40 |
Correct |
642 ms |
671544 KB |
Output is correct |
41 |
Correct |
619 ms |
671548 KB |
Output is correct |
42 |
Correct |
652 ms |
671560 KB |
Output is correct |
43 |
Correct |
672 ms |
671528 KB |
Output is correct |
44 |
Correct |
550 ms |
671708 KB |
Output is correct |
45 |
Correct |
537 ms |
671464 KB |
Output is correct |
46 |
Correct |
486 ms |
671500 KB |
Output is correct |
47 |
Correct |
612 ms |
671504 KB |
Output is correct |
48 |
Correct |
524 ms |
671648 KB |
Output is correct |
49 |
Correct |
556 ms |
671480 KB |
Output is correct |
50 |
Correct |
561 ms |
671532 KB |
Output is correct |
51 |
Correct |
505 ms |
671492 KB |
Output is correct |
52 |
Correct |
571 ms |
671512 KB |
Output is correct |
53 |
Correct |
588 ms |
671388 KB |
Output is correct |
54 |
Correct |
626 ms |
671480 KB |
Output is correct |
55 |
Correct |
594 ms |
671524 KB |
Output is correct |
56 |
Correct |
481 ms |
671608 KB |
Output is correct |
57 |
Correct |
487 ms |
671480 KB |
Output is correct |
58 |
Correct |
509 ms |
671608 KB |
Output is correct |
59 |
Correct |
514 ms |
671572 KB |
Output is correct |
60 |
Correct |
536 ms |
671588 KB |
Output is correct |
61 |
Correct |
622 ms |
671608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
562 ms |
671480 KB |
Output is correct |
2 |
Correct |
585 ms |
671576 KB |
Output is correct |
3 |
Correct |
610 ms |
671584 KB |
Output is correct |
4 |
Correct |
525 ms |
671532 KB |
Output is correct |
5 |
Correct |
467 ms |
671480 KB |
Output is correct |
6 |
Correct |
474 ms |
671480 KB |
Output is correct |
7 |
Correct |
486 ms |
671512 KB |
Output is correct |
8 |
Correct |
579 ms |
671592 KB |
Output is correct |
9 |
Correct |
627 ms |
671728 KB |
Output is correct |
10 |
Correct |
550 ms |
671480 KB |
Output is correct |
11 |
Correct |
513 ms |
671568 KB |
Output is correct |
12 |
Correct |
577 ms |
671580 KB |
Output is correct |
13 |
Correct |
589 ms |
671564 KB |
Output is correct |
14 |
Correct |
504 ms |
671480 KB |
Output is correct |
15 |
Correct |
566 ms |
671512 KB |
Output is correct |
16 |
Correct |
535 ms |
671480 KB |
Output is correct |
17 |
Correct |
515 ms |
671496 KB |
Output is correct |
18 |
Correct |
499 ms |
671428 KB |
Output is correct |
19 |
Correct |
531 ms |
671608 KB |
Output is correct |
20 |
Correct |
462 ms |
671480 KB |
Output is correct |
21 |
Correct |
511 ms |
671480 KB |
Output is correct |
22 |
Correct |
621 ms |
671580 KB |
Output is correct |
23 |
Correct |
558 ms |
671496 KB |
Output is correct |
24 |
Correct |
581 ms |
671480 KB |
Output is correct |
25 |
Correct |
577 ms |
671432 KB |
Output is correct |
26 |
Correct |
509 ms |
671448 KB |
Output is correct |
27 |
Correct |
521 ms |
671468 KB |
Output is correct |
28 |
Correct |
496 ms |
671524 KB |
Output is correct |
29 |
Correct |
565 ms |
671608 KB |
Output is correct |
30 |
Correct |
564 ms |
671652 KB |
Output is correct |
31 |
Correct |
603 ms |
671480 KB |
Output is correct |
32 |
Correct |
526 ms |
671488 KB |
Output is correct |
33 |
Correct |
487 ms |
671484 KB |
Output is correct |
34 |
Correct |
561 ms |
671444 KB |
Output is correct |
35 |
Correct |
501 ms |
671608 KB |
Output is correct |
36 |
Correct |
611 ms |
671536 KB |
Output is correct |
37 |
Correct |
655 ms |
671524 KB |
Output is correct |
38 |
Correct |
614 ms |
671608 KB |
Output is correct |
39 |
Correct |
575 ms |
671520 KB |
Output is correct |
40 |
Correct |
642 ms |
671544 KB |
Output is correct |
41 |
Correct |
619 ms |
671548 KB |
Output is correct |
42 |
Correct |
652 ms |
671560 KB |
Output is correct |
43 |
Correct |
672 ms |
671528 KB |
Output is correct |
44 |
Correct |
550 ms |
671708 KB |
Output is correct |
45 |
Correct |
537 ms |
671464 KB |
Output is correct |
46 |
Correct |
486 ms |
671500 KB |
Output is correct |
47 |
Correct |
612 ms |
671504 KB |
Output is correct |
48 |
Correct |
524 ms |
671648 KB |
Output is correct |
49 |
Correct |
556 ms |
671480 KB |
Output is correct |
50 |
Correct |
561 ms |
671532 KB |
Output is correct |
51 |
Correct |
505 ms |
671492 KB |
Output is correct |
52 |
Correct |
571 ms |
671512 KB |
Output is correct |
53 |
Correct |
588 ms |
671388 KB |
Output is correct |
54 |
Correct |
626 ms |
671480 KB |
Output is correct |
55 |
Correct |
594 ms |
671524 KB |
Output is correct |
56 |
Correct |
481 ms |
671608 KB |
Output is correct |
57 |
Correct |
487 ms |
671480 KB |
Output is correct |
58 |
Correct |
509 ms |
671608 KB |
Output is correct |
59 |
Correct |
514 ms |
671572 KB |
Output is correct |
60 |
Correct |
536 ms |
671588 KB |
Output is correct |
61 |
Correct |
622 ms |
671608 KB |
Output is correct |
62 |
Correct |
1065 ms |
671596 KB |
Output is correct |
63 |
Correct |
974 ms |
671480 KB |
Output is correct |
64 |
Correct |
722 ms |
671520 KB |
Output is correct |
65 |
Correct |
669 ms |
671480 KB |
Output is correct |
66 |
Correct |
1553 ms |
671588 KB |
Output is correct |
67 |
Correct |
1474 ms |
671572 KB |
Output is correct |
68 |
Correct |
1723 ms |
671608 KB |
Output is correct |
69 |
Correct |
799 ms |
671580 KB |
Output is correct |
70 |
Correct |
854 ms |
671480 KB |
Output is correct |
71 |
Correct |
690 ms |
671632 KB |
Output is correct |
72 |
Correct |
731 ms |
671596 KB |
Output is correct |
73 |
Correct |
1549 ms |
671692 KB |
Output is correct |
74 |
Correct |
1403 ms |
671784 KB |
Output is correct |