#include <algorithm>
#include <cstring>
#include <cstdio>
using ll = long long;
constexpr int MOD = 1e9 + 7;
inline void upd(int &x, int y) { (x += y) >= MOD ? (x -= MOD) : 0; }
int arr[305], all[305][305], dp_l[155][305][305][2], dp_r[305][305][605][3];
inline int solve(int n, int off)
{
memset(all, 0, sizeof(all));
all[n][0] = 1;
for (int i = n - 1; i >= 0; i--)
{
for (int j = 0; j <= n; j++)
{
if (!all[i + 1][j])
continue;
if (arr[i] != 1 && j != n)
upd(all[i][j + 1], all[i + 1][j]);
if (arr[i] != -1 && j)
upd(all[i][j - 1], all[i + 1][j]);
}
}
memset(dp_l, 0, sizeof(dp_l));
for (int x = 0; x < n / 2; x++)
{
auto dp = dp_l[x];
dp[0][0][!x] = 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= x; j++)
{
for (int flg = 0; flg < 2; flg++)
{
if (!dp[i][j][flg])
continue;
if (arr[i] != 1 && j)
upd(dp[i + 1][j - 1][flg], dp[i][j][flg]);
if (arr[i] != -1 && j != x)
upd(dp[i + 1][j + 1][flg || j + 1 == x], dp[i][j][flg]);
}
}
}
}
memset(dp_r, 0, sizeof(dp_r));
for (int x = 0; x < n; x++)
{
auto dp = dp_r[x];
dp[n][0][!x] = 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)
upd(dp[i][j + 1][0], dp[i + 1][j][0]);
if (arr[i] != -1 && j)
upd(dp[i][j - 1][0], dp[i + 1][j][0]);
}
if (arr[i] != 1 && x)
upd(dp[i][x][1], dp[i + 1][x - 1][0]);
if (arr[i] != -1 && x < n)
upd(dp[i][x][1], dp[i + 1][x + 1][0]);
for (int j = x; j <= std::min(x * 2, n - 1); j++)
{
if (!dp[i + 1][j][1])
continue;
if (arr[i] != 1)
upd(dp[i][j + 1][1], dp[i + 1][j][1]);
if (arr[i] != -1 && j >= x + 2)
upd(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 ? all[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;
}
}
}
memset(dp_r, 0, sizeof(dp_r));
for (int x = 0; x < n / 2; x++)
{
auto dp = dp_r[x];
dp[n][n][!(x + off)] = 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)
upd(dp[i][j + 1 + n][j + 1 == x + off], dp[i + 1][j + n][0]);
if (arr[i] != -1)
upd(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)
upd(dp[i][j + 1 + n][1], dp[i + 1][j + n][1]);
if (arr[i] != -1)
upd(dp[i][j - 1 + n][1 + !j], dp[i + 1][j + n][1]);
}
for (int j = -n; j <= std::min(x * 2, n - 1); j++)
{
if (!dp[i + 1][j + n][2])
continue;
if (arr[i] != 1 && j != x * 2)
upd(dp[i][j + 1 + n][2], dp[i + 1][j + n][2]);
if (arr[i] != -1 && j != -n)
upd(dp[i][j - 1 + n][2], dp[i + 1][j + n][2]);
}
}
}
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()
{
int n;
scanf("%d%s", &n, str);
for (int i = 0; i < n; i++)
arr[i] = str[i] == '(' ? 1 : str[i] == ')' ? -1 : 0;
int ans = solve(n, 0);
upd(ans, all[0][0]);
for (int i = 0; i < n; i++)
arr[i] *= -1;
std::reverse(arr, arr + n);
upd(ans, solve(n, 1));
printf("%d\n", ans);
return 0;
}
Compilation message
securitygate.cpp: In function 'int main()':
securitygate.cpp:146:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%s", &n, str);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1448 ms |
774684 KB |
Output is correct |
2 |
Correct |
1316 ms |
774408 KB |
Output is correct |
3 |
Correct |
1318 ms |
774532 KB |
Output is correct |
4 |
Correct |
1150 ms |
774416 KB |
Output is correct |
5 |
Correct |
1102 ms |
774548 KB |
Output is correct |
6 |
Correct |
1160 ms |
774480 KB |
Output is correct |
7 |
Correct |
1177 ms |
774492 KB |
Output is correct |
8 |
Correct |
1135 ms |
774492 KB |
Output is correct |
9 |
Correct |
1186 ms |
774520 KB |
Output is correct |
10 |
Correct |
1191 ms |
774368 KB |
Output is correct |
11 |
Correct |
1166 ms |
774452 KB |
Output is correct |
12 |
Correct |
1243 ms |
774520 KB |
Output is correct |
13 |
Correct |
1178 ms |
774520 KB |
Output is correct |
14 |
Correct |
1129 ms |
774476 KB |
Output is correct |
15 |
Correct |
1172 ms |
774528 KB |
Output is correct |
16 |
Correct |
1175 ms |
774492 KB |
Output is correct |
17 |
Correct |
1247 ms |
774440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1448 ms |
774684 KB |
Output is correct |
2 |
Correct |
1316 ms |
774408 KB |
Output is correct |
3 |
Correct |
1318 ms |
774532 KB |
Output is correct |
4 |
Correct |
1150 ms |
774416 KB |
Output is correct |
5 |
Correct |
1102 ms |
774548 KB |
Output is correct |
6 |
Correct |
1160 ms |
774480 KB |
Output is correct |
7 |
Correct |
1177 ms |
774492 KB |
Output is correct |
8 |
Correct |
1135 ms |
774492 KB |
Output is correct |
9 |
Correct |
1186 ms |
774520 KB |
Output is correct |
10 |
Correct |
1191 ms |
774368 KB |
Output is correct |
11 |
Correct |
1166 ms |
774452 KB |
Output is correct |
12 |
Correct |
1243 ms |
774520 KB |
Output is correct |
13 |
Correct |
1178 ms |
774520 KB |
Output is correct |
14 |
Correct |
1129 ms |
774476 KB |
Output is correct |
15 |
Correct |
1172 ms |
774528 KB |
Output is correct |
16 |
Correct |
1175 ms |
774492 KB |
Output is correct |
17 |
Correct |
1247 ms |
774440 KB |
Output is correct |
18 |
Correct |
1279 ms |
774536 KB |
Output is correct |
19 |
Correct |
1262 ms |
774504 KB |
Output is correct |
20 |
Correct |
1212 ms |
774392 KB |
Output is correct |
21 |
Correct |
1157 ms |
774612 KB |
Output is correct |
22 |
Correct |
1155 ms |
774492 KB |
Output is correct |
23 |
Correct |
1267 ms |
774492 KB |
Output is correct |
24 |
Correct |
1204 ms |
774416 KB |
Output is correct |
25 |
Correct |
1154 ms |
774476 KB |
Output is correct |
26 |
Correct |
1177 ms |
774544 KB |
Output is correct |
27 |
Correct |
1185 ms |
774424 KB |
Output is correct |
28 |
Correct |
1214 ms |
774544 KB |
Output is correct |
29 |
Correct |
1202 ms |
774468 KB |
Output is correct |
30 |
Correct |
1159 ms |
774536 KB |
Output is correct |
31 |
Correct |
1232 ms |
774528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1448 ms |
774684 KB |
Output is correct |
2 |
Correct |
1316 ms |
774408 KB |
Output is correct |
3 |
Correct |
1318 ms |
774532 KB |
Output is correct |
4 |
Correct |
1150 ms |
774416 KB |
Output is correct |
5 |
Correct |
1102 ms |
774548 KB |
Output is correct |
6 |
Correct |
1160 ms |
774480 KB |
Output is correct |
7 |
Correct |
1177 ms |
774492 KB |
Output is correct |
8 |
Correct |
1135 ms |
774492 KB |
Output is correct |
9 |
Correct |
1186 ms |
774520 KB |
Output is correct |
10 |
Correct |
1191 ms |
774368 KB |
Output is correct |
11 |
Correct |
1166 ms |
774452 KB |
Output is correct |
12 |
Correct |
1243 ms |
774520 KB |
Output is correct |
13 |
Correct |
1178 ms |
774520 KB |
Output is correct |
14 |
Correct |
1129 ms |
774476 KB |
Output is correct |
15 |
Correct |
1172 ms |
774528 KB |
Output is correct |
16 |
Correct |
1175 ms |
774492 KB |
Output is correct |
17 |
Correct |
1247 ms |
774440 KB |
Output is correct |
18 |
Correct |
1279 ms |
774536 KB |
Output is correct |
19 |
Correct |
1262 ms |
774504 KB |
Output is correct |
20 |
Correct |
1212 ms |
774392 KB |
Output is correct |
21 |
Correct |
1157 ms |
774612 KB |
Output is correct |
22 |
Correct |
1155 ms |
774492 KB |
Output is correct |
23 |
Correct |
1267 ms |
774492 KB |
Output is correct |
24 |
Correct |
1204 ms |
774416 KB |
Output is correct |
25 |
Correct |
1154 ms |
774476 KB |
Output is correct |
26 |
Correct |
1177 ms |
774544 KB |
Output is correct |
27 |
Correct |
1185 ms |
774424 KB |
Output is correct |
28 |
Correct |
1214 ms |
774544 KB |
Output is correct |
29 |
Correct |
1202 ms |
774468 KB |
Output is correct |
30 |
Correct |
1159 ms |
774536 KB |
Output is correct |
31 |
Correct |
1232 ms |
774528 KB |
Output is correct |
32 |
Correct |
1164 ms |
774472 KB |
Output is correct |
33 |
Correct |
1262 ms |
774528 KB |
Output is correct |
34 |
Correct |
1140 ms |
774392 KB |
Output is correct |
35 |
Correct |
1210 ms |
774420 KB |
Output is correct |
36 |
Correct |
1188 ms |
774508 KB |
Output is correct |
37 |
Correct |
1235 ms |
774468 KB |
Output is correct |
38 |
Correct |
1241 ms |
774472 KB |
Output is correct |
39 |
Correct |
1204 ms |
774512 KB |
Output is correct |
40 |
Correct |
1247 ms |
774556 KB |
Output is correct |
41 |
Correct |
1147 ms |
774496 KB |
Output is correct |
42 |
Correct |
1209 ms |
774344 KB |
Output is correct |
43 |
Correct |
1243 ms |
774392 KB |
Output is correct |
44 |
Correct |
1287 ms |
774520 KB |
Output is correct |
45 |
Correct |
1287 ms |
774520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1448 ms |
774684 KB |
Output is correct |
2 |
Correct |
1316 ms |
774408 KB |
Output is correct |
3 |
Correct |
1318 ms |
774532 KB |
Output is correct |
4 |
Correct |
1150 ms |
774416 KB |
Output is correct |
5 |
Correct |
1102 ms |
774548 KB |
Output is correct |
6 |
Correct |
1160 ms |
774480 KB |
Output is correct |
7 |
Correct |
1177 ms |
774492 KB |
Output is correct |
8 |
Correct |
1135 ms |
774492 KB |
Output is correct |
9 |
Correct |
1186 ms |
774520 KB |
Output is correct |
10 |
Correct |
1191 ms |
774368 KB |
Output is correct |
11 |
Correct |
1166 ms |
774452 KB |
Output is correct |
12 |
Correct |
1243 ms |
774520 KB |
Output is correct |
13 |
Correct |
1178 ms |
774520 KB |
Output is correct |
14 |
Correct |
1129 ms |
774476 KB |
Output is correct |
15 |
Correct |
1172 ms |
774528 KB |
Output is correct |
16 |
Correct |
1175 ms |
774492 KB |
Output is correct |
17 |
Correct |
1247 ms |
774440 KB |
Output is correct |
18 |
Correct |
1279 ms |
774536 KB |
Output is correct |
19 |
Correct |
1262 ms |
774504 KB |
Output is correct |
20 |
Correct |
1212 ms |
774392 KB |
Output is correct |
21 |
Correct |
1157 ms |
774612 KB |
Output is correct |
22 |
Correct |
1155 ms |
774492 KB |
Output is correct |
23 |
Correct |
1267 ms |
774492 KB |
Output is correct |
24 |
Correct |
1204 ms |
774416 KB |
Output is correct |
25 |
Correct |
1154 ms |
774476 KB |
Output is correct |
26 |
Correct |
1177 ms |
774544 KB |
Output is correct |
27 |
Correct |
1185 ms |
774424 KB |
Output is correct |
28 |
Correct |
1214 ms |
774544 KB |
Output is correct |
29 |
Correct |
1202 ms |
774468 KB |
Output is correct |
30 |
Correct |
1159 ms |
774536 KB |
Output is correct |
31 |
Correct |
1232 ms |
774528 KB |
Output is correct |
32 |
Correct |
1164 ms |
774472 KB |
Output is correct |
33 |
Correct |
1262 ms |
774528 KB |
Output is correct |
34 |
Correct |
1140 ms |
774392 KB |
Output is correct |
35 |
Correct |
1210 ms |
774420 KB |
Output is correct |
36 |
Correct |
1188 ms |
774508 KB |
Output is correct |
37 |
Correct |
1235 ms |
774468 KB |
Output is correct |
38 |
Correct |
1241 ms |
774472 KB |
Output is correct |
39 |
Correct |
1204 ms |
774512 KB |
Output is correct |
40 |
Correct |
1247 ms |
774556 KB |
Output is correct |
41 |
Correct |
1147 ms |
774496 KB |
Output is correct |
42 |
Correct |
1209 ms |
774344 KB |
Output is correct |
43 |
Correct |
1243 ms |
774392 KB |
Output is correct |
44 |
Correct |
1287 ms |
774520 KB |
Output is correct |
45 |
Correct |
1287 ms |
774520 KB |
Output is correct |
46 |
Correct |
1156 ms |
774520 KB |
Output is correct |
47 |
Correct |
1242 ms |
774412 KB |
Output is correct |
48 |
Correct |
1158 ms |
774392 KB |
Output is correct |
49 |
Correct |
1208 ms |
774332 KB |
Output is correct |
50 |
Correct |
1221 ms |
774444 KB |
Output is correct |
51 |
Correct |
1175 ms |
774400 KB |
Output is correct |
52 |
Correct |
1243 ms |
774532 KB |
Output is correct |
53 |
Correct |
1182 ms |
774464 KB |
Output is correct |
54 |
Correct |
1201 ms |
774520 KB |
Output is correct |
55 |
Correct |
1143 ms |
774392 KB |
Output is correct |
56 |
Correct |
1235 ms |
774492 KB |
Output is correct |
57 |
Correct |
1156 ms |
774464 KB |
Output is correct |
58 |
Correct |
1199 ms |
774416 KB |
Output is correct |
59 |
Correct |
1241 ms |
774392 KB |
Output is correct |
60 |
Correct |
1229 ms |
774520 KB |
Output is correct |
61 |
Correct |
1187 ms |
774460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1448 ms |
774684 KB |
Output is correct |
2 |
Correct |
1316 ms |
774408 KB |
Output is correct |
3 |
Correct |
1318 ms |
774532 KB |
Output is correct |
4 |
Correct |
1150 ms |
774416 KB |
Output is correct |
5 |
Correct |
1102 ms |
774548 KB |
Output is correct |
6 |
Correct |
1160 ms |
774480 KB |
Output is correct |
7 |
Correct |
1177 ms |
774492 KB |
Output is correct |
8 |
Correct |
1135 ms |
774492 KB |
Output is correct |
9 |
Correct |
1186 ms |
774520 KB |
Output is correct |
10 |
Correct |
1191 ms |
774368 KB |
Output is correct |
11 |
Correct |
1166 ms |
774452 KB |
Output is correct |
12 |
Correct |
1243 ms |
774520 KB |
Output is correct |
13 |
Correct |
1178 ms |
774520 KB |
Output is correct |
14 |
Correct |
1129 ms |
774476 KB |
Output is correct |
15 |
Correct |
1172 ms |
774528 KB |
Output is correct |
16 |
Correct |
1175 ms |
774492 KB |
Output is correct |
17 |
Correct |
1247 ms |
774440 KB |
Output is correct |
18 |
Correct |
1279 ms |
774536 KB |
Output is correct |
19 |
Correct |
1262 ms |
774504 KB |
Output is correct |
20 |
Correct |
1212 ms |
774392 KB |
Output is correct |
21 |
Correct |
1157 ms |
774612 KB |
Output is correct |
22 |
Correct |
1155 ms |
774492 KB |
Output is correct |
23 |
Correct |
1267 ms |
774492 KB |
Output is correct |
24 |
Correct |
1204 ms |
774416 KB |
Output is correct |
25 |
Correct |
1154 ms |
774476 KB |
Output is correct |
26 |
Correct |
1177 ms |
774544 KB |
Output is correct |
27 |
Correct |
1185 ms |
774424 KB |
Output is correct |
28 |
Correct |
1214 ms |
774544 KB |
Output is correct |
29 |
Correct |
1202 ms |
774468 KB |
Output is correct |
30 |
Correct |
1159 ms |
774536 KB |
Output is correct |
31 |
Correct |
1232 ms |
774528 KB |
Output is correct |
32 |
Correct |
1164 ms |
774472 KB |
Output is correct |
33 |
Correct |
1262 ms |
774528 KB |
Output is correct |
34 |
Correct |
1140 ms |
774392 KB |
Output is correct |
35 |
Correct |
1210 ms |
774420 KB |
Output is correct |
36 |
Correct |
1188 ms |
774508 KB |
Output is correct |
37 |
Correct |
1235 ms |
774468 KB |
Output is correct |
38 |
Correct |
1241 ms |
774472 KB |
Output is correct |
39 |
Correct |
1204 ms |
774512 KB |
Output is correct |
40 |
Correct |
1247 ms |
774556 KB |
Output is correct |
41 |
Correct |
1147 ms |
774496 KB |
Output is correct |
42 |
Correct |
1209 ms |
774344 KB |
Output is correct |
43 |
Correct |
1243 ms |
774392 KB |
Output is correct |
44 |
Correct |
1287 ms |
774520 KB |
Output is correct |
45 |
Correct |
1287 ms |
774520 KB |
Output is correct |
46 |
Correct |
1156 ms |
774520 KB |
Output is correct |
47 |
Correct |
1242 ms |
774412 KB |
Output is correct |
48 |
Correct |
1158 ms |
774392 KB |
Output is correct |
49 |
Correct |
1208 ms |
774332 KB |
Output is correct |
50 |
Correct |
1221 ms |
774444 KB |
Output is correct |
51 |
Correct |
1175 ms |
774400 KB |
Output is correct |
52 |
Correct |
1243 ms |
774532 KB |
Output is correct |
53 |
Correct |
1182 ms |
774464 KB |
Output is correct |
54 |
Correct |
1201 ms |
774520 KB |
Output is correct |
55 |
Correct |
1143 ms |
774392 KB |
Output is correct |
56 |
Correct |
1235 ms |
774492 KB |
Output is correct |
57 |
Correct |
1156 ms |
774464 KB |
Output is correct |
58 |
Correct |
1199 ms |
774416 KB |
Output is correct |
59 |
Correct |
1241 ms |
774392 KB |
Output is correct |
60 |
Correct |
1229 ms |
774520 KB |
Output is correct |
61 |
Correct |
1187 ms |
774460 KB |
Output is correct |
62 |
Correct |
1514 ms |
774520 KB |
Output is correct |
63 |
Correct |
1482 ms |
774516 KB |
Output is correct |
64 |
Correct |
1282 ms |
774500 KB |
Output is correct |
65 |
Correct |
1298 ms |
774444 KB |
Output is correct |
66 |
Correct |
1883 ms |
774512 KB |
Output is correct |
67 |
Correct |
1910 ms |
774520 KB |
Output is correct |
68 |
Correct |
2049 ms |
774504 KB |
Output is correct |
69 |
Correct |
1272 ms |
774520 KB |
Output is correct |
70 |
Correct |
1363 ms |
774520 KB |
Output is correct |
71 |
Correct |
1229 ms |
774444 KB |
Output is correct |
72 |
Correct |
1298 ms |
774436 KB |
Output is correct |
73 |
Correct |
1998 ms |
774468 KB |
Output is correct |
74 |
Correct |
1824 ms |
774496 KB |
Output is correct |