#include <bits/stdc++.h>
using namespace std;
#define ll long long
int dp[2][1 << 20], dp2[2][1 << 20];
string s;
int a[1 << 20];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, q;
cin >> n >> q;
int m = n;
n = 1 << n;
for (int i = 0; i < n; i++) {
char c;
cin >> c;
a[i] = c - '0';
}
for (int i = 0; i < n; i++) dp[m & 1][i] = a[i], dp2[m & 1][i] = a[i];
for (int i = m - 1; i >= 0; i--) {
for (int j = 0; j < n; j++) {
dp[i & 1][j] = dp[(i + 1) & 1][j];
dp2[i & 1][j] = dp2[(i + 1) & 1][j];
if ((j >> i) & 1) {
dp[i & 1][j] += dp[(i + 1) & 1][j ^ (1 << i)];
} else {
dp2[i & 1][j] += dp2[(i + 1) & 1][j ^ (1 << i)];
}
}
}
while (q--) {
cin >> s;
int frq[3]{};
for (int i = 0; i < m; i++) {
if (s[i] == '?') {
frq[2]++;
} else {
frq[s[i] - '0']++;
}
}
reverse(s.begin(), s.end());
int ans = 0;
if (frq[2] <= frq[0] && frq[2] <= frq[1]) {
for (int msk = 0; msk < (1 << frq[2]); msk++) {
int cur = 0;
int v = 0;
for (int i = 0; i < m; i++) {
int b = s[i] - '0';
if (s[i] == '?') {
b = (msk >> (cur++)) & 1;
}
v += b << i;
}
ans += a[v];
}
} else if (frq[1] <= frq[0] && frq[1] <= frq[2]) {
for (int msk = 0; msk < (1 << frq[1]); msk++) {
int cur = 0, v = 0;
for (int i = 0; i < m; i++) {
int b = s[i] - '0';
if (s[i] == '?') b = 1;
if (s[i] == '1') b = ((msk >> (cur++)) & 1);
v += b << i;
}
int cnt = __builtin_popcount(msk);
if ((frq[1] - cnt) & 1) {
ans -= dp[0][v];
} else {
ans += dp[0][v];
}
}
} else {
for (int msk = 0; msk < (1 << frq[0]); msk++) {
int cur = 0, v = 0;
for (int i = 0; i < m; i++) {
int b = s[i] - '0';
if (s[i] == '?') b = 0;
if (s[i] == '0') b = ((msk >> (cur++)) & 1);
v += b << i;
}
int cnt = __builtin_popcount(msk);
if (cnt & 1) {
ans -= dp2[0][v];
} else {
ans += dp2[0][v];
}
}
}
cout << ans << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
177 ms |
23636 KB |
Output is correct |
12 |
Correct |
195 ms |
22868 KB |
Output is correct |
13 |
Correct |
255 ms |
22356 KB |
Output is correct |
14 |
Correct |
243 ms |
22204 KB |
Output is correct |
15 |
Correct |
297 ms |
23364 KB |
Output is correct |
16 |
Correct |
358 ms |
22448 KB |
Output is correct |
17 |
Correct |
317 ms |
22364 KB |
Output is correct |
18 |
Correct |
144 ms |
24404 KB |
Output is correct |
19 |
Correct |
135 ms |
21264 KB |
Output is correct |
20 |
Correct |
197 ms |
22864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
177 ms |
23636 KB |
Output is correct |
12 |
Correct |
195 ms |
22868 KB |
Output is correct |
13 |
Correct |
255 ms |
22356 KB |
Output is correct |
14 |
Correct |
243 ms |
22204 KB |
Output is correct |
15 |
Correct |
297 ms |
23364 KB |
Output is correct |
16 |
Correct |
358 ms |
22448 KB |
Output is correct |
17 |
Correct |
317 ms |
22364 KB |
Output is correct |
18 |
Correct |
144 ms |
24404 KB |
Output is correct |
19 |
Correct |
135 ms |
21264 KB |
Output is correct |
20 |
Correct |
197 ms |
22864 KB |
Output is correct |
21 |
Correct |
224 ms |
26192 KB |
Output is correct |
22 |
Correct |
246 ms |
26496 KB |
Output is correct |
23 |
Correct |
424 ms |
25536 KB |
Output is correct |
24 |
Correct |
541 ms |
25312 KB |
Output is correct |
25 |
Correct |
381 ms |
27336 KB |
Output is correct |
26 |
Correct |
573 ms |
25880 KB |
Output is correct |
27 |
Correct |
497 ms |
25644 KB |
Output is correct |
28 |
Correct |
171 ms |
28496 KB |
Output is correct |
29 |
Correct |
145 ms |
24376 KB |
Output is correct |
30 |
Correct |
254 ms |
26472 KB |
Output is correct |
31 |
Correct |
543 ms |
26512 KB |
Output is correct |
32 |
Correct |
781 ms |
26192 KB |
Output is correct |
33 |
Correct |
287 ms |
23124 KB |
Output is correct |
34 |
Correct |
533 ms |
25196 KB |
Output is correct |
35 |
Correct |
503 ms |
25692 KB |
Output is correct |
36 |
Correct |
152 ms |
24184 KB |
Output is correct |
37 |
Correct |
228 ms |
26172 KB |
Output is correct |
38 |
Correct |
147 ms |
24336 KB |
Output is correct |
39 |
Correct |
425 ms |
25680 KB |
Output is correct |
40 |
Correct |
509 ms |
25172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
61 ms |
23236 KB |
Output is correct |
12 |
Correct |
57 ms |
23120 KB |
Output is correct |
13 |
Correct |
89 ms |
23116 KB |
Output is correct |
14 |
Correct |
159 ms |
23204 KB |
Output is correct |
15 |
Correct |
76 ms |
23256 KB |
Output is correct |
16 |
Correct |
155 ms |
23128 KB |
Output is correct |
17 |
Correct |
110 ms |
23128 KB |
Output is correct |
18 |
Correct |
52 ms |
23376 KB |
Output is correct |
19 |
Correct |
53 ms |
22996 KB |
Output is correct |
20 |
Correct |
62 ms |
23204 KB |
Output is correct |
21 |
Correct |
97 ms |
23240 KB |
Output is correct |
22 |
Correct |
155 ms |
23088 KB |
Output is correct |
23 |
Correct |
71 ms |
22992 KB |
Output is correct |
24 |
Correct |
122 ms |
22980 KB |
Output is correct |
25 |
Correct |
113 ms |
23124 KB |
Output is correct |
26 |
Correct |
52 ms |
22868 KB |
Output is correct |
27 |
Correct |
72 ms |
23332 KB |
Output is correct |
28 |
Correct |
55 ms |
22952 KB |
Output is correct |
29 |
Correct |
87 ms |
22988 KB |
Output is correct |
30 |
Correct |
128 ms |
23120 KB |
Output is correct |
31 |
Correct |
75 ms |
23388 KB |
Output is correct |
32 |
Correct |
133 ms |
22984 KB |
Output is correct |
33 |
Correct |
112 ms |
22996 KB |
Output is correct |
34 |
Correct |
51 ms |
23016 KB |
Output is correct |
35 |
Correct |
100 ms |
23048 KB |
Output is correct |
36 |
Correct |
108 ms |
23148 KB |
Output is correct |
37 |
Correct |
96 ms |
23144 KB |
Output is correct |
38 |
Correct |
98 ms |
23220 KB |
Output is correct |
39 |
Correct |
95 ms |
23124 KB |
Output is correct |
40 |
Correct |
94 ms |
22984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
2 ms |
8540 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
2 ms |
8540 KB |
Output is correct |
11 |
Correct |
177 ms |
23636 KB |
Output is correct |
12 |
Correct |
195 ms |
22868 KB |
Output is correct |
13 |
Correct |
255 ms |
22356 KB |
Output is correct |
14 |
Correct |
243 ms |
22204 KB |
Output is correct |
15 |
Correct |
297 ms |
23364 KB |
Output is correct |
16 |
Correct |
358 ms |
22448 KB |
Output is correct |
17 |
Correct |
317 ms |
22364 KB |
Output is correct |
18 |
Correct |
144 ms |
24404 KB |
Output is correct |
19 |
Correct |
135 ms |
21264 KB |
Output is correct |
20 |
Correct |
197 ms |
22864 KB |
Output is correct |
21 |
Correct |
224 ms |
26192 KB |
Output is correct |
22 |
Correct |
246 ms |
26496 KB |
Output is correct |
23 |
Correct |
424 ms |
25536 KB |
Output is correct |
24 |
Correct |
541 ms |
25312 KB |
Output is correct |
25 |
Correct |
381 ms |
27336 KB |
Output is correct |
26 |
Correct |
573 ms |
25880 KB |
Output is correct |
27 |
Correct |
497 ms |
25644 KB |
Output is correct |
28 |
Correct |
171 ms |
28496 KB |
Output is correct |
29 |
Correct |
145 ms |
24376 KB |
Output is correct |
30 |
Correct |
254 ms |
26472 KB |
Output is correct |
31 |
Correct |
543 ms |
26512 KB |
Output is correct |
32 |
Correct |
781 ms |
26192 KB |
Output is correct |
33 |
Correct |
287 ms |
23124 KB |
Output is correct |
34 |
Correct |
533 ms |
25196 KB |
Output is correct |
35 |
Correct |
503 ms |
25692 KB |
Output is correct |
36 |
Correct |
152 ms |
24184 KB |
Output is correct |
37 |
Correct |
228 ms |
26172 KB |
Output is correct |
38 |
Correct |
147 ms |
24336 KB |
Output is correct |
39 |
Correct |
425 ms |
25680 KB |
Output is correct |
40 |
Correct |
509 ms |
25172 KB |
Output is correct |
41 |
Correct |
61 ms |
23236 KB |
Output is correct |
42 |
Correct |
57 ms |
23120 KB |
Output is correct |
43 |
Correct |
89 ms |
23116 KB |
Output is correct |
44 |
Correct |
159 ms |
23204 KB |
Output is correct |
45 |
Correct |
76 ms |
23256 KB |
Output is correct |
46 |
Correct |
155 ms |
23128 KB |
Output is correct |
47 |
Correct |
110 ms |
23128 KB |
Output is correct |
48 |
Correct |
52 ms |
23376 KB |
Output is correct |
49 |
Correct |
53 ms |
22996 KB |
Output is correct |
50 |
Correct |
62 ms |
23204 KB |
Output is correct |
51 |
Correct |
97 ms |
23240 KB |
Output is correct |
52 |
Correct |
155 ms |
23088 KB |
Output is correct |
53 |
Correct |
71 ms |
22992 KB |
Output is correct |
54 |
Correct |
122 ms |
22980 KB |
Output is correct |
55 |
Correct |
113 ms |
23124 KB |
Output is correct |
56 |
Correct |
52 ms |
22868 KB |
Output is correct |
57 |
Correct |
72 ms |
23332 KB |
Output is correct |
58 |
Correct |
55 ms |
22952 KB |
Output is correct |
59 |
Correct |
87 ms |
22988 KB |
Output is correct |
60 |
Correct |
128 ms |
23120 KB |
Output is correct |
61 |
Correct |
75 ms |
23388 KB |
Output is correct |
62 |
Correct |
133 ms |
22984 KB |
Output is correct |
63 |
Correct |
112 ms |
22996 KB |
Output is correct |
64 |
Correct |
51 ms |
23016 KB |
Output is correct |
65 |
Correct |
100 ms |
23048 KB |
Output is correct |
66 |
Correct |
108 ms |
23148 KB |
Output is correct |
67 |
Correct |
96 ms |
23144 KB |
Output is correct |
68 |
Correct |
98 ms |
23220 KB |
Output is correct |
69 |
Correct |
95 ms |
23124 KB |
Output is correct |
70 |
Correct |
94 ms |
22984 KB |
Output is correct |
71 |
Correct |
372 ms |
47308 KB |
Output is correct |
72 |
Correct |
377 ms |
47440 KB |
Output is correct |
73 |
Correct |
877 ms |
46068 KB |
Output is correct |
74 |
Execution timed out |
2037 ms |
43288 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |