// https://oj.uz/problem/view/JOI18_snake_escaping
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
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 |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
332 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 |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
332 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 |
200 ms |
14988 KB |
Output is correct |
12 |
Correct |
239 ms |
14780 KB |
Output is correct |
13 |
Correct |
210 ms |
13900 KB |
Output is correct |
14 |
Correct |
227 ms |
14024 KB |
Output is correct |
15 |
Correct |
273 ms |
15068 KB |
Output is correct |
16 |
Correct |
240 ms |
14220 KB |
Output is correct |
17 |
Correct |
241 ms |
14156 KB |
Output is correct |
18 |
Correct |
181 ms |
16016 KB |
Output is correct |
19 |
Correct |
193 ms |
13040 KB |
Output is correct |
20 |
Correct |
213 ms |
14712 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 |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
332 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 |
200 ms |
14988 KB |
Output is correct |
12 |
Correct |
239 ms |
14780 KB |
Output is correct |
13 |
Correct |
210 ms |
13900 KB |
Output is correct |
14 |
Correct |
227 ms |
14024 KB |
Output is correct |
15 |
Correct |
273 ms |
15068 KB |
Output is correct |
16 |
Correct |
240 ms |
14220 KB |
Output is correct |
17 |
Correct |
241 ms |
14156 KB |
Output is correct |
18 |
Correct |
181 ms |
16016 KB |
Output is correct |
19 |
Correct |
193 ms |
13040 KB |
Output is correct |
20 |
Correct |
213 ms |
14712 KB |
Output is correct |
21 |
Correct |
210 ms |
18120 KB |
Output is correct |
22 |
Correct |
223 ms |
18232 KB |
Output is correct |
23 |
Correct |
263 ms |
17300 KB |
Output is correct |
24 |
Correct |
253 ms |
17100 KB |
Output is correct |
25 |
Correct |
262 ms |
19072 KB |
Output is correct |
26 |
Correct |
284 ms |
17628 KB |
Output is correct |
27 |
Correct |
290 ms |
17536 KB |
Output is correct |
28 |
Correct |
180 ms |
20172 KB |
Output is correct |
29 |
Correct |
208 ms |
16104 KB |
Output is correct |
30 |
Correct |
242 ms |
18248 KB |
Output is correct |
31 |
Correct |
293 ms |
18024 KB |
Output is correct |
32 |
Correct |
303 ms |
18068 KB |
Output is correct |
33 |
Correct |
241 ms |
16928 KB |
Output is correct |
34 |
Correct |
277 ms |
17092 KB |
Output is correct |
35 |
Correct |
297 ms |
17556 KB |
Output is correct |
36 |
Correct |
163 ms |
16076 KB |
Output is correct |
37 |
Correct |
223 ms |
18008 KB |
Output is correct |
38 |
Correct |
230 ms |
16116 KB |
Output is correct |
39 |
Correct |
270 ms |
17212 KB |
Output is correct |
40 |
Correct |
269 ms |
17024 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 |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
332 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 |
47 ms |
16084 KB |
Output is correct |
12 |
Correct |
47 ms |
16100 KB |
Output is correct |
13 |
Correct |
51 ms |
15920 KB |
Output is correct |
14 |
Correct |
68 ms |
16016 KB |
Output is correct |
15 |
Correct |
52 ms |
16072 KB |
Output is correct |
16 |
Correct |
76 ms |
16020 KB |
Output is correct |
17 |
Correct |
64 ms |
15948 KB |
Output is correct |
18 |
Correct |
46 ms |
16144 KB |
Output is correct |
19 |
Correct |
56 ms |
15836 KB |
Output is correct |
20 |
Correct |
46 ms |
16016 KB |
Output is correct |
21 |
Correct |
54 ms |
16088 KB |
Output is correct |
22 |
Correct |
61 ms |
15968 KB |
Output is correct |
23 |
Correct |
53 ms |
15892 KB |
Output is correct |
24 |
Correct |
68 ms |
16032 KB |
Output is correct |
25 |
Correct |
68 ms |
16068 KB |
Output is correct |
26 |
Correct |
57 ms |
15848 KB |
Output is correct |
27 |
Correct |
47 ms |
16072 KB |
Output is correct |
28 |
Correct |
48 ms |
15820 KB |
Output is correct |
29 |
Correct |
54 ms |
15984 KB |
Output is correct |
30 |
Correct |
68 ms |
15956 KB |
Output is correct |
31 |
Correct |
52 ms |
15948 KB |
Output is correct |
32 |
Correct |
76 ms |
16068 KB |
Output is correct |
33 |
Correct |
63 ms |
15936 KB |
Output is correct |
34 |
Correct |
43 ms |
15820 KB |
Output is correct |
35 |
Correct |
70 ms |
16040 KB |
Output is correct |
36 |
Correct |
73 ms |
15988 KB |
Output is correct |
37 |
Correct |
59 ms |
16028 KB |
Output is correct |
38 |
Correct |
74 ms |
15980 KB |
Output is correct |
39 |
Correct |
72 ms |
15976 KB |
Output is correct |
40 |
Correct |
82 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 |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
332 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 |
200 ms |
14988 KB |
Output is correct |
12 |
Correct |
239 ms |
14780 KB |
Output is correct |
13 |
Correct |
210 ms |
13900 KB |
Output is correct |
14 |
Correct |
227 ms |
14024 KB |
Output is correct |
15 |
Correct |
273 ms |
15068 KB |
Output is correct |
16 |
Correct |
240 ms |
14220 KB |
Output is correct |
17 |
Correct |
241 ms |
14156 KB |
Output is correct |
18 |
Correct |
181 ms |
16016 KB |
Output is correct |
19 |
Correct |
193 ms |
13040 KB |
Output is correct |
20 |
Correct |
213 ms |
14712 KB |
Output is correct |
21 |
Correct |
210 ms |
18120 KB |
Output is correct |
22 |
Correct |
223 ms |
18232 KB |
Output is correct |
23 |
Correct |
263 ms |
17300 KB |
Output is correct |
24 |
Correct |
253 ms |
17100 KB |
Output is correct |
25 |
Correct |
262 ms |
19072 KB |
Output is correct |
26 |
Correct |
284 ms |
17628 KB |
Output is correct |
27 |
Correct |
290 ms |
17536 KB |
Output is correct |
28 |
Correct |
180 ms |
20172 KB |
Output is correct |
29 |
Correct |
208 ms |
16104 KB |
Output is correct |
30 |
Correct |
242 ms |
18248 KB |
Output is correct |
31 |
Correct |
293 ms |
18024 KB |
Output is correct |
32 |
Correct |
303 ms |
18068 KB |
Output is correct |
33 |
Correct |
241 ms |
16928 KB |
Output is correct |
34 |
Correct |
277 ms |
17092 KB |
Output is correct |
35 |
Correct |
297 ms |
17556 KB |
Output is correct |
36 |
Correct |
163 ms |
16076 KB |
Output is correct |
37 |
Correct |
223 ms |
18008 KB |
Output is correct |
38 |
Correct |
230 ms |
16116 KB |
Output is correct |
39 |
Correct |
270 ms |
17212 KB |
Output is correct |
40 |
Correct |
269 ms |
17024 KB |
Output is correct |
41 |
Correct |
47 ms |
16084 KB |
Output is correct |
42 |
Correct |
47 ms |
16100 KB |
Output is correct |
43 |
Correct |
51 ms |
15920 KB |
Output is correct |
44 |
Correct |
68 ms |
16016 KB |
Output is correct |
45 |
Correct |
52 ms |
16072 KB |
Output is correct |
46 |
Correct |
76 ms |
16020 KB |
Output is correct |
47 |
Correct |
64 ms |
15948 KB |
Output is correct |
48 |
Correct |
46 ms |
16144 KB |
Output is correct |
49 |
Correct |
56 ms |
15836 KB |
Output is correct |
50 |
Correct |
46 ms |
16016 KB |
Output is correct |
51 |
Correct |
54 ms |
16088 KB |
Output is correct |
52 |
Correct |
61 ms |
15968 KB |
Output is correct |
53 |
Correct |
53 ms |
15892 KB |
Output is correct |
54 |
Correct |
68 ms |
16032 KB |
Output is correct |
55 |
Correct |
68 ms |
16068 KB |
Output is correct |
56 |
Correct |
57 ms |
15848 KB |
Output is correct |
57 |
Correct |
47 ms |
16072 KB |
Output is correct |
58 |
Correct |
48 ms |
15820 KB |
Output is correct |
59 |
Correct |
54 ms |
15984 KB |
Output is correct |
60 |
Correct |
68 ms |
15956 KB |
Output is correct |
61 |
Correct |
52 ms |
15948 KB |
Output is correct |
62 |
Correct |
76 ms |
16068 KB |
Output is correct |
63 |
Correct |
63 ms |
15936 KB |
Output is correct |
64 |
Correct |
43 ms |
15820 KB |
Output is correct |
65 |
Correct |
70 ms |
16040 KB |
Output is correct |
66 |
Correct |
73 ms |
15988 KB |
Output is correct |
67 |
Correct |
59 ms |
16028 KB |
Output is correct |
68 |
Correct |
74 ms |
15980 KB |
Output is correct |
69 |
Correct |
72 ms |
15976 KB |
Output is correct |
70 |
Correct |
82 ms |
15956 KB |
Output is correct |
71 |
Correct |
392 ms |
40300 KB |
Output is correct |
72 |
Correct |
463 ms |
40452 KB |
Output is correct |
73 |
Correct |
606 ms |
38968 KB |
Output is correct |
74 |
Correct |
804 ms |
39312 KB |
Output is correct |
75 |
Correct |
577 ms |
41348 KB |
Output is correct |
76 |
Correct |
776 ms |
39612 KB |
Output is correct |
77 |
Correct |
770 ms |
39520 KB |
Output is correct |
78 |
Correct |
255 ms |
43312 KB |
Output is correct |
79 |
Correct |
413 ms |
37424 KB |
Output is correct |
80 |
Correct |
399 ms |
40472 KB |
Output is correct |
81 |
Correct |
669 ms |
40328 KB |
Output is correct |
82 |
Correct |
783 ms |
39392 KB |
Output is correct |
83 |
Correct |
424 ms |
38560 KB |
Output is correct |
84 |
Correct |
709 ms |
39264 KB |
Output is correct |
85 |
Correct |
785 ms |
39460 KB |
Output is correct |
86 |
Correct |
269 ms |
37416 KB |
Output is correct |
87 |
Correct |
374 ms |
40240 KB |
Output is correct |
88 |
Correct |
382 ms |
37260 KB |
Output is correct |
89 |
Correct |
507 ms |
38944 KB |
Output is correct |
90 |
Correct |
480 ms |
39272 KB |
Output is correct |
91 |
Correct |
381 ms |
38468 KB |
Output is correct |
92 |
Correct |
870 ms |
39724 KB |
Output is correct |
93 |
Correct |
723 ms |
39492 KB |
Output is correct |
94 |
Correct |
291 ms |
37280 KB |
Output is correct |
95 |
Correct |
646 ms |
39312 KB |
Output is correct |
96 |
Correct |
653 ms |
39368 KB |
Output is correct |
97 |
Correct |
674 ms |
39344 KB |
Output is correct |
98 |
Correct |
687 ms |
39336 KB |
Output is correct |
99 |
Correct |
644 ms |
39436 KB |
Output is correct |
100 |
Correct |
682 ms |
39420 KB |
Output is correct |