#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<int> b(n);
for (uint32_t x = 0; x < n; x++) {
b[x] = a[x] - '0';
}
vector dp_sub = b, dp_super = b;
for (int s = 0; s < h; s++) {
for (uint32_t x = n; x + 1 > 0; x--) {
if (x >> s & 1) {
dp_sub[x] += dp_sub[x ^ (1u << s)];
}
}
for (uint32_t x = 0; x < n; x++) {
if (!(x >> s & 1)) {
dp_super[x] += dp_super[x | (1u << 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 += b[v[1] | x];
} else {
int term = p == 1 ? dp_sub[(v[1] ^ x) | v[2]] : dp_super[v[1] | x];
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 |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
484 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
484 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
190 ms |
14852 KB |
Output is correct |
12 |
Correct |
182 ms |
14420 KB |
Output is correct |
13 |
Correct |
219 ms |
13904 KB |
Output is correct |
14 |
Correct |
214 ms |
13864 KB |
Output is correct |
15 |
Correct |
197 ms |
14908 KB |
Output is correct |
16 |
Correct |
218 ms |
13908 KB |
Output is correct |
17 |
Correct |
221 ms |
13904 KB |
Output is correct |
18 |
Correct |
141 ms |
15956 KB |
Output is correct |
19 |
Correct |
196 ms |
12880 KB |
Output is correct |
20 |
Correct |
186 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
484 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
190 ms |
14852 KB |
Output is correct |
12 |
Correct |
182 ms |
14420 KB |
Output is correct |
13 |
Correct |
219 ms |
13904 KB |
Output is correct |
14 |
Correct |
214 ms |
13864 KB |
Output is correct |
15 |
Correct |
197 ms |
14908 KB |
Output is correct |
16 |
Correct |
218 ms |
13908 KB |
Output is correct |
17 |
Correct |
221 ms |
13904 KB |
Output is correct |
18 |
Correct |
141 ms |
15956 KB |
Output is correct |
19 |
Correct |
196 ms |
12880 KB |
Output is correct |
20 |
Correct |
186 ms |
14420 KB |
Output is correct |
21 |
Correct |
200 ms |
17564 KB |
Output is correct |
22 |
Correct |
212 ms |
17684 KB |
Output is correct |
23 |
Correct |
244 ms |
16820 KB |
Output is correct |
24 |
Correct |
262 ms |
16724 KB |
Output is correct |
25 |
Correct |
215 ms |
18512 KB |
Output is correct |
26 |
Correct |
259 ms |
17160 KB |
Output is correct |
27 |
Correct |
260 ms |
17104 KB |
Output is correct |
28 |
Correct |
155 ms |
19688 KB |
Output is correct |
29 |
Correct |
206 ms |
15552 KB |
Output is correct |
30 |
Correct |
220 ms |
17732 KB |
Output is correct |
31 |
Correct |
265 ms |
17488 KB |
Output is correct |
32 |
Correct |
259 ms |
17748 KB |
Output is correct |
33 |
Correct |
216 ms |
16548 KB |
Output is correct |
34 |
Correct |
262 ms |
16700 KB |
Output is correct |
35 |
Correct |
260 ms |
16976 KB |
Output is correct |
36 |
Correct |
160 ms |
15664 KB |
Output is correct |
37 |
Correct |
192 ms |
17492 KB |
Output is correct |
38 |
Correct |
218 ms |
15520 KB |
Output is correct |
39 |
Correct |
241 ms |
16916 KB |
Output is correct |
40 |
Correct |
280 ms |
16640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
484 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
52 ms |
15980 KB |
Output is correct |
12 |
Correct |
49 ms |
16172 KB |
Output is correct |
13 |
Correct |
53 ms |
16108 KB |
Output is correct |
14 |
Correct |
61 ms |
15964 KB |
Output is correct |
15 |
Correct |
60 ms |
16036 KB |
Output is correct |
16 |
Correct |
62 ms |
16168 KB |
Output is correct |
17 |
Correct |
68 ms |
15956 KB |
Output is correct |
18 |
Correct |
47 ms |
16116 KB |
Output is correct |
19 |
Correct |
56 ms |
15956 KB |
Output is correct |
20 |
Correct |
49 ms |
16116 KB |
Output is correct |
21 |
Correct |
51 ms |
16112 KB |
Output is correct |
22 |
Correct |
60 ms |
16112 KB |
Output is correct |
23 |
Correct |
50 ms |
15952 KB |
Output is correct |
24 |
Correct |
70 ms |
15956 KB |
Output is correct |
25 |
Correct |
55 ms |
16064 KB |
Output is correct |
26 |
Correct |
45 ms |
15948 KB |
Output is correct |
27 |
Correct |
49 ms |
16112 KB |
Output is correct |
28 |
Correct |
62 ms |
15952 KB |
Output is correct |
29 |
Correct |
52 ms |
16112 KB |
Output is correct |
30 |
Correct |
57 ms |
15972 KB |
Output is correct |
31 |
Correct |
50 ms |
16116 KB |
Output is correct |
32 |
Correct |
57 ms |
16112 KB |
Output is correct |
33 |
Correct |
77 ms |
16328 KB |
Output is correct |
34 |
Correct |
45 ms |
16028 KB |
Output is correct |
35 |
Correct |
67 ms |
16116 KB |
Output is correct |
36 |
Correct |
55 ms |
16116 KB |
Output is correct |
37 |
Correct |
55 ms |
16116 KB |
Output is correct |
38 |
Correct |
55 ms |
16116 KB |
Output is correct |
39 |
Correct |
55 ms |
16080 KB |
Output is correct |
40 |
Correct |
54 ms |
16116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
484 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
190 ms |
14852 KB |
Output is correct |
12 |
Correct |
182 ms |
14420 KB |
Output is correct |
13 |
Correct |
219 ms |
13904 KB |
Output is correct |
14 |
Correct |
214 ms |
13864 KB |
Output is correct |
15 |
Correct |
197 ms |
14908 KB |
Output is correct |
16 |
Correct |
218 ms |
13908 KB |
Output is correct |
17 |
Correct |
221 ms |
13904 KB |
Output is correct |
18 |
Correct |
141 ms |
15956 KB |
Output is correct |
19 |
Correct |
196 ms |
12880 KB |
Output is correct |
20 |
Correct |
186 ms |
14420 KB |
Output is correct |
21 |
Correct |
200 ms |
17564 KB |
Output is correct |
22 |
Correct |
212 ms |
17684 KB |
Output is correct |
23 |
Correct |
244 ms |
16820 KB |
Output is correct |
24 |
Correct |
262 ms |
16724 KB |
Output is correct |
25 |
Correct |
215 ms |
18512 KB |
Output is correct |
26 |
Correct |
259 ms |
17160 KB |
Output is correct |
27 |
Correct |
260 ms |
17104 KB |
Output is correct |
28 |
Correct |
155 ms |
19688 KB |
Output is correct |
29 |
Correct |
206 ms |
15552 KB |
Output is correct |
30 |
Correct |
220 ms |
17732 KB |
Output is correct |
31 |
Correct |
265 ms |
17488 KB |
Output is correct |
32 |
Correct |
259 ms |
17748 KB |
Output is correct |
33 |
Correct |
216 ms |
16548 KB |
Output is correct |
34 |
Correct |
262 ms |
16700 KB |
Output is correct |
35 |
Correct |
260 ms |
16976 KB |
Output is correct |
36 |
Correct |
160 ms |
15664 KB |
Output is correct |
37 |
Correct |
192 ms |
17492 KB |
Output is correct |
38 |
Correct |
218 ms |
15520 KB |
Output is correct |
39 |
Correct |
241 ms |
16916 KB |
Output is correct |
40 |
Correct |
280 ms |
16640 KB |
Output is correct |
41 |
Correct |
52 ms |
15980 KB |
Output is correct |
42 |
Correct |
49 ms |
16172 KB |
Output is correct |
43 |
Correct |
53 ms |
16108 KB |
Output is correct |
44 |
Correct |
61 ms |
15964 KB |
Output is correct |
45 |
Correct |
60 ms |
16036 KB |
Output is correct |
46 |
Correct |
62 ms |
16168 KB |
Output is correct |
47 |
Correct |
68 ms |
15956 KB |
Output is correct |
48 |
Correct |
47 ms |
16116 KB |
Output is correct |
49 |
Correct |
56 ms |
15956 KB |
Output is correct |
50 |
Correct |
49 ms |
16116 KB |
Output is correct |
51 |
Correct |
51 ms |
16112 KB |
Output is correct |
52 |
Correct |
60 ms |
16112 KB |
Output is correct |
53 |
Correct |
50 ms |
15952 KB |
Output is correct |
54 |
Correct |
70 ms |
15956 KB |
Output is correct |
55 |
Correct |
55 ms |
16064 KB |
Output is correct |
56 |
Correct |
45 ms |
15948 KB |
Output is correct |
57 |
Correct |
49 ms |
16112 KB |
Output is correct |
58 |
Correct |
62 ms |
15952 KB |
Output is correct |
59 |
Correct |
52 ms |
16112 KB |
Output is correct |
60 |
Correct |
57 ms |
15972 KB |
Output is correct |
61 |
Correct |
50 ms |
16116 KB |
Output is correct |
62 |
Correct |
57 ms |
16112 KB |
Output is correct |
63 |
Correct |
77 ms |
16328 KB |
Output is correct |
64 |
Correct |
45 ms |
16028 KB |
Output is correct |
65 |
Correct |
67 ms |
16116 KB |
Output is correct |
66 |
Correct |
55 ms |
16116 KB |
Output is correct |
67 |
Correct |
55 ms |
16116 KB |
Output is correct |
68 |
Correct |
55 ms |
16116 KB |
Output is correct |
69 |
Correct |
55 ms |
16080 KB |
Output is correct |
70 |
Correct |
54 ms |
16116 KB |
Output is correct |
71 |
Correct |
341 ms |
40676 KB |
Output is correct |
72 |
Correct |
366 ms |
40536 KB |
Output is correct |
73 |
Correct |
413 ms |
38888 KB |
Output is correct |
74 |
Correct |
533 ms |
39296 KB |
Output is correct |
75 |
Correct |
335 ms |
41456 KB |
Output is correct |
76 |
Correct |
544 ms |
39724 KB |
Output is correct |
77 |
Correct |
522 ms |
39840 KB |
Output is correct |
78 |
Correct |
254 ms |
43460 KB |
Output is correct |
79 |
Correct |
321 ms |
37208 KB |
Output is correct |
80 |
Correct |
342 ms |
40380 KB |
Output is correct |
81 |
Correct |
434 ms |
40480 KB |
Output is correct |
82 |
Correct |
494 ms |
39592 KB |
Output is correct |
83 |
Correct |
326 ms |
38644 KB |
Output is correct |
84 |
Correct |
528 ms |
39412 KB |
Output is correct |
85 |
Correct |
491 ms |
39592 KB |
Output is correct |
86 |
Correct |
252 ms |
37364 KB |
Output is correct |
87 |
Correct |
331 ms |
40276 KB |
Output is correct |
88 |
Correct |
356 ms |
37212 KB |
Output is correct |
89 |
Correct |
381 ms |
38916 KB |
Output is correct |
90 |
Correct |
422 ms |
39692 KB |
Output is correct |
91 |
Correct |
344 ms |
38640 KB |
Output is correct |
92 |
Correct |
551 ms |
39664 KB |
Output is correct |
93 |
Correct |
516 ms |
39508 KB |
Output is correct |
94 |
Correct |
232 ms |
37364 KB |
Output is correct |
95 |
Correct |
438 ms |
39316 KB |
Output is correct |
96 |
Correct |
460 ms |
39468 KB |
Output is correct |
97 |
Correct |
442 ms |
39572 KB |
Output is correct |
98 |
Correct |
449 ms |
39524 KB |
Output is correct |
99 |
Correct |
459 ms |
39412 KB |
Output is correct |
100 |
Correct |
447 ms |
39424 KB |
Output is correct |