/**
* author: NimaAryan
* created: 2024-08-12 13:06:22
**/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2") // don't trust it
#ifdef LOCAL
#include "algo/debug.h"
#endif
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int L, Q;
cin >> L >> Q;
string S;
cin >> S;
vector<int> sub(1 << L);
for (int i = 0; i < S.size(); ++i) {
sub[i] = (S[i] - '0');
}
for (int i = 0; i < L; ++i) {
for (int u = (1 << L) - 1; u >= 0; --u) {
if (u >> i & 1) {
sub[u] += sub[u ^ (1 << i)];
}
}
}
vector<int> super(1 << L);
for (int i = 0; i < S.size(); ++i) {
super[i] = (S[i] - '0');
}
for (int i = 0; i < L; ++i) {
for (int u = 0; u < (1 << L); ++u) {
if (!(u >> i & 1)) {
super[u] += super[u ^ (1 << i)];
}
}
}
while (Q--) {
string T;
cin >> T;
reverse(T.begin(), T.end());
int cnt0 = count(T.begin(), T.end(), '0');
int cnt1 = count(T.begin(), T.end(), '1');
if (cnt0 <= L / 3) {
int v = 0;
int u = 0;
for (int i = 0; i < L; ++i) {
if (T[i] == '1') {
v ^= (1 << i);
} else if (T[i] == '0') {
u ^= (1 << i);
}
}
int ans = 0;
for (int s = u; s > 0; s = (s - 1) & u) {
ans += (__builtin_popcount(s) % 2 == 0 ? +1 : -1) * super[v | s];
}
ans += super[v];
cout << ans << "\n";
} else if (cnt1 <= L / 3) {
int v = 0;
int u = 0;
for (int i = 0; i < L; ++i) {
if (T[i] == '?') {
v ^= (1 << i);
} else if (T[i] == '1') {
u ^= (1 << i);
}
}
int ans = 0;
for (int s = u; s > 0; s = (s - 1) & u) {
ans += (__builtin_popcount(s) % 2 == 0 ? +1 : -1) * sub[v | (u - s)];
}
ans += sub[v | u];
cout << ans << "\n";
} else {
int u = 0;
int v = 0;
for (int i = 0; i < L; ++i) {
if (T[i] == '?') {
u ^= (1 << i);
} else if (T[i] == '1') {
v ^= (1 << i);
}
}
int ans = 0;
for (int s = u; s > 0; s = (s - 1) & u) {
ans += S[v | s] - '0';
}
ans += S[v] - '0';
cout << ans << "\n";
}
}
return 0;
}
Compilation message
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:29:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for (int i = 0; i < S.size(); ++i) {
| ~~^~~~~~~~~~
snake_escaping.cpp:41:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for (int i = 0; i < S.size(); ++i) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
456 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
456 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
137 ms |
4416 KB |
Output is correct |
12 |
Correct |
125 ms |
3920 KB |
Output is correct |
13 |
Correct |
113 ms |
3152 KB |
Output is correct |
14 |
Correct |
114 ms |
3668 KB |
Output is correct |
15 |
Correct |
124 ms |
4316 KB |
Output is correct |
16 |
Correct |
128 ms |
3412 KB |
Output is correct |
17 |
Correct |
129 ms |
3384 KB |
Output is correct |
18 |
Correct |
92 ms |
5336 KB |
Output is correct |
19 |
Correct |
87 ms |
2336 KB |
Output is correct |
20 |
Correct |
128 ms |
3924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
456 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
137 ms |
4416 KB |
Output is correct |
12 |
Correct |
125 ms |
3920 KB |
Output is correct |
13 |
Correct |
113 ms |
3152 KB |
Output is correct |
14 |
Correct |
114 ms |
3668 KB |
Output is correct |
15 |
Correct |
124 ms |
4316 KB |
Output is correct |
16 |
Correct |
128 ms |
3412 KB |
Output is correct |
17 |
Correct |
129 ms |
3384 KB |
Output is correct |
18 |
Correct |
92 ms |
5336 KB |
Output is correct |
19 |
Correct |
87 ms |
2336 KB |
Output is correct |
20 |
Correct |
128 ms |
3924 KB |
Output is correct |
21 |
Correct |
137 ms |
4436 KB |
Output is correct |
22 |
Correct |
139 ms |
4688 KB |
Output is correct |
23 |
Correct |
135 ms |
3668 KB |
Output is correct |
24 |
Correct |
141 ms |
3472 KB |
Output is correct |
25 |
Correct |
130 ms |
5496 KB |
Output is correct |
26 |
Correct |
154 ms |
3920 KB |
Output is correct |
27 |
Correct |
157 ms |
3920 KB |
Output is correct |
28 |
Correct |
103 ms |
6484 KB |
Output is correct |
29 |
Correct |
113 ms |
2516 KB |
Output is correct |
30 |
Correct |
146 ms |
4688 KB |
Output is correct |
31 |
Correct |
148 ms |
4308 KB |
Output is correct |
32 |
Correct |
145 ms |
4436 KB |
Output is correct |
33 |
Correct |
142 ms |
3380 KB |
Output is correct |
34 |
Correct |
154 ms |
3464 KB |
Output is correct |
35 |
Correct |
165 ms |
3796 KB |
Output is correct |
36 |
Correct |
103 ms |
2372 KB |
Output is correct |
37 |
Correct |
133 ms |
4476 KB |
Output is correct |
38 |
Correct |
122 ms |
2496 KB |
Output is correct |
39 |
Correct |
159 ms |
3664 KB |
Output is correct |
40 |
Correct |
151 ms |
3412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
456 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
34 ms |
10044 KB |
Output is correct |
12 |
Correct |
34 ms |
9972 KB |
Output is correct |
13 |
Correct |
35 ms |
9836 KB |
Output is correct |
14 |
Correct |
38 ms |
9824 KB |
Output is correct |
15 |
Correct |
33 ms |
9972 KB |
Output is correct |
16 |
Correct |
38 ms |
9980 KB |
Output is correct |
17 |
Correct |
37 ms |
9984 KB |
Output is correct |
18 |
Correct |
31 ms |
9984 KB |
Output is correct |
19 |
Correct |
30 ms |
9728 KB |
Output is correct |
20 |
Correct |
35 ms |
9968 KB |
Output is correct |
21 |
Correct |
36 ms |
9968 KB |
Output is correct |
22 |
Correct |
38 ms |
9836 KB |
Output is correct |
23 |
Correct |
34 ms |
9728 KB |
Output is correct |
24 |
Correct |
44 ms |
9824 KB |
Output is correct |
25 |
Correct |
37 ms |
9836 KB |
Output is correct |
26 |
Correct |
30 ms |
9828 KB |
Output is correct |
27 |
Correct |
42 ms |
9984 KB |
Output is correct |
28 |
Correct |
31 ms |
9724 KB |
Output is correct |
29 |
Correct |
36 ms |
9968 KB |
Output is correct |
30 |
Correct |
37 ms |
9968 KB |
Output is correct |
31 |
Correct |
33 ms |
9968 KB |
Output is correct |
32 |
Correct |
38 ms |
9828 KB |
Output is correct |
33 |
Correct |
37 ms |
9976 KB |
Output is correct |
34 |
Correct |
30 ms |
9728 KB |
Output is correct |
35 |
Correct |
36 ms |
9836 KB |
Output is correct |
36 |
Correct |
36 ms |
9972 KB |
Output is correct |
37 |
Correct |
35 ms |
9972 KB |
Output is correct |
38 |
Correct |
44 ms |
9972 KB |
Output is correct |
39 |
Correct |
37 ms |
9984 KB |
Output is correct |
40 |
Correct |
36 ms |
9924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
456 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
137 ms |
4416 KB |
Output is correct |
12 |
Correct |
125 ms |
3920 KB |
Output is correct |
13 |
Correct |
113 ms |
3152 KB |
Output is correct |
14 |
Correct |
114 ms |
3668 KB |
Output is correct |
15 |
Correct |
124 ms |
4316 KB |
Output is correct |
16 |
Correct |
128 ms |
3412 KB |
Output is correct |
17 |
Correct |
129 ms |
3384 KB |
Output is correct |
18 |
Correct |
92 ms |
5336 KB |
Output is correct |
19 |
Correct |
87 ms |
2336 KB |
Output is correct |
20 |
Correct |
128 ms |
3924 KB |
Output is correct |
21 |
Correct |
137 ms |
4436 KB |
Output is correct |
22 |
Correct |
139 ms |
4688 KB |
Output is correct |
23 |
Correct |
135 ms |
3668 KB |
Output is correct |
24 |
Correct |
141 ms |
3472 KB |
Output is correct |
25 |
Correct |
130 ms |
5496 KB |
Output is correct |
26 |
Correct |
154 ms |
3920 KB |
Output is correct |
27 |
Correct |
157 ms |
3920 KB |
Output is correct |
28 |
Correct |
103 ms |
6484 KB |
Output is correct |
29 |
Correct |
113 ms |
2516 KB |
Output is correct |
30 |
Correct |
146 ms |
4688 KB |
Output is correct |
31 |
Correct |
148 ms |
4308 KB |
Output is correct |
32 |
Correct |
145 ms |
4436 KB |
Output is correct |
33 |
Correct |
142 ms |
3380 KB |
Output is correct |
34 |
Correct |
154 ms |
3464 KB |
Output is correct |
35 |
Correct |
165 ms |
3796 KB |
Output is correct |
36 |
Correct |
103 ms |
2372 KB |
Output is correct |
37 |
Correct |
133 ms |
4476 KB |
Output is correct |
38 |
Correct |
122 ms |
2496 KB |
Output is correct |
39 |
Correct |
159 ms |
3664 KB |
Output is correct |
40 |
Correct |
151 ms |
3412 KB |
Output is correct |
41 |
Correct |
34 ms |
10044 KB |
Output is correct |
42 |
Correct |
34 ms |
9972 KB |
Output is correct |
43 |
Correct |
35 ms |
9836 KB |
Output is correct |
44 |
Correct |
38 ms |
9824 KB |
Output is correct |
45 |
Correct |
33 ms |
9972 KB |
Output is correct |
46 |
Correct |
38 ms |
9980 KB |
Output is correct |
47 |
Correct |
37 ms |
9984 KB |
Output is correct |
48 |
Correct |
31 ms |
9984 KB |
Output is correct |
49 |
Correct |
30 ms |
9728 KB |
Output is correct |
50 |
Correct |
35 ms |
9968 KB |
Output is correct |
51 |
Correct |
36 ms |
9968 KB |
Output is correct |
52 |
Correct |
38 ms |
9836 KB |
Output is correct |
53 |
Correct |
34 ms |
9728 KB |
Output is correct |
54 |
Correct |
44 ms |
9824 KB |
Output is correct |
55 |
Correct |
37 ms |
9836 KB |
Output is correct |
56 |
Correct |
30 ms |
9828 KB |
Output is correct |
57 |
Correct |
42 ms |
9984 KB |
Output is correct |
58 |
Correct |
31 ms |
9724 KB |
Output is correct |
59 |
Correct |
36 ms |
9968 KB |
Output is correct |
60 |
Correct |
37 ms |
9968 KB |
Output is correct |
61 |
Correct |
33 ms |
9968 KB |
Output is correct |
62 |
Correct |
38 ms |
9828 KB |
Output is correct |
63 |
Correct |
37 ms |
9976 KB |
Output is correct |
64 |
Correct |
30 ms |
9728 KB |
Output is correct |
65 |
Correct |
36 ms |
9836 KB |
Output is correct |
66 |
Correct |
36 ms |
9972 KB |
Output is correct |
67 |
Correct |
35 ms |
9972 KB |
Output is correct |
68 |
Correct |
44 ms |
9972 KB |
Output is correct |
69 |
Correct |
37 ms |
9984 KB |
Output is correct |
70 |
Correct |
36 ms |
9924 KB |
Output is correct |
71 |
Correct |
233 ms |
14796 KB |
Output is correct |
72 |
Correct |
231 ms |
14756 KB |
Output is correct |
73 |
Correct |
251 ms |
13344 KB |
Output is correct |
74 |
Correct |
318 ms |
18136 KB |
Output is correct |
75 |
Correct |
230 ms |
37368 KB |
Output is correct |
76 |
Correct |
328 ms |
35572 KB |
Output is correct |
77 |
Correct |
300 ms |
35412 KB |
Output is correct |
78 |
Correct |
171 ms |
39412 KB |
Output is correct |
79 |
Correct |
161 ms |
33264 KB |
Output is correct |
80 |
Correct |
268 ms |
36316 KB |
Output is correct |
81 |
Correct |
257 ms |
36336 KB |
Output is correct |
82 |
Correct |
305 ms |
35316 KB |
Output is correct |
83 |
Correct |
242 ms |
34548 KB |
Output is correct |
84 |
Correct |
301 ms |
35248 KB |
Output is correct |
85 |
Correct |
295 ms |
35824 KB |
Output is correct |
86 |
Correct |
156 ms |
33268 KB |
Output is correct |
87 |
Correct |
224 ms |
36340 KB |
Output is correct |
88 |
Correct |
169 ms |
33268 KB |
Output is correct |
89 |
Correct |
263 ms |
34860 KB |
Output is correct |
90 |
Correct |
303 ms |
35412 KB |
Output is correct |
91 |
Correct |
212 ms |
34584 KB |
Output is correct |
92 |
Correct |
307 ms |
35568 KB |
Output is correct |
93 |
Correct |
295 ms |
36080 KB |
Output is correct |
94 |
Correct |
158 ms |
33356 KB |
Output is correct |
95 |
Correct |
271 ms |
35276 KB |
Output is correct |
96 |
Correct |
300 ms |
35316 KB |
Output is correct |
97 |
Correct |
267 ms |
35364 KB |
Output is correct |
98 |
Correct |
261 ms |
35608 KB |
Output is correct |
99 |
Correct |
268 ms |
35312 KB |
Output is correct |
100 |
Correct |
351 ms |
35316 KB |
Output is correct |