#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
int toxic[1 << 20], sosSub[1 << 20], sosSuper[1 << 20];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int k, Q; cin >> k >> Q;
for (int i = 0; i < (1 << k); i++) {
char c; cin >> c;
toxic[i] = sosSub[i] = sosSuper[i] = c - '0';
}
for (int i = 0; i < k; i++) {
for (int mask = 0; mask < (1 << k); mask++) {
if (mask & (1 << i)) sosSub[mask] += sosSub[mask ^ (1 << i)];
else sosSuper[mask] += sosSuper[mask ^ (1 << i)];
}
}
int full = (1 << k) - 1;
while (Q--) {
string s; cin >> s;
reverse(all(s));
int maskZ = 0, maskO = 0, maskQ = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '0') maskZ |= (1 << i);
if (s[i] == '1') maskO |= (1 << i);
if (s[i] == '?') maskQ |= (1 << i);
}
if (__builtin_popcount(maskQ) < 7) {
int ans = 0;
for (int sub = maskQ; sub; sub = (sub - 1) & maskQ)
ans += toxic[sub | maskO];
cout << ans + toxic[maskO] << "\n";
}
else if (__builtin_popcount(maskZ) < 7) {
int ans = sosSuper[maskO];
for (int sub = maskZ; sub; sub = (sub - 1) & maskZ) {
if (__builtin_parity(sub)) ans -= sosSuper[sub | maskO];
else ans += sosSuper[sub | maskO];
}
cout << ans << "\n";
}
else if (__builtin_popcount(maskO) < 7) {
int ans = sosSub[maskO | maskQ];
for (int sub = maskO; sub; sub = (sub - 1) & maskO) {
int inv = maskO ^ sub;
if (__builtin_parity(sub)) ans -= sosSub[inv | maskQ];
else ans += sosSub[inv | maskQ];
}
cout << ans << "\n";
}
}
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:39:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
snake_escaping.cpp:33:9: warning: unused variable 'full' [-Wunused-variable]
33 | int full = (1 << k) - 1;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4452 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4452 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
205 ms |
19112 KB |
Output is correct |
12 |
Correct |
195 ms |
18840 KB |
Output is correct |
13 |
Correct |
187 ms |
18256 KB |
Output is correct |
14 |
Correct |
169 ms |
18240 KB |
Output is correct |
15 |
Correct |
227 ms |
19188 KB |
Output is correct |
16 |
Correct |
196 ms |
18640 KB |
Output is correct |
17 |
Correct |
194 ms |
18252 KB |
Output is correct |
18 |
Correct |
125 ms |
20272 KB |
Output is correct |
19 |
Correct |
189 ms |
17232 KB |
Output is correct |
20 |
Correct |
191 ms |
18772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4452 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
205 ms |
19112 KB |
Output is correct |
12 |
Correct |
195 ms |
18840 KB |
Output is correct |
13 |
Correct |
187 ms |
18256 KB |
Output is correct |
14 |
Correct |
169 ms |
18240 KB |
Output is correct |
15 |
Correct |
227 ms |
19188 KB |
Output is correct |
16 |
Correct |
196 ms |
18640 KB |
Output is correct |
17 |
Correct |
194 ms |
18252 KB |
Output is correct |
18 |
Correct |
125 ms |
20272 KB |
Output is correct |
19 |
Correct |
189 ms |
17232 KB |
Output is correct |
20 |
Correct |
191 ms |
18772 KB |
Output is correct |
21 |
Correct |
190 ms |
22100 KB |
Output is correct |
22 |
Correct |
240 ms |
22248 KB |
Output is correct |
23 |
Correct |
235 ms |
21332 KB |
Output is correct |
24 |
Correct |
203 ms |
21168 KB |
Output is correct |
25 |
Correct |
195 ms |
23144 KB |
Output is correct |
26 |
Correct |
191 ms |
21588 KB |
Output is correct |
27 |
Correct |
231 ms |
21664 KB |
Output is correct |
28 |
Correct |
120 ms |
24096 KB |
Output is correct |
29 |
Correct |
147 ms |
20436 KB |
Output is correct |
30 |
Correct |
185 ms |
22360 KB |
Output is correct |
31 |
Correct |
214 ms |
22100 KB |
Output is correct |
32 |
Correct |
244 ms |
22100 KB |
Output is correct |
33 |
Correct |
194 ms |
21172 KB |
Output is correct |
34 |
Correct |
199 ms |
21184 KB |
Output is correct |
35 |
Correct |
226 ms |
21616 KB |
Output is correct |
36 |
Correct |
136 ms |
20280 KB |
Output is correct |
37 |
Correct |
219 ms |
22100 KB |
Output is correct |
38 |
Correct |
174 ms |
20200 KB |
Output is correct |
39 |
Correct |
220 ms |
21468 KB |
Output is correct |
40 |
Correct |
199 ms |
21072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4452 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
34 ms |
14932 KB |
Output is correct |
12 |
Correct |
52 ms |
14928 KB |
Output is correct |
13 |
Correct |
39 ms |
14928 KB |
Output is correct |
14 |
Correct |
42 ms |
14932 KB |
Output is correct |
15 |
Correct |
35 ms |
14940 KB |
Output is correct |
16 |
Correct |
47 ms |
14988 KB |
Output is correct |
17 |
Correct |
50 ms |
14928 KB |
Output is correct |
18 |
Correct |
47 ms |
15032 KB |
Output is correct |
19 |
Correct |
45 ms |
14672 KB |
Output is correct |
20 |
Correct |
33 ms |
14928 KB |
Output is correct |
21 |
Correct |
40 ms |
15052 KB |
Output is correct |
22 |
Correct |
44 ms |
14788 KB |
Output is correct |
23 |
Correct |
32 ms |
14928 KB |
Output is correct |
24 |
Correct |
40 ms |
14932 KB |
Output is correct |
25 |
Correct |
58 ms |
14780 KB |
Output is correct |
26 |
Correct |
40 ms |
14780 KB |
Output is correct |
27 |
Correct |
37 ms |
14928 KB |
Output is correct |
28 |
Correct |
31 ms |
14672 KB |
Output is correct |
29 |
Correct |
65 ms |
14932 KB |
Output is correct |
30 |
Correct |
39 ms |
14792 KB |
Output is correct |
31 |
Correct |
36 ms |
14796 KB |
Output is correct |
32 |
Correct |
48 ms |
15116 KB |
Output is correct |
33 |
Correct |
42 ms |
14932 KB |
Output is correct |
34 |
Correct |
54 ms |
14676 KB |
Output is correct |
35 |
Correct |
37 ms |
14932 KB |
Output is correct |
36 |
Correct |
34 ms |
14932 KB |
Output is correct |
37 |
Correct |
66 ms |
14788 KB |
Output is correct |
38 |
Correct |
59 ms |
14932 KB |
Output is correct |
39 |
Correct |
35 ms |
14936 KB |
Output is correct |
40 |
Correct |
36 ms |
14820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4452 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
205 ms |
19112 KB |
Output is correct |
12 |
Correct |
195 ms |
18840 KB |
Output is correct |
13 |
Correct |
187 ms |
18256 KB |
Output is correct |
14 |
Correct |
169 ms |
18240 KB |
Output is correct |
15 |
Correct |
227 ms |
19188 KB |
Output is correct |
16 |
Correct |
196 ms |
18640 KB |
Output is correct |
17 |
Correct |
194 ms |
18252 KB |
Output is correct |
18 |
Correct |
125 ms |
20272 KB |
Output is correct |
19 |
Correct |
189 ms |
17232 KB |
Output is correct |
20 |
Correct |
191 ms |
18772 KB |
Output is correct |
21 |
Correct |
190 ms |
22100 KB |
Output is correct |
22 |
Correct |
240 ms |
22248 KB |
Output is correct |
23 |
Correct |
235 ms |
21332 KB |
Output is correct |
24 |
Correct |
203 ms |
21168 KB |
Output is correct |
25 |
Correct |
195 ms |
23144 KB |
Output is correct |
26 |
Correct |
191 ms |
21588 KB |
Output is correct |
27 |
Correct |
231 ms |
21664 KB |
Output is correct |
28 |
Correct |
120 ms |
24096 KB |
Output is correct |
29 |
Correct |
147 ms |
20436 KB |
Output is correct |
30 |
Correct |
185 ms |
22360 KB |
Output is correct |
31 |
Correct |
214 ms |
22100 KB |
Output is correct |
32 |
Correct |
244 ms |
22100 KB |
Output is correct |
33 |
Correct |
194 ms |
21172 KB |
Output is correct |
34 |
Correct |
199 ms |
21184 KB |
Output is correct |
35 |
Correct |
226 ms |
21616 KB |
Output is correct |
36 |
Correct |
136 ms |
20280 KB |
Output is correct |
37 |
Correct |
219 ms |
22100 KB |
Output is correct |
38 |
Correct |
174 ms |
20200 KB |
Output is correct |
39 |
Correct |
220 ms |
21468 KB |
Output is correct |
40 |
Correct |
199 ms |
21072 KB |
Output is correct |
41 |
Correct |
34 ms |
14932 KB |
Output is correct |
42 |
Correct |
52 ms |
14928 KB |
Output is correct |
43 |
Correct |
39 ms |
14928 KB |
Output is correct |
44 |
Correct |
42 ms |
14932 KB |
Output is correct |
45 |
Correct |
35 ms |
14940 KB |
Output is correct |
46 |
Correct |
47 ms |
14988 KB |
Output is correct |
47 |
Correct |
50 ms |
14928 KB |
Output is correct |
48 |
Correct |
47 ms |
15032 KB |
Output is correct |
49 |
Correct |
45 ms |
14672 KB |
Output is correct |
50 |
Correct |
33 ms |
14928 KB |
Output is correct |
51 |
Correct |
40 ms |
15052 KB |
Output is correct |
52 |
Correct |
44 ms |
14788 KB |
Output is correct |
53 |
Correct |
32 ms |
14928 KB |
Output is correct |
54 |
Correct |
40 ms |
14932 KB |
Output is correct |
55 |
Correct |
58 ms |
14780 KB |
Output is correct |
56 |
Correct |
40 ms |
14780 KB |
Output is correct |
57 |
Correct |
37 ms |
14928 KB |
Output is correct |
58 |
Correct |
31 ms |
14672 KB |
Output is correct |
59 |
Correct |
65 ms |
14932 KB |
Output is correct |
60 |
Correct |
39 ms |
14792 KB |
Output is correct |
61 |
Correct |
36 ms |
14796 KB |
Output is correct |
62 |
Correct |
48 ms |
15116 KB |
Output is correct |
63 |
Correct |
42 ms |
14932 KB |
Output is correct |
64 |
Correct |
54 ms |
14676 KB |
Output is correct |
65 |
Correct |
37 ms |
14932 KB |
Output is correct |
66 |
Correct |
34 ms |
14932 KB |
Output is correct |
67 |
Correct |
66 ms |
14788 KB |
Output is correct |
68 |
Correct |
59 ms |
14932 KB |
Output is correct |
69 |
Correct |
35 ms |
14936 KB |
Output is correct |
70 |
Correct |
36 ms |
14820 KB |
Output is correct |
71 |
Correct |
296 ms |
39192 KB |
Output is correct |
72 |
Correct |
313 ms |
39464 KB |
Output is correct |
73 |
Correct |
360 ms |
37848 KB |
Output is correct |
74 |
Correct |
458 ms |
38348 KB |
Output is correct |
75 |
Correct |
259 ms |
40208 KB |
Output is correct |
76 |
Correct |
412 ms |
38636 KB |
Output is correct |
77 |
Correct |
394 ms |
38492 KB |
Output is correct |
78 |
Correct |
187 ms |
42204 KB |
Output is correct |
79 |
Correct |
325 ms |
36212 KB |
Output is correct |
80 |
Correct |
281 ms |
39508 KB |
Output is correct |
81 |
Correct |
364 ms |
39228 KB |
Output is correct |
82 |
Correct |
487 ms |
38136 KB |
Output is correct |
83 |
Correct |
260 ms |
37460 KB |
Output is correct |
84 |
Correct |
435 ms |
38140 KB |
Output is correct |
85 |
Correct |
407 ms |
38540 KB |
Output is correct |
86 |
Correct |
237 ms |
36296 KB |
Output is correct |
87 |
Correct |
312 ms |
39256 KB |
Output is correct |
88 |
Correct |
284 ms |
36184 KB |
Output is correct |
89 |
Correct |
321 ms |
37972 KB |
Output is correct |
90 |
Correct |
459 ms |
38168 KB |
Output is correct |
91 |
Correct |
293 ms |
37444 KB |
Output is correct |
92 |
Correct |
421 ms |
38484 KB |
Output is correct |
93 |
Correct |
361 ms |
38484 KB |
Output is correct |
94 |
Correct |
230 ms |
36084 KB |
Output is correct |
95 |
Correct |
369 ms |
38228 KB |
Output is correct |
96 |
Correct |
308 ms |
38228 KB |
Output is correct |
97 |
Correct |
340 ms |
38340 KB |
Output is correct |
98 |
Correct |
383 ms |
38224 KB |
Output is correct |
99 |
Correct |
370 ms |
38228 KB |
Output is correct |
100 |
Correct |
354 ms |
38412 KB |
Output is correct |