Submission #1018204

# Submission time Handle Problem Language Result Execution time Memory
1018204 2024-07-09T16:20:24 Z mnbvcxz123 Snake Escaping (JOI18_snake_escaping) C++17
100 / 100
571 ms 43492 KB
#pragma GCC optimize("O3")

#include <bits/stdc++.h>

int main() {
	using namespace std;
	ios_base::sync_with_stdio(false); 
	cin.tie(NULL);

	int L, Q;
	cin >> L >> Q;

	string S;
	cin >> S;
	vector<int> A(1 << L), dp1(1 << L), dp2(1 << L);
	for (int i = 0; i < (1 << L); i++)
		A[i] = dp1[i] = dp2[i] = S[i] - '0';
	
	for (int b = 0; b < L; b++) {
		for (int m = 0; m < (1 << L); m++) {
			if (m >> b & 1)
				dp1[m] += dp1[m ^ (1 << b)];
			else 
				dp2[m] += dp2[m ^ (1 << b)];
		}
	}
	
	while (Q--) {
		string S;
		cin >> S;
		reverse(S.begin(), S.end());
		int s0 = 0, s1 = 0, s2 = 0;
		int m0 = 0, m1 = 0, m2 = 0;
		for (int i = 0; i < L; i++) {
			char c = S[i];
			if (c == '?') {
				s2++;
				m2 |= (1 << i);
			} else if (c == '0') {
				s0++;
				m0 |= (1 << i);
			} else if (c == '1') {
				s1++;
				m1 |= (1 << i);
			}
		}

		int s = min({s0, s1, s2});
		if (s == s2) {
			int ans = A[m1];
			for (int m = m2; m > 0; m = (m - 1) & m2) 
				ans += A[m1 | m];
			cout << ans << '\n';
		} else if (s == s0) {
			int ans = dp2[m1];
			for (int m = m0; m > 0; m = (m - 1) & m0) {
				if (__builtin_popcount(m) % 2 == 0)
					ans += dp2[m1 | m];
				else 
					ans -= dp2[m1 | m];
			}
			cout << ans << '\n';
		} else if (s == s1) {
			int ans = s1 % 2 == 0 ? dp1[m2] : -dp1[m2];
			for (int m = m1; m > 0; m = (m - 1) & m1) {
				if (__builtin_popcount(m) % 2 == s1 % 2)
					ans += dp1[m2 | m];
				else 
					ans -= dp1[m2 | m];
			}
			cout << ans << '\n';
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 143 ms 15180 KB Output is correct
12 Correct 156 ms 14676 KB Output is correct
13 Correct 175 ms 13904 KB Output is correct
14 Correct 168 ms 14160 KB Output is correct
15 Correct 170 ms 15144 KB Output is correct
16 Correct 179 ms 14164 KB Output is correct
17 Correct 185 ms 14184 KB Output is correct
18 Correct 120 ms 16212 KB Output is correct
19 Correct 164 ms 13036 KB Output is correct
20 Correct 150 ms 14676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 143 ms 15180 KB Output is correct
12 Correct 156 ms 14676 KB Output is correct
13 Correct 175 ms 13904 KB Output is correct
14 Correct 168 ms 14160 KB Output is correct
15 Correct 170 ms 15144 KB Output is correct
16 Correct 179 ms 14164 KB Output is correct
17 Correct 185 ms 14184 KB Output is correct
18 Correct 120 ms 16212 KB Output is correct
19 Correct 164 ms 13036 KB Output is correct
20 Correct 150 ms 14676 KB Output is correct
21 Correct 157 ms 18260 KB Output is correct
22 Correct 197 ms 18376 KB Output is correct
23 Correct 202 ms 17240 KB Output is correct
24 Correct 192 ms 17236 KB Output is correct
25 Correct 198 ms 19276 KB Output is correct
26 Correct 215 ms 17724 KB Output is correct
27 Correct 224 ms 17736 KB Output is correct
28 Correct 125 ms 20296 KB Output is correct
29 Correct 197 ms 16228 KB Output is correct
30 Correct 173 ms 18260 KB Output is correct
31 Correct 217 ms 18120 KB Output is correct
32 Correct 254 ms 18112 KB Output is correct
33 Correct 181 ms 16980 KB Output is correct
34 Correct 216 ms 17204 KB Output is correct
35 Correct 223 ms 17628 KB Output is correct
36 Correct 112 ms 16212 KB Output is correct
37 Correct 167 ms 18280 KB Output is correct
38 Correct 192 ms 16212 KB Output is correct
39 Correct 224 ms 17232 KB Output is correct
40 Correct 224 ms 17232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 36 ms 16116 KB Output is correct
12 Correct 34 ms 16124 KB Output is correct
13 Correct 36 ms 16128 KB Output is correct
14 Correct 53 ms 15960 KB Output is correct
15 Correct 36 ms 16112 KB Output is correct
16 Correct 44 ms 16120 KB Output is correct
17 Correct 39 ms 16168 KB Output is correct
18 Correct 40 ms 16356 KB Output is correct
19 Correct 35 ms 15860 KB Output is correct
20 Correct 34 ms 16120 KB Output is correct
21 Correct 38 ms 16108 KB Output is correct
22 Correct 51 ms 16120 KB Output is correct
23 Correct 33 ms 16112 KB Output is correct
24 Correct 41 ms 16120 KB Output is correct
25 Correct 41 ms 16116 KB Output is correct
26 Correct 30 ms 15972 KB Output is correct
27 Correct 34 ms 16116 KB Output is correct
28 Correct 34 ms 15856 KB Output is correct
29 Correct 35 ms 16124 KB Output is correct
30 Correct 38 ms 15964 KB Output is correct
31 Correct 34 ms 16128 KB Output is correct
32 Correct 44 ms 16120 KB Output is correct
33 Correct 43 ms 16036 KB Output is correct
34 Correct 35 ms 15976 KB Output is correct
35 Correct 41 ms 16116 KB Output is correct
36 Correct 40 ms 16116 KB Output is correct
37 Correct 38 ms 16068 KB Output is correct
38 Correct 41 ms 16112 KB Output is correct
39 Correct 42 ms 16116 KB Output is correct
40 Correct 41 ms 16168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 143 ms 15180 KB Output is correct
12 Correct 156 ms 14676 KB Output is correct
13 Correct 175 ms 13904 KB Output is correct
14 Correct 168 ms 14160 KB Output is correct
15 Correct 170 ms 15144 KB Output is correct
16 Correct 179 ms 14164 KB Output is correct
17 Correct 185 ms 14184 KB Output is correct
18 Correct 120 ms 16212 KB Output is correct
19 Correct 164 ms 13036 KB Output is correct
20 Correct 150 ms 14676 KB Output is correct
21 Correct 157 ms 18260 KB Output is correct
22 Correct 197 ms 18376 KB Output is correct
23 Correct 202 ms 17240 KB Output is correct
24 Correct 192 ms 17236 KB Output is correct
25 Correct 198 ms 19276 KB Output is correct
26 Correct 215 ms 17724 KB Output is correct
27 Correct 224 ms 17736 KB Output is correct
28 Correct 125 ms 20296 KB Output is correct
29 Correct 197 ms 16228 KB Output is correct
30 Correct 173 ms 18260 KB Output is correct
31 Correct 217 ms 18120 KB Output is correct
32 Correct 254 ms 18112 KB Output is correct
33 Correct 181 ms 16980 KB Output is correct
34 Correct 216 ms 17204 KB Output is correct
35 Correct 223 ms 17628 KB Output is correct
36 Correct 112 ms 16212 KB Output is correct
37 Correct 167 ms 18280 KB Output is correct
38 Correct 192 ms 16212 KB Output is correct
39 Correct 224 ms 17232 KB Output is correct
40 Correct 224 ms 17232 KB Output is correct
41 Correct 36 ms 16116 KB Output is correct
42 Correct 34 ms 16124 KB Output is correct
43 Correct 36 ms 16128 KB Output is correct
44 Correct 53 ms 15960 KB Output is correct
45 Correct 36 ms 16112 KB Output is correct
46 Correct 44 ms 16120 KB Output is correct
47 Correct 39 ms 16168 KB Output is correct
48 Correct 40 ms 16356 KB Output is correct
49 Correct 35 ms 15860 KB Output is correct
50 Correct 34 ms 16120 KB Output is correct
51 Correct 38 ms 16108 KB Output is correct
52 Correct 51 ms 16120 KB Output is correct
53 Correct 33 ms 16112 KB Output is correct
54 Correct 41 ms 16120 KB Output is correct
55 Correct 41 ms 16116 KB Output is correct
56 Correct 30 ms 15972 KB Output is correct
57 Correct 34 ms 16116 KB Output is correct
58 Correct 34 ms 15856 KB Output is correct
59 Correct 35 ms 16124 KB Output is correct
60 Correct 38 ms 15964 KB Output is correct
61 Correct 34 ms 16128 KB Output is correct
62 Correct 44 ms 16120 KB Output is correct
63 Correct 43 ms 16036 KB Output is correct
64 Correct 35 ms 15976 KB Output is correct
65 Correct 41 ms 16116 KB Output is correct
66 Correct 40 ms 16116 KB Output is correct
67 Correct 38 ms 16068 KB Output is correct
68 Correct 41 ms 16112 KB Output is correct
69 Correct 42 ms 16116 KB Output is correct
70 Correct 41 ms 16168 KB Output is correct
71 Correct 278 ms 40288 KB Output is correct
72 Correct 317 ms 40668 KB Output is correct
73 Correct 355 ms 39124 KB Output is correct
74 Correct 571 ms 39372 KB Output is correct
75 Correct 306 ms 41456 KB Output is correct
76 Correct 459 ms 39672 KB Output is correct
77 Correct 416 ms 39668 KB Output is correct
78 Correct 178 ms 43492 KB Output is correct
79 Correct 282 ms 37476 KB Output is correct
80 Correct 260 ms 40484 KB Output is correct
81 Correct 357 ms 40436 KB Output is correct
82 Correct 569 ms 39488 KB Output is correct
83 Correct 257 ms 38444 KB Output is correct
84 Correct 468 ms 39452 KB Output is correct
85 Correct 403 ms 39628 KB Output is correct
86 Correct 169 ms 37600 KB Output is correct
87 Correct 258 ms 40412 KB Output is correct
88 Correct 309 ms 37364 KB Output is correct
89 Correct 319 ms 39096 KB Output is correct
90 Correct 384 ms 39424 KB Output is correct
91 Correct 270 ms 38640 KB Output is correct
92 Correct 501 ms 39844 KB Output is correct
93 Correct 430 ms 39668 KB Output is correct
94 Correct 164 ms 37364 KB Output is correct
95 Correct 397 ms 40436 KB Output is correct
96 Correct 350 ms 39404 KB Output is correct
97 Correct 379 ms 39424 KB Output is correct
98 Correct 365 ms 39520 KB Output is correct
99 Correct 362 ms 39412 KB Output is correct
100 Correct 403 ms 39428 KB Output is correct