#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]) {
int x = 0, st = 0;
for (int i = 0; i < m; i++) {
int b = s[i] - '0';
if (s[i] == '?') b = 1;
if (s[i] == '1') st += 1 << i, b = 0;
x += b << i;
}
for (int msk = st; msk >= 0; msk = ((msk - 1) & st)) {
int v = x | msk;
int cnt = __builtin_popcount(msk);
if ((frq[1] - cnt) & 1) {
ans -= dp[0][v];
} else {
ans += dp[0][v];
}
if (!msk) break;
}
} else {
int x = 0, st = 0;
for (int i = 0; i < m; i++) {
int b = s[i] - '0';
if (s[i] == '?') b = 0;
if (s[i] == '0') st += 1 << i;
x += b << i;
}
for (int msk = st; msk >= 0; msk = ((msk - 1) & st)) {
int v = x | msk;
int cnt = __builtin_popcount(msk);
if (cnt & 1) {
ans -= dp2[0][v];
} else {
ans += dp2[0][v];
}
if (msk == 0) break;
}
}
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8536 KB |
Output is correct |
10 |
Correct |
1 ms |
8536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8536 KB |
Output is correct |
10 |
Correct |
1 ms |
8536 KB |
Output is correct |
11 |
Correct |
184 ms |
23296 KB |
Output is correct |
12 |
Correct |
214 ms |
22996 KB |
Output is correct |
13 |
Correct |
197 ms |
22100 KB |
Output is correct |
14 |
Correct |
242 ms |
22276 KB |
Output is correct |
15 |
Correct |
205 ms |
23220 KB |
Output is correct |
16 |
Correct |
252 ms |
22352 KB |
Output is correct |
17 |
Correct |
251 ms |
22396 KB |
Output is correct |
18 |
Correct |
145 ms |
24208 KB |
Output is correct |
19 |
Correct |
142 ms |
21332 KB |
Output is correct |
20 |
Correct |
200 ms |
22868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8536 KB |
Output is correct |
10 |
Correct |
1 ms |
8536 KB |
Output is correct |
11 |
Correct |
184 ms |
23296 KB |
Output is correct |
12 |
Correct |
214 ms |
22996 KB |
Output is correct |
13 |
Correct |
197 ms |
22100 KB |
Output is correct |
14 |
Correct |
242 ms |
22276 KB |
Output is correct |
15 |
Correct |
205 ms |
23220 KB |
Output is correct |
16 |
Correct |
252 ms |
22352 KB |
Output is correct |
17 |
Correct |
251 ms |
22396 KB |
Output is correct |
18 |
Correct |
145 ms |
24208 KB |
Output is correct |
19 |
Correct |
142 ms |
21332 KB |
Output is correct |
20 |
Correct |
200 ms |
22868 KB |
Output is correct |
21 |
Correct |
227 ms |
26192 KB |
Output is correct |
22 |
Correct |
256 ms |
26312 KB |
Output is correct |
23 |
Correct |
299 ms |
25408 KB |
Output is correct |
24 |
Correct |
503 ms |
25424 KB |
Output is correct |
25 |
Correct |
258 ms |
27220 KB |
Output is correct |
26 |
Correct |
383 ms |
25792 KB |
Output is correct |
27 |
Correct |
336 ms |
25760 KB |
Output is correct |
28 |
Correct |
163 ms |
28284 KB |
Output is correct |
29 |
Correct |
159 ms |
24160 KB |
Output is correct |
30 |
Correct |
251 ms |
26476 KB |
Output is correct |
31 |
Correct |
279 ms |
26388 KB |
Output is correct |
32 |
Correct |
306 ms |
26160 KB |
Output is correct |
33 |
Correct |
203 ms |
25208 KB |
Output is correct |
34 |
Correct |
388 ms |
25672 KB |
Output is correct |
35 |
Correct |
333 ms |
25680 KB |
Output is correct |
36 |
Correct |
215 ms |
24160 KB |
Output is correct |
37 |
Correct |
243 ms |
26364 KB |
Output is correct |
38 |
Correct |
154 ms |
24364 KB |
Output is correct |
39 |
Correct |
296 ms |
17492 KB |
Output is correct |
40 |
Correct |
673 ms |
17392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8536 KB |
Output is correct |
10 |
Correct |
1 ms |
8536 KB |
Output is correct |
11 |
Correct |
62 ms |
23124 KB |
Output is correct |
12 |
Correct |
69 ms |
23240 KB |
Output is correct |
13 |
Correct |
100 ms |
23132 KB |
Output is correct |
14 |
Correct |
76 ms |
23000 KB |
Output is correct |
15 |
Correct |
62 ms |
23120 KB |
Output is correct |
16 |
Correct |
116 ms |
23120 KB |
Output is correct |
17 |
Correct |
104 ms |
23124 KB |
Output is correct |
18 |
Correct |
56 ms |
23376 KB |
Output is correct |
19 |
Correct |
55 ms |
22864 KB |
Output is correct |
20 |
Correct |
80 ms |
23240 KB |
Output is correct |
21 |
Correct |
66 ms |
23236 KB |
Output is correct |
22 |
Correct |
73 ms |
23124 KB |
Output is correct |
23 |
Correct |
62 ms |
23032 KB |
Output is correct |
24 |
Correct |
109 ms |
23120 KB |
Output is correct |
25 |
Correct |
84 ms |
23124 KB |
Output is correct |
26 |
Correct |
56 ms |
22868 KB |
Output is correct |
27 |
Correct |
63 ms |
23184 KB |
Output is correct |
28 |
Correct |
56 ms |
22920 KB |
Output is correct |
29 |
Correct |
89 ms |
23120 KB |
Output is correct |
30 |
Correct |
128 ms |
23048 KB |
Output is correct |
31 |
Correct |
60 ms |
23124 KB |
Output is correct |
32 |
Correct |
125 ms |
22984 KB |
Output is correct |
33 |
Correct |
95 ms |
22996 KB |
Output is correct |
34 |
Correct |
52 ms |
22984 KB |
Output is correct |
35 |
Correct |
77 ms |
23120 KB |
Output is correct |
36 |
Correct |
74 ms |
23124 KB |
Output is correct |
37 |
Correct |
75 ms |
23144 KB |
Output is correct |
38 |
Correct |
76 ms |
22984 KB |
Output is correct |
39 |
Correct |
75 ms |
23124 KB |
Output is correct |
40 |
Correct |
75 ms |
23124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8536 KB |
Output is correct |
10 |
Correct |
1 ms |
8536 KB |
Output is correct |
11 |
Correct |
184 ms |
23296 KB |
Output is correct |
12 |
Correct |
214 ms |
22996 KB |
Output is correct |
13 |
Correct |
197 ms |
22100 KB |
Output is correct |
14 |
Correct |
242 ms |
22276 KB |
Output is correct |
15 |
Correct |
205 ms |
23220 KB |
Output is correct |
16 |
Correct |
252 ms |
22352 KB |
Output is correct |
17 |
Correct |
251 ms |
22396 KB |
Output is correct |
18 |
Correct |
145 ms |
24208 KB |
Output is correct |
19 |
Correct |
142 ms |
21332 KB |
Output is correct |
20 |
Correct |
200 ms |
22868 KB |
Output is correct |
21 |
Correct |
227 ms |
26192 KB |
Output is correct |
22 |
Correct |
256 ms |
26312 KB |
Output is correct |
23 |
Correct |
299 ms |
25408 KB |
Output is correct |
24 |
Correct |
503 ms |
25424 KB |
Output is correct |
25 |
Correct |
258 ms |
27220 KB |
Output is correct |
26 |
Correct |
383 ms |
25792 KB |
Output is correct |
27 |
Correct |
336 ms |
25760 KB |
Output is correct |
28 |
Correct |
163 ms |
28284 KB |
Output is correct |
29 |
Correct |
159 ms |
24160 KB |
Output is correct |
30 |
Correct |
251 ms |
26476 KB |
Output is correct |
31 |
Correct |
279 ms |
26388 KB |
Output is correct |
32 |
Correct |
306 ms |
26160 KB |
Output is correct |
33 |
Correct |
203 ms |
25208 KB |
Output is correct |
34 |
Correct |
388 ms |
25672 KB |
Output is correct |
35 |
Correct |
333 ms |
25680 KB |
Output is correct |
36 |
Correct |
215 ms |
24160 KB |
Output is correct |
37 |
Correct |
243 ms |
26364 KB |
Output is correct |
38 |
Correct |
154 ms |
24364 KB |
Output is correct |
39 |
Correct |
296 ms |
17492 KB |
Output is correct |
40 |
Correct |
673 ms |
17392 KB |
Output is correct |
41 |
Correct |
62 ms |
23124 KB |
Output is correct |
42 |
Correct |
69 ms |
23240 KB |
Output is correct |
43 |
Correct |
100 ms |
23132 KB |
Output is correct |
44 |
Correct |
76 ms |
23000 KB |
Output is correct |
45 |
Correct |
62 ms |
23120 KB |
Output is correct |
46 |
Correct |
116 ms |
23120 KB |
Output is correct |
47 |
Correct |
104 ms |
23124 KB |
Output is correct |
48 |
Correct |
56 ms |
23376 KB |
Output is correct |
49 |
Correct |
55 ms |
22864 KB |
Output is correct |
50 |
Correct |
80 ms |
23240 KB |
Output is correct |
51 |
Correct |
66 ms |
23236 KB |
Output is correct |
52 |
Correct |
73 ms |
23124 KB |
Output is correct |
53 |
Correct |
62 ms |
23032 KB |
Output is correct |
54 |
Correct |
109 ms |
23120 KB |
Output is correct |
55 |
Correct |
84 ms |
23124 KB |
Output is correct |
56 |
Correct |
56 ms |
22868 KB |
Output is correct |
57 |
Correct |
63 ms |
23184 KB |
Output is correct |
58 |
Correct |
56 ms |
22920 KB |
Output is correct |
59 |
Correct |
89 ms |
23120 KB |
Output is correct |
60 |
Correct |
128 ms |
23048 KB |
Output is correct |
61 |
Correct |
60 ms |
23124 KB |
Output is correct |
62 |
Correct |
125 ms |
22984 KB |
Output is correct |
63 |
Correct |
95 ms |
22996 KB |
Output is correct |
64 |
Correct |
52 ms |
22984 KB |
Output is correct |
65 |
Correct |
77 ms |
23120 KB |
Output is correct |
66 |
Correct |
74 ms |
23124 KB |
Output is correct |
67 |
Correct |
75 ms |
23144 KB |
Output is correct |
68 |
Correct |
76 ms |
22984 KB |
Output is correct |
69 |
Correct |
75 ms |
23124 KB |
Output is correct |
70 |
Correct |
75 ms |
23124 KB |
Output is correct |
71 |
Correct |
372 ms |
47404 KB |
Output is correct |
72 |
Correct |
386 ms |
47444 KB |
Output is correct |
73 |
Correct |
682 ms |
46036 KB |
Output is correct |
74 |
Correct |
639 ms |
46380 KB |
Output is correct |
75 |
Correct |
374 ms |
48468 KB |
Output is correct |
76 |
Correct |
1018 ms |
46900 KB |
Output is correct |
77 |
Correct |
801 ms |
46932 KB |
Output is correct |
78 |
Correct |
240 ms |
50516 KB |
Output is correct |
79 |
Correct |
203 ms |
44372 KB |
Output is correct |
80 |
Correct |
380 ms |
47576 KB |
Output is correct |
81 |
Correct |
453 ms |
47444 KB |
Output is correct |
82 |
Correct |
573 ms |
46280 KB |
Output is correct |
83 |
Correct |
424 ms |
45652 KB |
Output is correct |
84 |
Correct |
1201 ms |
46540 KB |
Output is correct |
85 |
Correct |
749 ms |
46584 KB |
Output is correct |
86 |
Correct |
223 ms |
44368 KB |
Output is correct |
87 |
Correct |
378 ms |
47764 KB |
Output is correct |
88 |
Correct |
218 ms |
44492 KB |
Output is correct |
89 |
Correct |
684 ms |
46052 KB |
Output is correct |
90 |
Correct |
1714 ms |
46652 KB |
Output is correct |
91 |
Correct |
397 ms |
45508 KB |
Output is correct |
92 |
Correct |
934 ms |
46792 KB |
Output is correct |
93 |
Correct |
769 ms |
46548 KB |
Output is correct |
94 |
Correct |
220 ms |
44368 KB |
Output is correct |
95 |
Correct |
678 ms |
46604 KB |
Output is correct |
96 |
Correct |
646 ms |
46672 KB |
Output is correct |
97 |
Correct |
668 ms |
46676 KB |
Output is correct |
98 |
Correct |
651 ms |
46928 KB |
Output is correct |
99 |
Correct |
664 ms |
46808 KB |
Output is correct |
100 |
Correct |
655 ms |
46804 KB |
Output is correct |