#include <bits/stdc++.h>
using namespace std;
namespace {}
void solve() {
int h, q;
cin >> h >> q;
uint32_t n = 1 << h;
string a;
cin >> a;
vector dp_sub(n, vector<int>(h + 1)), dp_super(n, vector<int>(h + 1));
for (uint32_t x = 0; x < n; x++) {
dp_sub[x][0] = a[x] - '0';
for (int s = 0; s < h; s++) {
dp_sub[x][s + 1] = dp_sub[x][s];
if (x >> s & 1) {
dp_sub[x][s + 1] += dp_sub[x ^ (1u << s)][s];
}
}
}
for (uint32_t x = n - 1; x + 1 > 0; x--) {
dp_super[x][0] = a[x] - '0';
for (int s = 0; s < h; s++) {
dp_super[x][s + 1] = dp_super[x][s];
if (!(x >> s & 1)) {
dp_super[x][s + 1] += dp_super[x | (1u << s)][s];
}
}
}
for (int i = 0; i < q; i++) {
string t;
cin >> t;
reverse(t.begin(), t.end());
array<uint32_t, 3> v;
v.fill(0);
for (int s = 0; s < h; s++) {
if (t[s] == '0') {
v[0] |= 1u << s;
} else if (t[s] == '1') {
v[1] |= 1u << s;
} else {
v[2] |= 1u << s;
}
}
int ans = 0;
{
auto it = min_element(v.begin(), v.end(), [&](uint32_t x, uint32_t y) -> bool {
return __builtin_popcount(x) < __builtin_popcount(y);
});
int p = distance(v.begin(), it);
for (uint32_t x = v[p];; x = (x - 1) & v[p]) {
if (p == 2) {
ans += a[v[1] | x] - '0';
} else {
int term = p == 1 ? dp_sub[(v[1] ^ x) | v[2]][h] : dp_super[v[1] | x][h];
if (__builtin_parity(x) == 0) {
ans += term;
} else {
ans -= term;
}
}
if (x == 0) {
break;
}
}
}
cout << ans << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
173 ms |
14260 KB |
Output is correct |
12 |
Correct |
189 ms |
14096 KB |
Output is correct |
13 |
Correct |
209 ms |
13036 KB |
Output is correct |
14 |
Correct |
218 ms |
13396 KB |
Output is correct |
15 |
Correct |
205 ms |
14164 KB |
Output is correct |
16 |
Correct |
221 ms |
13400 KB |
Output is correct |
17 |
Correct |
221 ms |
13400 KB |
Output is correct |
18 |
Correct |
144 ms |
15188 KB |
Output is correct |
19 |
Correct |
170 ms |
12124 KB |
Output is correct |
20 |
Correct |
202 ms |
13856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
173 ms |
14260 KB |
Output is correct |
12 |
Correct |
189 ms |
14096 KB |
Output is correct |
13 |
Correct |
209 ms |
13036 KB |
Output is correct |
14 |
Correct |
218 ms |
13396 KB |
Output is correct |
15 |
Correct |
205 ms |
14164 KB |
Output is correct |
16 |
Correct |
221 ms |
13400 KB |
Output is correct |
17 |
Correct |
221 ms |
13400 KB |
Output is correct |
18 |
Correct |
144 ms |
15188 KB |
Output is correct |
19 |
Correct |
170 ms |
12124 KB |
Output is correct |
20 |
Correct |
202 ms |
13856 KB |
Output is correct |
21 |
Correct |
217 ms |
18308 KB |
Output is correct |
22 |
Correct |
220 ms |
18516 KB |
Output is correct |
23 |
Correct |
255 ms |
17380 KB |
Output is correct |
24 |
Correct |
288 ms |
17236 KB |
Output is correct |
25 |
Correct |
228 ms |
19236 KB |
Output is correct |
26 |
Correct |
279 ms |
17748 KB |
Output is correct |
27 |
Correct |
275 ms |
17860 KB |
Output is correct |
28 |
Correct |
162 ms |
20384 KB |
Output is correct |
29 |
Correct |
206 ms |
16320 KB |
Output is correct |
30 |
Correct |
236 ms |
18512 KB |
Output is correct |
31 |
Correct |
258 ms |
18260 KB |
Output is correct |
32 |
Correct |
337 ms |
18340 KB |
Output is correct |
33 |
Correct |
220 ms |
17236 KB |
Output is correct |
34 |
Correct |
299 ms |
17380 KB |
Output is correct |
35 |
Correct |
278 ms |
17660 KB |
Output is correct |
36 |
Correct |
157 ms |
16528 KB |
Output is correct |
37 |
Correct |
244 ms |
18312 KB |
Output is correct |
38 |
Correct |
226 ms |
16424 KB |
Output is correct |
39 |
Correct |
245 ms |
17564 KB |
Output is correct |
40 |
Correct |
296 ms |
17408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Runtime error |
63 ms |
65536 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
173 ms |
14260 KB |
Output is correct |
12 |
Correct |
189 ms |
14096 KB |
Output is correct |
13 |
Correct |
209 ms |
13036 KB |
Output is correct |
14 |
Correct |
218 ms |
13396 KB |
Output is correct |
15 |
Correct |
205 ms |
14164 KB |
Output is correct |
16 |
Correct |
221 ms |
13400 KB |
Output is correct |
17 |
Correct |
221 ms |
13400 KB |
Output is correct |
18 |
Correct |
144 ms |
15188 KB |
Output is correct |
19 |
Correct |
170 ms |
12124 KB |
Output is correct |
20 |
Correct |
202 ms |
13856 KB |
Output is correct |
21 |
Correct |
217 ms |
18308 KB |
Output is correct |
22 |
Correct |
220 ms |
18516 KB |
Output is correct |
23 |
Correct |
255 ms |
17380 KB |
Output is correct |
24 |
Correct |
288 ms |
17236 KB |
Output is correct |
25 |
Correct |
228 ms |
19236 KB |
Output is correct |
26 |
Correct |
279 ms |
17748 KB |
Output is correct |
27 |
Correct |
275 ms |
17860 KB |
Output is correct |
28 |
Correct |
162 ms |
20384 KB |
Output is correct |
29 |
Correct |
206 ms |
16320 KB |
Output is correct |
30 |
Correct |
236 ms |
18512 KB |
Output is correct |
31 |
Correct |
258 ms |
18260 KB |
Output is correct |
32 |
Correct |
337 ms |
18340 KB |
Output is correct |
33 |
Correct |
220 ms |
17236 KB |
Output is correct |
34 |
Correct |
299 ms |
17380 KB |
Output is correct |
35 |
Correct |
278 ms |
17660 KB |
Output is correct |
36 |
Correct |
157 ms |
16528 KB |
Output is correct |
37 |
Correct |
244 ms |
18312 KB |
Output is correct |
38 |
Correct |
226 ms |
16424 KB |
Output is correct |
39 |
Correct |
245 ms |
17564 KB |
Output is correct |
40 |
Correct |
296 ms |
17408 KB |
Output is correct |
41 |
Runtime error |
63 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Halted |
0 ms |
0 KB |
- |