#include <bits/stdc++.h>
using namespace std;
#define ll long long
int dp[21][1 << 20], dp2[21][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][i] = a[i], dp2[m][i] = a[i];
for (int i = m - 1; i >= 0; i--) {
for (int j = 0; j < n; j++) {
dp[i][j] = dp[i + 1][j];
dp2[i][j] = dp2[i + 1][j];
if ((j >> i) & 1) {
dp[i][j] += dp[i + 1][j ^ (1 << i)];
} else {
dp2[i][j] += dp2[i + 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
45404 KB |
Output is correct |
2 |
Correct |
6 ms |
45588 KB |
Output is correct |
3 |
Correct |
5 ms |
45404 KB |
Output is correct |
4 |
Correct |
5 ms |
45404 KB |
Output is correct |
5 |
Correct |
6 ms |
45404 KB |
Output is correct |
6 |
Correct |
6 ms |
45404 KB |
Output is correct |
7 |
Correct |
6 ms |
45584 KB |
Output is correct |
8 |
Correct |
6 ms |
45404 KB |
Output is correct |
9 |
Correct |
6 ms |
45532 KB |
Output is correct |
10 |
Correct |
5 ms |
45404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
45404 KB |
Output is correct |
2 |
Correct |
6 ms |
45588 KB |
Output is correct |
3 |
Correct |
5 ms |
45404 KB |
Output is correct |
4 |
Correct |
5 ms |
45404 KB |
Output is correct |
5 |
Correct |
6 ms |
45404 KB |
Output is correct |
6 |
Correct |
6 ms |
45404 KB |
Output is correct |
7 |
Correct |
6 ms |
45584 KB |
Output is correct |
8 |
Correct |
6 ms |
45404 KB |
Output is correct |
9 |
Correct |
6 ms |
45532 KB |
Output is correct |
10 |
Correct |
5 ms |
45404 KB |
Output is correct |
11 |
Correct |
173 ms |
49472 KB |
Output is correct |
12 |
Correct |
193 ms |
49148 KB |
Output is correct |
13 |
Correct |
264 ms |
48460 KB |
Output is correct |
14 |
Correct |
253 ms |
48464 KB |
Output is correct |
15 |
Correct |
277 ms |
49536 KB |
Output is correct |
16 |
Correct |
348 ms |
48632 KB |
Output is correct |
17 |
Correct |
318 ms |
48724 KB |
Output is correct |
18 |
Correct |
146 ms |
50520 KB |
Output is correct |
19 |
Correct |
129 ms |
47444 KB |
Output is correct |
20 |
Correct |
195 ms |
49268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
45404 KB |
Output is correct |
2 |
Correct |
6 ms |
45588 KB |
Output is correct |
3 |
Correct |
5 ms |
45404 KB |
Output is correct |
4 |
Correct |
5 ms |
45404 KB |
Output is correct |
5 |
Correct |
6 ms |
45404 KB |
Output is correct |
6 |
Correct |
6 ms |
45404 KB |
Output is correct |
7 |
Correct |
6 ms |
45584 KB |
Output is correct |
8 |
Correct |
6 ms |
45404 KB |
Output is correct |
9 |
Correct |
6 ms |
45532 KB |
Output is correct |
10 |
Correct |
5 ms |
45404 KB |
Output is correct |
11 |
Correct |
173 ms |
49472 KB |
Output is correct |
12 |
Correct |
193 ms |
49148 KB |
Output is correct |
13 |
Correct |
264 ms |
48460 KB |
Output is correct |
14 |
Correct |
253 ms |
48464 KB |
Output is correct |
15 |
Correct |
277 ms |
49536 KB |
Output is correct |
16 |
Correct |
348 ms |
48632 KB |
Output is correct |
17 |
Correct |
318 ms |
48724 KB |
Output is correct |
18 |
Correct |
146 ms |
50520 KB |
Output is correct |
19 |
Correct |
129 ms |
47444 KB |
Output is correct |
20 |
Correct |
195 ms |
49268 KB |
Output is correct |
21 |
Correct |
235 ms |
61720 KB |
Output is correct |
22 |
Correct |
268 ms |
62036 KB |
Output is correct |
23 |
Correct |
428 ms |
61324 KB |
Output is correct |
24 |
Correct |
503 ms |
60836 KB |
Output is correct |
25 |
Correct |
378 ms |
62740 KB |
Output is correct |
26 |
Correct |
572 ms |
61392 KB |
Output is correct |
27 |
Correct |
505 ms |
61444 KB |
Output is correct |
28 |
Correct |
163 ms |
63828 KB |
Output is correct |
29 |
Correct |
150 ms |
59812 KB |
Output is correct |
30 |
Correct |
250 ms |
62032 KB |
Output is correct |
31 |
Correct |
546 ms |
62000 KB |
Output is correct |
32 |
Correct |
766 ms |
61832 KB |
Output is correct |
33 |
Correct |
294 ms |
60644 KB |
Output is correct |
34 |
Correct |
530 ms |
60808 KB |
Output is correct |
35 |
Correct |
500 ms |
61184 KB |
Output is correct |
36 |
Correct |
153 ms |
59732 KB |
Output is correct |
37 |
Correct |
226 ms |
61776 KB |
Output is correct |
38 |
Correct |
144 ms |
59732 KB |
Output is correct |
39 |
Correct |
434 ms |
61008 KB |
Output is correct |
40 |
Correct |
519 ms |
60828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
45404 KB |
Output is correct |
2 |
Correct |
6 ms |
45588 KB |
Output is correct |
3 |
Correct |
5 ms |
45404 KB |
Output is correct |
4 |
Correct |
5 ms |
45404 KB |
Output is correct |
5 |
Correct |
6 ms |
45404 KB |
Output is correct |
6 |
Correct |
6 ms |
45404 KB |
Output is correct |
7 |
Correct |
6 ms |
45584 KB |
Output is correct |
8 |
Correct |
6 ms |
45404 KB |
Output is correct |
9 |
Correct |
6 ms |
45532 KB |
Output is correct |
10 |
Correct |
5 ms |
45404 KB |
Output is correct |
11 |
Runtime error |
30 ms |
65536 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
45404 KB |
Output is correct |
2 |
Correct |
6 ms |
45588 KB |
Output is correct |
3 |
Correct |
5 ms |
45404 KB |
Output is correct |
4 |
Correct |
5 ms |
45404 KB |
Output is correct |
5 |
Correct |
6 ms |
45404 KB |
Output is correct |
6 |
Correct |
6 ms |
45404 KB |
Output is correct |
7 |
Correct |
6 ms |
45584 KB |
Output is correct |
8 |
Correct |
6 ms |
45404 KB |
Output is correct |
9 |
Correct |
6 ms |
45532 KB |
Output is correct |
10 |
Correct |
5 ms |
45404 KB |
Output is correct |
11 |
Correct |
173 ms |
49472 KB |
Output is correct |
12 |
Correct |
193 ms |
49148 KB |
Output is correct |
13 |
Correct |
264 ms |
48460 KB |
Output is correct |
14 |
Correct |
253 ms |
48464 KB |
Output is correct |
15 |
Correct |
277 ms |
49536 KB |
Output is correct |
16 |
Correct |
348 ms |
48632 KB |
Output is correct |
17 |
Correct |
318 ms |
48724 KB |
Output is correct |
18 |
Correct |
146 ms |
50520 KB |
Output is correct |
19 |
Correct |
129 ms |
47444 KB |
Output is correct |
20 |
Correct |
195 ms |
49268 KB |
Output is correct |
21 |
Correct |
235 ms |
61720 KB |
Output is correct |
22 |
Correct |
268 ms |
62036 KB |
Output is correct |
23 |
Correct |
428 ms |
61324 KB |
Output is correct |
24 |
Correct |
503 ms |
60836 KB |
Output is correct |
25 |
Correct |
378 ms |
62740 KB |
Output is correct |
26 |
Correct |
572 ms |
61392 KB |
Output is correct |
27 |
Correct |
505 ms |
61444 KB |
Output is correct |
28 |
Correct |
163 ms |
63828 KB |
Output is correct |
29 |
Correct |
150 ms |
59812 KB |
Output is correct |
30 |
Correct |
250 ms |
62032 KB |
Output is correct |
31 |
Correct |
546 ms |
62000 KB |
Output is correct |
32 |
Correct |
766 ms |
61832 KB |
Output is correct |
33 |
Correct |
294 ms |
60644 KB |
Output is correct |
34 |
Correct |
530 ms |
60808 KB |
Output is correct |
35 |
Correct |
500 ms |
61184 KB |
Output is correct |
36 |
Correct |
153 ms |
59732 KB |
Output is correct |
37 |
Correct |
226 ms |
61776 KB |
Output is correct |
38 |
Correct |
144 ms |
59732 KB |
Output is correct |
39 |
Correct |
434 ms |
61008 KB |
Output is correct |
40 |
Correct |
519 ms |
60828 KB |
Output is correct |
41 |
Runtime error |
30 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Halted |
0 ms |
0 KB |
- |