#include <bits/stdc++.h>
using namespace std;
#define ll long long
int dp[21][1 << 20], dp2[21][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;
int a[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--) {
string s;
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 |
8 ms |
41308 KB |
Output is correct |
2 |
Correct |
5 ms |
39260 KB |
Output is correct |
3 |
Correct |
6 ms |
41432 KB |
Output is correct |
4 |
Correct |
5 ms |
39260 KB |
Output is correct |
5 |
Correct |
5 ms |
39260 KB |
Output is correct |
6 |
Correct |
5 ms |
39260 KB |
Output is correct |
7 |
Correct |
5 ms |
39384 KB |
Output is correct |
8 |
Correct |
5 ms |
39260 KB |
Output is correct |
9 |
Correct |
5 ms |
39260 KB |
Output is correct |
10 |
Correct |
5 ms |
39260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
41308 KB |
Output is correct |
2 |
Correct |
5 ms |
39260 KB |
Output is correct |
3 |
Correct |
6 ms |
41432 KB |
Output is correct |
4 |
Correct |
5 ms |
39260 KB |
Output is correct |
5 |
Correct |
5 ms |
39260 KB |
Output is correct |
6 |
Correct |
5 ms |
39260 KB |
Output is correct |
7 |
Correct |
5 ms |
39384 KB |
Output is correct |
8 |
Correct |
5 ms |
39260 KB |
Output is correct |
9 |
Correct |
5 ms |
39260 KB |
Output is correct |
10 |
Correct |
5 ms |
39260 KB |
Output is correct |
11 |
Correct |
190 ms |
49424 KB |
Output is correct |
12 |
Correct |
203 ms |
48720 KB |
Output is correct |
13 |
Correct |
261 ms |
48224 KB |
Output is correct |
14 |
Correct |
267 ms |
47956 KB |
Output is correct |
15 |
Correct |
292 ms |
48984 KB |
Output is correct |
16 |
Correct |
367 ms |
48244 KB |
Output is correct |
17 |
Correct |
329 ms |
48120 KB |
Output is correct |
18 |
Correct |
153 ms |
50040 KB |
Output is correct |
19 |
Correct |
146 ms |
46988 KB |
Output is correct |
20 |
Correct |
206 ms |
48680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
41308 KB |
Output is correct |
2 |
Correct |
5 ms |
39260 KB |
Output is correct |
3 |
Correct |
6 ms |
41432 KB |
Output is correct |
4 |
Correct |
5 ms |
39260 KB |
Output is correct |
5 |
Correct |
5 ms |
39260 KB |
Output is correct |
6 |
Correct |
5 ms |
39260 KB |
Output is correct |
7 |
Correct |
5 ms |
39384 KB |
Output is correct |
8 |
Correct |
5 ms |
39260 KB |
Output is correct |
9 |
Correct |
5 ms |
39260 KB |
Output is correct |
10 |
Correct |
5 ms |
39260 KB |
Output is correct |
11 |
Correct |
190 ms |
49424 KB |
Output is correct |
12 |
Correct |
203 ms |
48720 KB |
Output is correct |
13 |
Correct |
261 ms |
48224 KB |
Output is correct |
14 |
Correct |
267 ms |
47956 KB |
Output is correct |
15 |
Correct |
292 ms |
48984 KB |
Output is correct |
16 |
Correct |
367 ms |
48244 KB |
Output is correct |
17 |
Correct |
329 ms |
48120 KB |
Output is correct |
18 |
Correct |
153 ms |
50040 KB |
Output is correct |
19 |
Correct |
146 ms |
46988 KB |
Output is correct |
20 |
Correct |
206 ms |
48680 KB |
Output is correct |
21 |
Correct |
232 ms |
50984 KB |
Output is correct |
22 |
Correct |
255 ms |
51028 KB |
Output is correct |
23 |
Correct |
436 ms |
50088 KB |
Output is correct |
24 |
Correct |
567 ms |
51896 KB |
Output is correct |
25 |
Correct |
400 ms |
52104 KB |
Output is correct |
26 |
Correct |
593 ms |
52304 KB |
Output is correct |
27 |
Correct |
531 ms |
50256 KB |
Output is correct |
28 |
Correct |
170 ms |
53072 KB |
Output is correct |
29 |
Correct |
157 ms |
48976 KB |
Output is correct |
30 |
Correct |
271 ms |
51108 KB |
Output is correct |
31 |
Correct |
552 ms |
53076 KB |
Output is correct |
32 |
Correct |
799 ms |
44116 KB |
Output is correct |
33 |
Correct |
327 ms |
42532 KB |
Output is correct |
34 |
Correct |
554 ms |
42724 KB |
Output is correct |
35 |
Correct |
521 ms |
43088 KB |
Output is correct |
36 |
Correct |
161 ms |
41800 KB |
Output is correct |
37 |
Correct |
244 ms |
45968 KB |
Output is correct |
38 |
Correct |
156 ms |
41688 KB |
Output is correct |
39 |
Correct |
441 ms |
43004 KB |
Output is correct |
40 |
Correct |
553 ms |
44960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
41308 KB |
Output is correct |
2 |
Correct |
5 ms |
39260 KB |
Output is correct |
3 |
Correct |
6 ms |
41432 KB |
Output is correct |
4 |
Correct |
5 ms |
39260 KB |
Output is correct |
5 |
Correct |
5 ms |
39260 KB |
Output is correct |
6 |
Correct |
5 ms |
39260 KB |
Output is correct |
7 |
Correct |
5 ms |
39384 KB |
Output is correct |
8 |
Correct |
5 ms |
39260 KB |
Output is correct |
9 |
Correct |
5 ms |
39260 KB |
Output is correct |
10 |
Correct |
5 ms |
39260 KB |
Output is correct |
11 |
Runtime error |
39 ms |
65536 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
41308 KB |
Output is correct |
2 |
Correct |
5 ms |
39260 KB |
Output is correct |
3 |
Correct |
6 ms |
41432 KB |
Output is correct |
4 |
Correct |
5 ms |
39260 KB |
Output is correct |
5 |
Correct |
5 ms |
39260 KB |
Output is correct |
6 |
Correct |
5 ms |
39260 KB |
Output is correct |
7 |
Correct |
5 ms |
39384 KB |
Output is correct |
8 |
Correct |
5 ms |
39260 KB |
Output is correct |
9 |
Correct |
5 ms |
39260 KB |
Output is correct |
10 |
Correct |
5 ms |
39260 KB |
Output is correct |
11 |
Correct |
190 ms |
49424 KB |
Output is correct |
12 |
Correct |
203 ms |
48720 KB |
Output is correct |
13 |
Correct |
261 ms |
48224 KB |
Output is correct |
14 |
Correct |
267 ms |
47956 KB |
Output is correct |
15 |
Correct |
292 ms |
48984 KB |
Output is correct |
16 |
Correct |
367 ms |
48244 KB |
Output is correct |
17 |
Correct |
329 ms |
48120 KB |
Output is correct |
18 |
Correct |
153 ms |
50040 KB |
Output is correct |
19 |
Correct |
146 ms |
46988 KB |
Output is correct |
20 |
Correct |
206 ms |
48680 KB |
Output is correct |
21 |
Correct |
232 ms |
50984 KB |
Output is correct |
22 |
Correct |
255 ms |
51028 KB |
Output is correct |
23 |
Correct |
436 ms |
50088 KB |
Output is correct |
24 |
Correct |
567 ms |
51896 KB |
Output is correct |
25 |
Correct |
400 ms |
52104 KB |
Output is correct |
26 |
Correct |
593 ms |
52304 KB |
Output is correct |
27 |
Correct |
531 ms |
50256 KB |
Output is correct |
28 |
Correct |
170 ms |
53072 KB |
Output is correct |
29 |
Correct |
157 ms |
48976 KB |
Output is correct |
30 |
Correct |
271 ms |
51108 KB |
Output is correct |
31 |
Correct |
552 ms |
53076 KB |
Output is correct |
32 |
Correct |
799 ms |
44116 KB |
Output is correct |
33 |
Correct |
327 ms |
42532 KB |
Output is correct |
34 |
Correct |
554 ms |
42724 KB |
Output is correct |
35 |
Correct |
521 ms |
43088 KB |
Output is correct |
36 |
Correct |
161 ms |
41800 KB |
Output is correct |
37 |
Correct |
244 ms |
45968 KB |
Output is correct |
38 |
Correct |
156 ms |
41688 KB |
Output is correct |
39 |
Correct |
441 ms |
43004 KB |
Output is correct |
40 |
Correct |
553 ms |
44960 KB |
Output is correct |
41 |
Runtime error |
39 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Halted |
0 ms |
0 KB |
- |