#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <iomanip>
#include <cassert>
#include <numeric>
#include <deque>
#include <functional>
#include <set>
#include <queue>
#include <unordered_map>
#include <map>
#include <bitset>
#include <iomanip>
#include <complex>
using namespace std;
const int maxn = 1 << 20;
int val[maxn]; // just the value
int dir[maxn]; // direct sum of subsets
int rev[maxn]; // reversed sum of subsets (i -> ~i transform)
int popcnt[maxn];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
for (int i = 1; i < maxn; ++i) {
popcnt[i] = popcnt[i / 2] + (i & 1);
}
int l, q;
cin >> l >> q;
const int n = 1 << l;
string s;
cin >> s;
for (int i = 0; i < n; ++i) {
val[i] = dir[i] = rev[((1 << l) - 1) ^ i] = s[i] - '0';
}
for (int bit = 0; bit < l; ++bit) {
for (int mask = 0; mask < (1 << l); ++mask) {
if ((1 << bit) & mask) {
dir[mask] += dir[mask ^ (1 << bit)];
rev[mask] += rev[mask ^ (1 << bit)];
}
}
}
while (q--) {
int mask0 = 0, mask1 = 0, mask2 = 0;
string t;
cin >> t;
reverse(t.begin(), t.end());
for (int i = 0; i < l; ++i) {
if (t[i] == '0') mask0 |= (1 << i);
else if (t[i] == '1') mask1 |= (1 << i);
else mask2 |= (1 << i);
}
int ans = 0;
if (popcnt[mask0] <= 6) {
for (int mask = mask0; mask; mask = mask0 & (mask - 1)) ans += rev[mask | mask2] * (popcnt[mask ^ mask0] & 1 ? -1 : 1);
ans += rev[mask2] * (popcnt[mask0] & 1 ? -1 : 1);
} else if (popcnt[mask1] <= 6) {
for (int mask = mask1; mask; mask = mask1 & (mask - 1)) ans += dir[mask | mask2] * (popcnt[mask ^ mask1] & 1 ? -1 : 1);
ans += dir[mask2] * (popcnt[mask1] & 1 ? -1 : 1);
} else {
for (int mask = mask2; mask; mask = mask2 & (mask - 1)) ans += val[mask1 | mask];
ans += val[mask1];
}
cout << ans << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4436 KB |
Output is correct |
2 |
Correct |
3 ms |
4436 KB |
Output is correct |
3 |
Correct |
3 ms |
4436 KB |
Output is correct |
4 |
Correct |
3 ms |
4436 KB |
Output is correct |
5 |
Correct |
4 ms |
4440 KB |
Output is correct |
6 |
Correct |
6 ms |
4436 KB |
Output is correct |
7 |
Correct |
5 ms |
4436 KB |
Output is correct |
8 |
Correct |
4 ms |
4436 KB |
Output is correct |
9 |
Correct |
5 ms |
4436 KB |
Output is correct |
10 |
Correct |
4 ms |
4440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4436 KB |
Output is correct |
2 |
Correct |
3 ms |
4436 KB |
Output is correct |
3 |
Correct |
3 ms |
4436 KB |
Output is correct |
4 |
Correct |
3 ms |
4436 KB |
Output is correct |
5 |
Correct |
4 ms |
4440 KB |
Output is correct |
6 |
Correct |
6 ms |
4436 KB |
Output is correct |
7 |
Correct |
5 ms |
4436 KB |
Output is correct |
8 |
Correct |
4 ms |
4436 KB |
Output is correct |
9 |
Correct |
5 ms |
4436 KB |
Output is correct |
10 |
Correct |
4 ms |
4440 KB |
Output is correct |
11 |
Correct |
252 ms |
19136 KB |
Output is correct |
12 |
Correct |
279 ms |
18804 KB |
Output is correct |
13 |
Correct |
335 ms |
18048 KB |
Output is correct |
14 |
Correct |
265 ms |
18100 KB |
Output is correct |
15 |
Correct |
300 ms |
19116 KB |
Output is correct |
16 |
Correct |
287 ms |
18324 KB |
Output is correct |
17 |
Correct |
329 ms |
18316 KB |
Output is correct |
18 |
Correct |
232 ms |
20112 KB |
Output is correct |
19 |
Correct |
269 ms |
17168 KB |
Output is correct |
20 |
Correct |
243 ms |
18868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4436 KB |
Output is correct |
2 |
Correct |
3 ms |
4436 KB |
Output is correct |
3 |
Correct |
3 ms |
4436 KB |
Output is correct |
4 |
Correct |
3 ms |
4436 KB |
Output is correct |
5 |
Correct |
4 ms |
4440 KB |
Output is correct |
6 |
Correct |
6 ms |
4436 KB |
Output is correct |
7 |
Correct |
5 ms |
4436 KB |
Output is correct |
8 |
Correct |
4 ms |
4436 KB |
Output is correct |
9 |
Correct |
5 ms |
4436 KB |
Output is correct |
10 |
Correct |
4 ms |
4440 KB |
Output is correct |
11 |
Correct |
252 ms |
19136 KB |
Output is correct |
12 |
Correct |
279 ms |
18804 KB |
Output is correct |
13 |
Correct |
335 ms |
18048 KB |
Output is correct |
14 |
Correct |
265 ms |
18100 KB |
Output is correct |
15 |
Correct |
300 ms |
19116 KB |
Output is correct |
16 |
Correct |
287 ms |
18324 KB |
Output is correct |
17 |
Correct |
329 ms |
18316 KB |
Output is correct |
18 |
Correct |
232 ms |
20112 KB |
Output is correct |
19 |
Correct |
269 ms |
17168 KB |
Output is correct |
20 |
Correct |
243 ms |
18868 KB |
Output is correct |
21 |
Correct |
255 ms |
22196 KB |
Output is correct |
22 |
Correct |
294 ms |
22360 KB |
Output is correct |
23 |
Correct |
455 ms |
21440 KB |
Output is correct |
24 |
Correct |
300 ms |
21256 KB |
Output is correct |
25 |
Correct |
250 ms |
23272 KB |
Output is correct |
26 |
Correct |
309 ms |
21660 KB |
Output is correct |
27 |
Correct |
317 ms |
21636 KB |
Output is correct |
28 |
Correct |
171 ms |
24268 KB |
Output is correct |
29 |
Correct |
346 ms |
20364 KB |
Output is correct |
30 |
Correct |
230 ms |
22416 KB |
Output is correct |
31 |
Correct |
294 ms |
22224 KB |
Output is correct |
32 |
Correct |
269 ms |
22216 KB |
Output is correct |
33 |
Correct |
255 ms |
21136 KB |
Output is correct |
34 |
Correct |
288 ms |
21240 KB |
Output is correct |
35 |
Correct |
332 ms |
21664 KB |
Output is correct |
36 |
Correct |
200 ms |
20200 KB |
Output is correct |
37 |
Correct |
215 ms |
22268 KB |
Output is correct |
38 |
Correct |
298 ms |
20348 KB |
Output is correct |
39 |
Correct |
258 ms |
21364 KB |
Output is correct |
40 |
Correct |
285 ms |
21240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4436 KB |
Output is correct |
2 |
Correct |
3 ms |
4436 KB |
Output is correct |
3 |
Correct |
3 ms |
4436 KB |
Output is correct |
4 |
Correct |
3 ms |
4436 KB |
Output is correct |
5 |
Correct |
4 ms |
4440 KB |
Output is correct |
6 |
Correct |
6 ms |
4436 KB |
Output is correct |
7 |
Correct |
5 ms |
4436 KB |
Output is correct |
8 |
Correct |
4 ms |
4436 KB |
Output is correct |
9 |
Correct |
5 ms |
4436 KB |
Output is correct |
10 |
Correct |
4 ms |
4440 KB |
Output is correct |
11 |
Correct |
48 ms |
20184 KB |
Output is correct |
12 |
Correct |
58 ms |
20092 KB |
Output is correct |
13 |
Correct |
67 ms |
20068 KB |
Output is correct |
14 |
Correct |
63 ms |
20076 KB |
Output is correct |
15 |
Correct |
52 ms |
20180 KB |
Output is correct |
16 |
Correct |
81 ms |
20040 KB |
Output is correct |
17 |
Correct |
66 ms |
20176 KB |
Output is correct |
18 |
Correct |
42 ms |
20332 KB |
Output is correct |
19 |
Correct |
48 ms |
20016 KB |
Output is correct |
20 |
Correct |
56 ms |
20144 KB |
Output is correct |
21 |
Correct |
54 ms |
20096 KB |
Output is correct |
22 |
Correct |
65 ms |
20044 KB |
Output is correct |
23 |
Correct |
48 ms |
20060 KB |
Output is correct |
24 |
Correct |
66 ms |
20148 KB |
Output is correct |
25 |
Correct |
68 ms |
20044 KB |
Output is correct |
26 |
Correct |
54 ms |
19988 KB |
Output is correct |
27 |
Correct |
56 ms |
20140 KB |
Output is correct |
28 |
Correct |
51 ms |
19948 KB |
Output is correct |
29 |
Correct |
57 ms |
20016 KB |
Output is correct |
30 |
Correct |
69 ms |
20036 KB |
Output is correct |
31 |
Correct |
73 ms |
20044 KB |
Output is correct |
32 |
Correct |
67 ms |
20128 KB |
Output is correct |
33 |
Correct |
67 ms |
20136 KB |
Output is correct |
34 |
Correct |
47 ms |
19916 KB |
Output is correct |
35 |
Correct |
85 ms |
20128 KB |
Output is correct |
36 |
Correct |
61 ms |
20044 KB |
Output is correct |
37 |
Correct |
64 ms |
20064 KB |
Output is correct |
38 |
Correct |
59 ms |
20120 KB |
Output is correct |
39 |
Correct |
63 ms |
20048 KB |
Output is correct |
40 |
Correct |
76 ms |
20088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4436 KB |
Output is correct |
2 |
Correct |
3 ms |
4436 KB |
Output is correct |
3 |
Correct |
3 ms |
4436 KB |
Output is correct |
4 |
Correct |
3 ms |
4436 KB |
Output is correct |
5 |
Correct |
4 ms |
4440 KB |
Output is correct |
6 |
Correct |
6 ms |
4436 KB |
Output is correct |
7 |
Correct |
5 ms |
4436 KB |
Output is correct |
8 |
Correct |
4 ms |
4436 KB |
Output is correct |
9 |
Correct |
5 ms |
4436 KB |
Output is correct |
10 |
Correct |
4 ms |
4440 KB |
Output is correct |
11 |
Correct |
252 ms |
19136 KB |
Output is correct |
12 |
Correct |
279 ms |
18804 KB |
Output is correct |
13 |
Correct |
335 ms |
18048 KB |
Output is correct |
14 |
Correct |
265 ms |
18100 KB |
Output is correct |
15 |
Correct |
300 ms |
19116 KB |
Output is correct |
16 |
Correct |
287 ms |
18324 KB |
Output is correct |
17 |
Correct |
329 ms |
18316 KB |
Output is correct |
18 |
Correct |
232 ms |
20112 KB |
Output is correct |
19 |
Correct |
269 ms |
17168 KB |
Output is correct |
20 |
Correct |
243 ms |
18868 KB |
Output is correct |
21 |
Correct |
255 ms |
22196 KB |
Output is correct |
22 |
Correct |
294 ms |
22360 KB |
Output is correct |
23 |
Correct |
455 ms |
21440 KB |
Output is correct |
24 |
Correct |
300 ms |
21256 KB |
Output is correct |
25 |
Correct |
250 ms |
23272 KB |
Output is correct |
26 |
Correct |
309 ms |
21660 KB |
Output is correct |
27 |
Correct |
317 ms |
21636 KB |
Output is correct |
28 |
Correct |
171 ms |
24268 KB |
Output is correct |
29 |
Correct |
346 ms |
20364 KB |
Output is correct |
30 |
Correct |
230 ms |
22416 KB |
Output is correct |
31 |
Correct |
294 ms |
22224 KB |
Output is correct |
32 |
Correct |
269 ms |
22216 KB |
Output is correct |
33 |
Correct |
255 ms |
21136 KB |
Output is correct |
34 |
Correct |
288 ms |
21240 KB |
Output is correct |
35 |
Correct |
332 ms |
21664 KB |
Output is correct |
36 |
Correct |
200 ms |
20200 KB |
Output is correct |
37 |
Correct |
215 ms |
22268 KB |
Output is correct |
38 |
Correct |
298 ms |
20348 KB |
Output is correct |
39 |
Correct |
258 ms |
21364 KB |
Output is correct |
40 |
Correct |
285 ms |
21240 KB |
Output is correct |
41 |
Correct |
48 ms |
20184 KB |
Output is correct |
42 |
Correct |
58 ms |
20092 KB |
Output is correct |
43 |
Correct |
67 ms |
20068 KB |
Output is correct |
44 |
Correct |
63 ms |
20076 KB |
Output is correct |
45 |
Correct |
52 ms |
20180 KB |
Output is correct |
46 |
Correct |
81 ms |
20040 KB |
Output is correct |
47 |
Correct |
66 ms |
20176 KB |
Output is correct |
48 |
Correct |
42 ms |
20332 KB |
Output is correct |
49 |
Correct |
48 ms |
20016 KB |
Output is correct |
50 |
Correct |
56 ms |
20144 KB |
Output is correct |
51 |
Correct |
54 ms |
20096 KB |
Output is correct |
52 |
Correct |
65 ms |
20044 KB |
Output is correct |
53 |
Correct |
48 ms |
20060 KB |
Output is correct |
54 |
Correct |
66 ms |
20148 KB |
Output is correct |
55 |
Correct |
68 ms |
20044 KB |
Output is correct |
56 |
Correct |
54 ms |
19988 KB |
Output is correct |
57 |
Correct |
56 ms |
20140 KB |
Output is correct |
58 |
Correct |
51 ms |
19948 KB |
Output is correct |
59 |
Correct |
57 ms |
20016 KB |
Output is correct |
60 |
Correct |
69 ms |
20036 KB |
Output is correct |
61 |
Correct |
73 ms |
20044 KB |
Output is correct |
62 |
Correct |
67 ms |
20128 KB |
Output is correct |
63 |
Correct |
67 ms |
20136 KB |
Output is correct |
64 |
Correct |
47 ms |
19916 KB |
Output is correct |
65 |
Correct |
85 ms |
20128 KB |
Output is correct |
66 |
Correct |
61 ms |
20044 KB |
Output is correct |
67 |
Correct |
64 ms |
20064 KB |
Output is correct |
68 |
Correct |
59 ms |
20120 KB |
Output is correct |
69 |
Correct |
63 ms |
20048 KB |
Output is correct |
70 |
Correct |
76 ms |
20088 KB |
Output is correct |
71 |
Correct |
357 ms |
44440 KB |
Output is correct |
72 |
Correct |
439 ms |
44632 KB |
Output is correct |
73 |
Correct |
601 ms |
43128 KB |
Output is correct |
74 |
Correct |
721 ms |
43452 KB |
Output is correct |
75 |
Correct |
474 ms |
45372 KB |
Output is correct |
76 |
Correct |
726 ms |
43768 KB |
Output is correct |
77 |
Correct |
800 ms |
43568 KB |
Output is correct |
78 |
Correct |
300 ms |
47464 KB |
Output is correct |
79 |
Correct |
477 ms |
41412 KB |
Output is correct |
80 |
Correct |
430 ms |
44512 KB |
Output is correct |
81 |
Correct |
635 ms |
44440 KB |
Output is correct |
82 |
Correct |
876 ms |
43412 KB |
Output is correct |
83 |
Correct |
434 ms |
42636 KB |
Output is correct |
84 |
Correct |
940 ms |
43352 KB |
Output is correct |
85 |
Correct |
764 ms |
43596 KB |
Output is correct |
86 |
Correct |
297 ms |
41360 KB |
Output is correct |
87 |
Correct |
391 ms |
44408 KB |
Output is correct |
88 |
Correct |
518 ms |
41460 KB |
Output is correct |
89 |
Correct |
566 ms |
43124 KB |
Output is correct |
90 |
Correct |
579 ms |
43472 KB |
Output is correct |
91 |
Correct |
478 ms |
42584 KB |
Output is correct |
92 |
Correct |
901 ms |
43724 KB |
Output is correct |
93 |
Correct |
715 ms |
43580 KB |
Output is correct |
94 |
Correct |
252 ms |
41340 KB |
Output is correct |
95 |
Correct |
627 ms |
43444 KB |
Output is correct |
96 |
Correct |
668 ms |
43416 KB |
Output is correct |
97 |
Correct |
657 ms |
43492 KB |
Output is correct |
98 |
Correct |
625 ms |
43600 KB |
Output is correct |
99 |
Correct |
637 ms |
43532 KB |
Output is correct |
100 |
Correct |
626 ms |
43572 KB |
Output is correct |