Submission #627508

# Submission time Handle Problem Language Result Execution time Memory
627508 2022-08-12T15:58:00 Z dutinmeow Snake Escaping (JOI18_snake_escaping) C++14
100 / 100
1086 ms 43380 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 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 210 ms 15076 KB Output is correct
12 Correct 212 ms 14740 KB Output is correct
13 Correct 231 ms 13912 KB Output is correct
14 Correct 224 ms 13992 KB Output is correct
15 Correct 219 ms 15028 KB Output is correct
16 Correct 237 ms 14352 KB Output is correct
17 Correct 253 ms 14372 KB Output is correct
18 Correct 161 ms 16036 KB Output is correct
19 Correct 202 ms 13040 KB Output is correct
20 Correct 206 ms 14696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 210 ms 15076 KB Output is correct
12 Correct 212 ms 14740 KB Output is correct
13 Correct 231 ms 13912 KB Output is correct
14 Correct 224 ms 13992 KB Output is correct
15 Correct 219 ms 15028 KB Output is correct
16 Correct 237 ms 14352 KB Output is correct
17 Correct 253 ms 14372 KB Output is correct
18 Correct 161 ms 16036 KB Output is correct
19 Correct 202 ms 13040 KB Output is correct
20 Correct 206 ms 14696 KB Output is correct
21 Correct 214 ms 18140 KB Output is correct
22 Correct 246 ms 18304 KB Output is correct
23 Correct 269 ms 17320 KB Output is correct
24 Correct 260 ms 17100 KB Output is correct
25 Correct 259 ms 19260 KB Output is correct
26 Correct 278 ms 17620 KB Output is correct
27 Correct 280 ms 17584 KB Output is correct
28 Correct 183 ms 20100 KB Output is correct
29 Correct 225 ms 16076 KB Output is correct
30 Correct 221 ms 18208 KB Output is correct
31 Correct 310 ms 18096 KB Output is correct
32 Correct 341 ms 18196 KB Output is correct
33 Correct 241 ms 16944 KB Output is correct
34 Correct 273 ms 17100 KB Output is correct
35 Correct 287 ms 17592 KB Output is correct
36 Correct 170 ms 16076 KB Output is correct
37 Correct 217 ms 18168 KB Output is correct
38 Correct 231 ms 16088 KB Output is correct
39 Correct 253 ms 17228 KB Output is correct
40 Correct 256 ms 17144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 43 ms 16100 KB Output is correct
12 Correct 52 ms 16084 KB Output is correct
13 Correct 58 ms 16028 KB Output is correct
14 Correct 57 ms 16020 KB Output is correct
15 Correct 45 ms 16024 KB Output is correct
16 Correct 66 ms 16040 KB Output is correct
17 Correct 83 ms 15948 KB Output is correct
18 Correct 45 ms 16204 KB Output is correct
19 Correct 44 ms 15836 KB Output is correct
20 Correct 45 ms 16096 KB Output is correct
21 Correct 56 ms 16088 KB Output is correct
22 Correct 61 ms 15948 KB Output is correct
23 Correct 49 ms 15980 KB Output is correct
24 Correct 75 ms 15980 KB Output is correct
25 Correct 64 ms 15960 KB Output is correct
26 Correct 39 ms 15868 KB Output is correct
27 Correct 48 ms 16072 KB Output is correct
28 Correct 51 ms 15820 KB Output is correct
29 Correct 67 ms 16020 KB Output is correct
30 Correct 50 ms 15952 KB Output is correct
31 Correct 45 ms 15980 KB Output is correct
32 Correct 73 ms 15992 KB Output is correct
33 Correct 60 ms 15960 KB Output is correct
34 Correct 44 ms 15908 KB Output is correct
35 Correct 63 ms 15964 KB Output is correct
36 Correct 57 ms 15948 KB Output is correct
37 Correct 76 ms 15984 KB Output is correct
38 Correct 59 ms 16016 KB Output is correct
39 Correct 59 ms 16032 KB Output is correct
40 Correct 56 ms 15956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 210 ms 15076 KB Output is correct
12 Correct 212 ms 14740 KB Output is correct
13 Correct 231 ms 13912 KB Output is correct
14 Correct 224 ms 13992 KB Output is correct
15 Correct 219 ms 15028 KB Output is correct
16 Correct 237 ms 14352 KB Output is correct
17 Correct 253 ms 14372 KB Output is correct
18 Correct 161 ms 16036 KB Output is correct
19 Correct 202 ms 13040 KB Output is correct
20 Correct 206 ms 14696 KB Output is correct
21 Correct 214 ms 18140 KB Output is correct
22 Correct 246 ms 18304 KB Output is correct
23 Correct 269 ms 17320 KB Output is correct
24 Correct 260 ms 17100 KB Output is correct
25 Correct 259 ms 19260 KB Output is correct
26 Correct 278 ms 17620 KB Output is correct
27 Correct 280 ms 17584 KB Output is correct
28 Correct 183 ms 20100 KB Output is correct
29 Correct 225 ms 16076 KB Output is correct
30 Correct 221 ms 18208 KB Output is correct
31 Correct 310 ms 18096 KB Output is correct
32 Correct 341 ms 18196 KB Output is correct
33 Correct 241 ms 16944 KB Output is correct
34 Correct 273 ms 17100 KB Output is correct
35 Correct 287 ms 17592 KB Output is correct
36 Correct 170 ms 16076 KB Output is correct
37 Correct 217 ms 18168 KB Output is correct
38 Correct 231 ms 16088 KB Output is correct
39 Correct 253 ms 17228 KB Output is correct
40 Correct 256 ms 17144 KB Output is correct
41 Correct 43 ms 16100 KB Output is correct
42 Correct 52 ms 16084 KB Output is correct
43 Correct 58 ms 16028 KB Output is correct
44 Correct 57 ms 16020 KB Output is correct
45 Correct 45 ms 16024 KB Output is correct
46 Correct 66 ms 16040 KB Output is correct
47 Correct 83 ms 15948 KB Output is correct
48 Correct 45 ms 16204 KB Output is correct
49 Correct 44 ms 15836 KB Output is correct
50 Correct 45 ms 16096 KB Output is correct
51 Correct 56 ms 16088 KB Output is correct
52 Correct 61 ms 15948 KB Output is correct
53 Correct 49 ms 15980 KB Output is correct
54 Correct 75 ms 15980 KB Output is correct
55 Correct 64 ms 15960 KB Output is correct
56 Correct 39 ms 15868 KB Output is correct
57 Correct 48 ms 16072 KB Output is correct
58 Correct 51 ms 15820 KB Output is correct
59 Correct 67 ms 16020 KB Output is correct
60 Correct 50 ms 15952 KB Output is correct
61 Correct 45 ms 15980 KB Output is correct
62 Correct 73 ms 15992 KB Output is correct
63 Correct 60 ms 15960 KB Output is correct
64 Correct 44 ms 15908 KB Output is correct
65 Correct 63 ms 15964 KB Output is correct
66 Correct 57 ms 15948 KB Output is correct
67 Correct 76 ms 15984 KB Output is correct
68 Correct 59 ms 16016 KB Output is correct
69 Correct 59 ms 16032 KB Output is correct
70 Correct 56 ms 15956 KB Output is correct
71 Correct 383 ms 40392 KB Output is correct
72 Correct 389 ms 40452 KB Output is correct
73 Correct 462 ms 39000 KB Output is correct
74 Correct 671 ms 39312 KB Output is correct
75 Correct 428 ms 41288 KB Output is correct
76 Correct 844 ms 39676 KB Output is correct
77 Correct 719 ms 39504 KB Output is correct
78 Correct 246 ms 43380 KB Output is correct
79 Correct 348 ms 37272 KB Output is correct
80 Correct 379 ms 40464 KB Output is correct
81 Correct 588 ms 40288 KB Output is correct
82 Correct 712 ms 39352 KB Output is correct
83 Correct 407 ms 38480 KB Output is correct
84 Correct 666 ms 39368 KB Output is correct
85 Correct 742 ms 39536 KB Output is correct
86 Correct 243 ms 37336 KB Output is correct
87 Correct 359 ms 40400 KB Output is correct
88 Correct 382 ms 37368 KB Output is correct
89 Correct 532 ms 39056 KB Output is correct
90 Correct 473 ms 39348 KB Output is correct
91 Correct 485 ms 38484 KB Output is correct
92 Correct 1086 ms 39628 KB Output is correct
93 Correct 876 ms 39488 KB Output is correct
94 Correct 312 ms 37268 KB Output is correct
95 Correct 796 ms 39488 KB Output is correct
96 Correct 677 ms 39464 KB Output is correct
97 Correct 741 ms 39472 KB Output is correct
98 Correct 730 ms 39432 KB Output is correct
99 Correct 635 ms 39452 KB Output is correct
100 Correct 819 ms 39476 KB Output is correct