#include <bits/stdc++.h>
using namespace std;
#define ll long long
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';
}
int dp[m + 1][n], dp2[m + 1][n];
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 |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
218 ms |
15188 KB |
Output is correct |
12 |
Correct |
201 ms |
14876 KB |
Output is correct |
13 |
Correct |
256 ms |
14020 KB |
Output is correct |
14 |
Correct |
243 ms |
14160 KB |
Output is correct |
15 |
Correct |
286 ms |
15064 KB |
Output is correct |
16 |
Correct |
384 ms |
14416 KB |
Output is correct |
17 |
Correct |
343 ms |
14416 KB |
Output is correct |
18 |
Correct |
157 ms |
16104 KB |
Output is correct |
19 |
Correct |
137 ms |
13168 KB |
Output is correct |
20 |
Correct |
225 ms |
14916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
218 ms |
15188 KB |
Output is correct |
12 |
Correct |
201 ms |
14876 KB |
Output is correct |
13 |
Correct |
256 ms |
14020 KB |
Output is correct |
14 |
Correct |
243 ms |
14160 KB |
Output is correct |
15 |
Correct |
286 ms |
15064 KB |
Output is correct |
16 |
Correct |
384 ms |
14416 KB |
Output is correct |
17 |
Correct |
343 ms |
14416 KB |
Output is correct |
18 |
Correct |
157 ms |
16104 KB |
Output is correct |
19 |
Correct |
137 ms |
13168 KB |
Output is correct |
20 |
Correct |
225 ms |
14916 KB |
Output is correct |
21 |
Correct |
231 ms |
19024 KB |
Output is correct |
22 |
Correct |
253 ms |
19024 KB |
Output is correct |
23 |
Correct |
452 ms |
18000 KB |
Output is correct |
24 |
Correct |
522 ms |
17960 KB |
Output is correct |
25 |
Correct |
388 ms |
19824 KB |
Output is correct |
26 |
Correct |
581 ms |
18516 KB |
Output is correct |
27 |
Correct |
535 ms |
18488 KB |
Output is correct |
28 |
Correct |
163 ms |
20940 KB |
Output is correct |
29 |
Correct |
145 ms |
17176 KB |
Output is correct |
30 |
Correct |
260 ms |
19452 KB |
Output is correct |
31 |
Correct |
538 ms |
19496 KB |
Output is correct |
32 |
Correct |
810 ms |
19072 KB |
Output is correct |
33 |
Correct |
285 ms |
17748 KB |
Output is correct |
34 |
Correct |
537 ms |
17968 KB |
Output is correct |
35 |
Correct |
497 ms |
18528 KB |
Output is correct |
36 |
Correct |
155 ms |
16948 KB |
Output is correct |
37 |
Correct |
238 ms |
18848 KB |
Output is correct |
38 |
Correct |
149 ms |
16976 KB |
Output is correct |
39 |
Correct |
440 ms |
18132 KB |
Output is correct |
40 |
Correct |
507 ms |
17872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Runtime error |
49 ms |
65536 KB |
Execution killed with signal 9 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
464 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
218 ms |
15188 KB |
Output is correct |
12 |
Correct |
201 ms |
14876 KB |
Output is correct |
13 |
Correct |
256 ms |
14020 KB |
Output is correct |
14 |
Correct |
243 ms |
14160 KB |
Output is correct |
15 |
Correct |
286 ms |
15064 KB |
Output is correct |
16 |
Correct |
384 ms |
14416 KB |
Output is correct |
17 |
Correct |
343 ms |
14416 KB |
Output is correct |
18 |
Correct |
157 ms |
16104 KB |
Output is correct |
19 |
Correct |
137 ms |
13168 KB |
Output is correct |
20 |
Correct |
225 ms |
14916 KB |
Output is correct |
21 |
Correct |
231 ms |
19024 KB |
Output is correct |
22 |
Correct |
253 ms |
19024 KB |
Output is correct |
23 |
Correct |
452 ms |
18000 KB |
Output is correct |
24 |
Correct |
522 ms |
17960 KB |
Output is correct |
25 |
Correct |
388 ms |
19824 KB |
Output is correct |
26 |
Correct |
581 ms |
18516 KB |
Output is correct |
27 |
Correct |
535 ms |
18488 KB |
Output is correct |
28 |
Correct |
163 ms |
20940 KB |
Output is correct |
29 |
Correct |
145 ms |
17176 KB |
Output is correct |
30 |
Correct |
260 ms |
19452 KB |
Output is correct |
31 |
Correct |
538 ms |
19496 KB |
Output is correct |
32 |
Correct |
810 ms |
19072 KB |
Output is correct |
33 |
Correct |
285 ms |
17748 KB |
Output is correct |
34 |
Correct |
537 ms |
17968 KB |
Output is correct |
35 |
Correct |
497 ms |
18528 KB |
Output is correct |
36 |
Correct |
155 ms |
16948 KB |
Output is correct |
37 |
Correct |
238 ms |
18848 KB |
Output is correct |
38 |
Correct |
149 ms |
16976 KB |
Output is correct |
39 |
Correct |
440 ms |
18132 KB |
Output is correct |
40 |
Correct |
507 ms |
17872 KB |
Output is correct |
41 |
Runtime error |
49 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Halted |
0 ms |
0 KB |
- |