Submission #680784

# Submission time Handle Problem Language Result Execution time Memory
680784 2023-01-11T19:12:18 Z rainboy Security Gate (JOI18_security_gate) C
100 / 100
341 ms 3220 KB
#include <stdio.h>
#include <string.h>

#define N	300
#define MD	1000000007

void flip(char *cc, int n) {
	int i, j;
	char tmp;

	for (i = 0; i < n; i++)
		cc[i] = cc[i] == 'x' ? 'x' : (cc[i] == '(' ? ')' : '(');
	for (i = 0, j = n - 1; i < j; i++, j--)
		tmp = cc[i], cc[i] = cc[j], cc[j] = tmp;
}

int solve1(char *cc, int n) {
	static int dp[N + 1], dq[N + 1];
	int i, d;

	memset(dp, 0, (n + 1) * sizeof *dp), dp[0] = 1;
	for (i = 0; i < n; i++) {
		memset(dq, 0, (n + 1) * sizeof *dq);
		for (d = 0; d <= n; d++) {
			int x = dp[d];

			if (x == 0)
				continue;
			if (cc[i] == '(' || cc[i] == 'x')
				dq[d + 1] = (dq[d + 1] + x) % MD;
			if ((cc[i] == ')' || cc[i] == 'x') && d > 0)
				dq[d - 1] = (dq[d - 1] + x) % MD;
		}
		memcpy(dp, dq, (n + 1) * sizeof *dq);
	}
	return dp[0];
}

int solve2(char *cc, int n) {
	static int dp[N + 1][N + 1], dq[N + 1][N + 1], dq_[N + 1][N + 1], dr[N + 1][N + 1], dr_[N + 1][N + 1];
	int i, d, a, a_, b, b_, ans;

	for (i = 0; i <= n; i++)
		memset(dp[i], 0, (n + 1) * sizeof *dp[i]);
	dp[0][0] = 1;
	for (i = 0; i < n; i++)
		for (d = 0; d <= n; d++) {
			int x = dp[i][d];

			if (x == 0)
				continue;
			if (cc[i] == '(' || cc[i] == 'x')
				dp[i + 1][d + 1] = (dp[i + 1][d + 1] + x) % MD;
			if ((cc[i] == ')' || cc[i] == 'x') && d > 0)
				dp[i + 1][d - 1] = (dp[i + 1][d - 1] + x) % MD;
		}
	for (a = 0; a <= n; a++)
		for (b = 0; b <= a; b++)
			dq[a][b] = dr[a][b] = 0;
	dq[0][0] = 1;
	ans = 0;
	for (i = n - 1; i >= 0; i--) {
		for (a = 0; a <= n; a++)
			for (b = 0; b <= a; b++)
				dq_[a][b] = dr_[a][b] = 0;
		for (a = 0; a <= n; a++)
			for (b = 0; b <= a; b++) {
				int x = dq[a][b];

				if (x == 0)
					continue;
				if (cc[i] == '(' || cc[i] == 'x') {
					if (b > 0)
						dq_[a][b - 1] = (dq_[a][b - 1] + x) % MD;
					else {
						for (d = 2; d <= n; d += 2)
							if (a + 1 >= d / 2)
								ans = (ans + (long long) dp[i][d - 1] * x) % MD;
							else {
								a_ = a + d / 2, b_ = d / 2 - (a + 1);
								dr_[a_][b_] = (dr_[a_][b_] + x) % MD;
							}
					}
				}
				if (cc[i] == ')' || cc[i] == 'x') {
					if (b < a)
						dq_[a][b + 1] = (dq_[a][b + 1] + x) % MD;
					else
						dq_[a + 1][b + 1] = (dq_[a + 1][b + 1] + x) % MD;
				}
			}
		for (a = 0; a <= n; a++)
			for (b = 0; b <= a; b++) {
				int x = dr[a][b];

				if (x == 0)
					continue;
				if (cc[i] == '(' || cc[i] == 'x') {
					if (b > 1)
						dr_[a][b - 1] = (dr_[a][b - 1] + x) % MD;
					else
						ans = (ans + (long long) dp[i][a] * x) % MD;
				}
				if (cc[i] == ')' || cc[i] == 'x') {
					if (b < a)
						dr_[a][b + 1] = (dr_[a][b + 1] + x) % MD;
				}
			}
		for (a = 0; a <= n; a++)
			for (b = 0; b <= a; b++)
				dq[a][b] = dq_[a][b], dr[a][b] = dr_[a][b];
	}
	return ans;
}

int solve3(char *cc, int n) {
	static int dp[N + 1][N + 1], dp_[N + 1][N + 1], dq[N + 1][N + 1], dr[N + 1], dr_[N + 1], xx[2][N + 1];
	int r, i, j, a, b, d, ans;

	for (r = 0; r < 2; r++) {
		for (a = 0; a <= n; a++)
			for (b = 0; b <= a; b++)
				dp[a][b] = 0;
		dp[0][0] = 1;
		for (i = 0; i < n; i++) {
			for (a = n; a >= 0; a--)
				dq[i][a] = ((a == n ? 0 : dq[i][a + 1]) + dp[a][0]) % MD;
			for (a = 0; a <= n; a++)
				for (b = 0; b <= a; b++)
					dp_[a][b] = 0;
			for (a = 0; a <= n; a++)
				for (b = 0; b <= a; b++) {
					int x = dp[a][b];

					if (x == 0)
						continue;
					if (cc[i] == '(' || cc[i] == 'x') {
						if (b < a)
							dp_[a][b + 1] = (dp_[a][b + 1] + x) % MD;
						else
							dp_[a + 1][b + 1] = (dp_[a + 1][b + 1] + x) % MD;
					}
					if ((cc[i] == ')' || cc[i] == 'x') && b > 0)
						dp_[a][b - 1] = (dp_[a][b - 1] + x) % MD;
				}
			for (a = 0; a <= n; a++)
				for (b = 0; b <= a; b++)
					dp[a][b] = dp_[a][b];
		}
		for (j = 0; j < n; j++) {
			memset(dr, 0, (n + 1) * sizeof *dr), dr[0] = 1;
			for (i = j; i >= 0; i--) {
				if (cc[i] == ')' || cc[i] == 'x') {
					for (d = 0; d <= n; d++)
						xx[r][j + 1] = (xx[r][j + 1] + (long long) dq[i][d / 2] * dr[d]) % MD;
				}
				memset(dr_, 0, (n + 1) * sizeof *dr_);
				for (d = 0; d <= n; d++) {
					int x = dr[d];

					if (x == 0)
						continue;
					if (cc[i] == '(' || cc[i] == 'x')
						dr_[d + 1] = (dr_[d + 1] + x) % MD;
					if ((cc[i] == ')' || cc[i] == 'x') && d > r)
						dr_[d - 1] = (dr_[d - 1] + x) % MD;
				}
				memcpy(dr, dr_, (n + 1) * sizeof *dr_);
			}
		}
		flip(cc, n);
	}
	ans = 0;
	for (i = 1; i < n; i++)
		ans = (ans + (long long) xx[0][i] * xx[1][n - i]) % MD;
	return ans;
}

int main() {
	static char cc[N + 1];
	int n, r, ans;

	scanf("%d%s", &n, cc);
	if (n % 2 != 0) {
		printf("0\n");
		return 0;
	}
	ans = solve1(cc, n);
	for (r = 0; r < 2; r++) {
		ans = (ans + solve2(cc, n)) % MD;
		flip(cc, n);
	}
	ans = (ans + solve3(cc, n)) % MD;
	printf("%d\n", ans);
	return 0;
}

Compilation message

securitygate.c: In function 'main':
securitygate.c:183:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  183 |  scanf("%d%s", &n, cc);
      |  ^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 10 ms 1192 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 10 ms 1268 KB Output is correct
9 Correct 9 ms 1164 KB Output is correct
10 Correct 9 ms 1164 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 5 ms 980 KB Output is correct
13 Correct 4 ms 1000 KB Output is correct
14 Correct 2 ms 808 KB Output is correct
15 Correct 11 ms 1276 KB Output is correct
16 Correct 11 ms 1236 KB Output is correct
17 Correct 10 ms 1228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 10 ms 1192 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 10 ms 1268 KB Output is correct
9 Correct 9 ms 1164 KB Output is correct
10 Correct 9 ms 1164 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 5 ms 980 KB Output is correct
13 Correct 4 ms 1000 KB Output is correct
14 Correct 2 ms 808 KB Output is correct
15 Correct 11 ms 1276 KB Output is correct
16 Correct 11 ms 1236 KB Output is correct
17 Correct 10 ms 1228 KB Output is correct
18 Correct 2 ms 724 KB Output is correct
19 Correct 9 ms 1188 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 6 ms 1084 KB Output is correct
22 Correct 9 ms 1276 KB Output is correct
23 Correct 9 ms 1168 KB Output is correct
24 Correct 10 ms 1276 KB Output is correct
25 Correct 5 ms 980 KB Output is correct
26 Correct 2 ms 724 KB Output is correct
27 Correct 8 ms 1108 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 9 ms 1160 KB Output is correct
30 Correct 10 ms 1196 KB Output is correct
31 Correct 11 ms 1280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 10 ms 1192 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 10 ms 1268 KB Output is correct
9 Correct 9 ms 1164 KB Output is correct
10 Correct 9 ms 1164 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 5 ms 980 KB Output is correct
13 Correct 4 ms 1000 KB Output is correct
14 Correct 2 ms 808 KB Output is correct
15 Correct 11 ms 1276 KB Output is correct
16 Correct 11 ms 1236 KB Output is correct
17 Correct 10 ms 1228 KB Output is correct
18 Correct 2 ms 724 KB Output is correct
19 Correct 9 ms 1188 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 6 ms 1084 KB Output is correct
22 Correct 9 ms 1276 KB Output is correct
23 Correct 9 ms 1168 KB Output is correct
24 Correct 10 ms 1276 KB Output is correct
25 Correct 5 ms 980 KB Output is correct
26 Correct 2 ms 724 KB Output is correct
27 Correct 8 ms 1108 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 9 ms 1160 KB Output is correct
30 Correct 10 ms 1196 KB Output is correct
31 Correct 11 ms 1280 KB Output is correct
32 Correct 1 ms 424 KB Output is correct
33 Correct 1 ms 420 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 10 ms 1236 KB Output is correct
37 Correct 10 ms 1268 KB Output is correct
38 Correct 9 ms 1236 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 8 ms 1144 KB Output is correct
41 Correct 8 ms 1108 KB Output is correct
42 Correct 11 ms 1148 KB Output is correct
43 Correct 10 ms 1236 KB Output is correct
44 Correct 10 ms 1272 KB Output is correct
45 Correct 12 ms 1316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 10 ms 1192 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 10 ms 1268 KB Output is correct
9 Correct 9 ms 1164 KB Output is correct
10 Correct 9 ms 1164 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 5 ms 980 KB Output is correct
13 Correct 4 ms 1000 KB Output is correct
14 Correct 2 ms 808 KB Output is correct
15 Correct 11 ms 1276 KB Output is correct
16 Correct 11 ms 1236 KB Output is correct
17 Correct 10 ms 1228 KB Output is correct
18 Correct 2 ms 724 KB Output is correct
19 Correct 9 ms 1188 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 6 ms 1084 KB Output is correct
22 Correct 9 ms 1276 KB Output is correct
23 Correct 9 ms 1168 KB Output is correct
24 Correct 10 ms 1276 KB Output is correct
25 Correct 5 ms 980 KB Output is correct
26 Correct 2 ms 724 KB Output is correct
27 Correct 8 ms 1108 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 9 ms 1160 KB Output is correct
30 Correct 10 ms 1196 KB Output is correct
31 Correct 11 ms 1280 KB Output is correct
32 Correct 1 ms 424 KB Output is correct
33 Correct 1 ms 420 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 10 ms 1236 KB Output is correct
37 Correct 10 ms 1268 KB Output is correct
38 Correct 9 ms 1236 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 8 ms 1144 KB Output is correct
41 Correct 8 ms 1108 KB Output is correct
42 Correct 11 ms 1148 KB Output is correct
43 Correct 10 ms 1236 KB Output is correct
44 Correct 10 ms 1272 KB Output is correct
45 Correct 12 ms 1316 KB Output is correct
46 Correct 1 ms 288 KB Output is correct
47 Correct 8 ms 1236 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 9 ms 1200 KB Output is correct
50 Correct 4 ms 852 KB Output is correct
51 Correct 1 ms 468 KB Output is correct
52 Correct 8 ms 1108 KB Output is correct
53 Correct 12 ms 1276 KB Output is correct
54 Correct 15 ms 1276 KB Output is correct
55 Correct 14 ms 1168 KB Output is correct
56 Correct 2 ms 724 KB Output is correct
57 Correct 1 ms 596 KB Output is correct
58 Correct 2 ms 596 KB Output is correct
59 Correct 6 ms 980 KB Output is correct
60 Correct 12 ms 1196 KB Output is correct
61 Correct 10 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 292 KB Output is correct
2 Correct 10 ms 1192 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 596 KB Output is correct
6 Correct 2 ms 724 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 10 ms 1268 KB Output is correct
9 Correct 9 ms 1164 KB Output is correct
10 Correct 9 ms 1164 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 5 ms 980 KB Output is correct
13 Correct 4 ms 1000 KB Output is correct
14 Correct 2 ms 808 KB Output is correct
15 Correct 11 ms 1276 KB Output is correct
16 Correct 11 ms 1236 KB Output is correct
17 Correct 10 ms 1228 KB Output is correct
18 Correct 2 ms 724 KB Output is correct
19 Correct 9 ms 1188 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 6 ms 1084 KB Output is correct
22 Correct 9 ms 1276 KB Output is correct
23 Correct 9 ms 1168 KB Output is correct
24 Correct 10 ms 1276 KB Output is correct
25 Correct 5 ms 980 KB Output is correct
26 Correct 2 ms 724 KB Output is correct
27 Correct 8 ms 1108 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 9 ms 1160 KB Output is correct
30 Correct 10 ms 1196 KB Output is correct
31 Correct 11 ms 1280 KB Output is correct
32 Correct 1 ms 424 KB Output is correct
33 Correct 1 ms 420 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 10 ms 1236 KB Output is correct
37 Correct 10 ms 1268 KB Output is correct
38 Correct 9 ms 1236 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 8 ms 1144 KB Output is correct
41 Correct 8 ms 1108 KB Output is correct
42 Correct 11 ms 1148 KB Output is correct
43 Correct 10 ms 1236 KB Output is correct
44 Correct 10 ms 1272 KB Output is correct
45 Correct 12 ms 1316 KB Output is correct
46 Correct 1 ms 288 KB Output is correct
47 Correct 8 ms 1236 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 9 ms 1200 KB Output is correct
50 Correct 4 ms 852 KB Output is correct
51 Correct 1 ms 468 KB Output is correct
52 Correct 8 ms 1108 KB Output is correct
53 Correct 12 ms 1276 KB Output is correct
54 Correct 15 ms 1276 KB Output is correct
55 Correct 14 ms 1168 KB Output is correct
56 Correct 2 ms 724 KB Output is correct
57 Correct 1 ms 596 KB Output is correct
58 Correct 2 ms 596 KB Output is correct
59 Correct 6 ms 980 KB Output is correct
60 Correct 12 ms 1196 KB Output is correct
61 Correct 10 ms 1236 KB Output is correct
62 Correct 140 ms 2404 KB Output is correct
63 Correct 132 ms 2404 KB Output is correct
64 Correct 57 ms 1872 KB Output is correct
65 Correct 31 ms 1492 KB Output is correct
66 Correct 294 ms 3108 KB Output is correct
67 Correct 274 ms 3112 KB Output is correct
68 Correct 341 ms 3220 KB Output is correct
69 Correct 41 ms 1880 KB Output is correct
70 Correct 64 ms 2200 KB Output is correct
71 Correct 27 ms 1320 KB Output is correct
72 Correct 34 ms 1484 KB Output is correct
73 Correct 295 ms 3108 KB Output is correct
74 Correct 236 ms 3020 KB Output is correct