#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int L, Q;
cin >> L >> Q;
vector<vector<int>> A(2, vector<int>(1 << L));
vector<vector<int>> dp(2, vector<int>(1 << L)); // Subset Sum
for (int i = 0; i < (1 << L); i++) {
char c;
cin >> c;
dp[0][i] = A[0][i] = dp[1][i ^ ((1 << L) - 1)] = A[1][i ^ ((1 << L) - 1)] = (c - '0');
}
for (int bit = 0; bit < 2; bit++) {
for (int i = 0; i < L; i++) {
for (int mask = 0; mask < (1 << L); mask++) {
if (mask & (1 << i)) {
dp[bit][mask] += dp[bit][mask ^ (1 << i)];
}
}
}
}
for (int qi = 0; qi < Q; qi++) { // O(Q 2^(L/3))
string s;
cin >> s;
reverse(begin(s), end(s));
int mask0 = 0;
int mask1 = 0;
int maskq = 0;
for (int i = 0; i < L; i++) {
if (s[i] == '0') mask0 |= 1 << i;
if (s[i] == '1') mask1 |= 1 << i;
if (s[i] == '?') maskq |= 1 << i;
}
int ans = 0;
if (min(__builtin_popcount(mask0), __builtin_popcount(mask1)) < __builtin_popcount(maskq)) {
if (__builtin_popcount(mask0) < __builtin_popcount(mask1)) {
for (int m = mask0; ; m = (m - 1) & mask0) {
ans += dp[1][(mask0 | maskq) ^ m] * ((__builtin_popcount(m) & 1) ? -1 : 1);
if (!m) {
break;
}
}
} else {
for (int m = mask1; ; m = (m - 1) & mask1) {
ans += dp[0][(mask1 | maskq) ^ m] * ((__builtin_popcount(m) & 1) ? -1 : 1);
if (!m) {
break;
}
}
}
} else {
for (int m = maskq; ; m = (m - 1) & maskq) {
ans += A[0][mask1 | m];
if (!m) {
break;
}
}
}
cout << ans << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
259 ms |
15096 KB |
Output is correct |
12 |
Correct |
265 ms |
14840 KB |
Output is correct |
13 |
Correct |
290 ms |
14312 KB |
Output is correct |
14 |
Correct |
279 ms |
14204 KB |
Output is correct |
15 |
Correct |
270 ms |
15188 KB |
Output is correct |
16 |
Correct |
295 ms |
14328 KB |
Output is correct |
17 |
Correct |
300 ms |
14200 KB |
Output is correct |
18 |
Correct |
221 ms |
16248 KB |
Output is correct |
19 |
Correct |
249 ms |
13176 KB |
Output is correct |
20 |
Correct |
267 ms |
14896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
259 ms |
15096 KB |
Output is correct |
12 |
Correct |
265 ms |
14840 KB |
Output is correct |
13 |
Correct |
290 ms |
14312 KB |
Output is correct |
14 |
Correct |
279 ms |
14204 KB |
Output is correct |
15 |
Correct |
270 ms |
15188 KB |
Output is correct |
16 |
Correct |
295 ms |
14328 KB |
Output is correct |
17 |
Correct |
300 ms |
14200 KB |
Output is correct |
18 |
Correct |
221 ms |
16248 KB |
Output is correct |
19 |
Correct |
249 ms |
13176 KB |
Output is correct |
20 |
Correct |
267 ms |
14896 KB |
Output is correct |
21 |
Correct |
309 ms |
18168 KB |
Output is correct |
22 |
Correct |
306 ms |
18428 KB |
Output is correct |
23 |
Correct |
333 ms |
17400 KB |
Output is correct |
24 |
Correct |
334 ms |
17144 KB |
Output is correct |
25 |
Correct |
305 ms |
19396 KB |
Output is correct |
26 |
Correct |
345 ms |
17912 KB |
Output is correct |
27 |
Correct |
352 ms |
17784 KB |
Output is correct |
28 |
Correct |
239 ms |
20176 KB |
Output is correct |
29 |
Correct |
274 ms |
16376 KB |
Output is correct |
30 |
Correct |
292 ms |
18296 KB |
Output is correct |
31 |
Correct |
339 ms |
18332 KB |
Output is correct |
32 |
Correct |
375 ms |
18296 KB |
Output is correct |
33 |
Correct |
288 ms |
17272 KB |
Output is correct |
34 |
Correct |
351 ms |
17144 KB |
Output is correct |
35 |
Correct |
346 ms |
17784 KB |
Output is correct |
36 |
Correct |
221 ms |
16248 KB |
Output is correct |
37 |
Correct |
282 ms |
18168 KB |
Output is correct |
38 |
Correct |
283 ms |
16388 KB |
Output is correct |
39 |
Correct |
324 ms |
17420 KB |
Output is correct |
40 |
Correct |
317 ms |
17400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
145 ms |
23148 KB |
Output is correct |
12 |
Correct |
151 ms |
23276 KB |
Output is correct |
13 |
Correct |
153 ms |
23148 KB |
Output is correct |
14 |
Correct |
172 ms |
23148 KB |
Output is correct |
15 |
Correct |
150 ms |
23228 KB |
Output is correct |
16 |
Correct |
185 ms |
23232 KB |
Output is correct |
17 |
Correct |
172 ms |
23148 KB |
Output is correct |
18 |
Correct |
136 ms |
23276 KB |
Output is correct |
19 |
Correct |
145 ms |
23148 KB |
Output is correct |
20 |
Correct |
144 ms |
23148 KB |
Output is correct |
21 |
Correct |
158 ms |
23148 KB |
Output is correct |
22 |
Correct |
169 ms |
23148 KB |
Output is correct |
23 |
Correct |
145 ms |
23020 KB |
Output is correct |
24 |
Correct |
183 ms |
23148 KB |
Output is correct |
25 |
Correct |
173 ms |
23148 KB |
Output is correct |
26 |
Correct |
136 ms |
23148 KB |
Output is correct |
27 |
Correct |
143 ms |
23148 KB |
Output is correct |
28 |
Correct |
145 ms |
23024 KB |
Output is correct |
29 |
Correct |
153 ms |
23148 KB |
Output is correct |
30 |
Correct |
157 ms |
23148 KB |
Output is correct |
31 |
Correct |
144 ms |
23020 KB |
Output is correct |
32 |
Correct |
183 ms |
23100 KB |
Output is correct |
33 |
Correct |
175 ms |
23096 KB |
Output is correct |
34 |
Correct |
135 ms |
23020 KB |
Output is correct |
35 |
Correct |
164 ms |
23148 KB |
Output is correct |
36 |
Correct |
162 ms |
23100 KB |
Output is correct |
37 |
Correct |
165 ms |
23256 KB |
Output is correct |
38 |
Correct |
163 ms |
23148 KB |
Output is correct |
39 |
Correct |
164 ms |
23148 KB |
Output is correct |
40 |
Correct |
164 ms |
23148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
259 ms |
15096 KB |
Output is correct |
12 |
Correct |
265 ms |
14840 KB |
Output is correct |
13 |
Correct |
290 ms |
14312 KB |
Output is correct |
14 |
Correct |
279 ms |
14204 KB |
Output is correct |
15 |
Correct |
270 ms |
15188 KB |
Output is correct |
16 |
Correct |
295 ms |
14328 KB |
Output is correct |
17 |
Correct |
300 ms |
14200 KB |
Output is correct |
18 |
Correct |
221 ms |
16248 KB |
Output is correct |
19 |
Correct |
249 ms |
13176 KB |
Output is correct |
20 |
Correct |
267 ms |
14896 KB |
Output is correct |
21 |
Correct |
309 ms |
18168 KB |
Output is correct |
22 |
Correct |
306 ms |
18428 KB |
Output is correct |
23 |
Correct |
333 ms |
17400 KB |
Output is correct |
24 |
Correct |
334 ms |
17144 KB |
Output is correct |
25 |
Correct |
305 ms |
19396 KB |
Output is correct |
26 |
Correct |
345 ms |
17912 KB |
Output is correct |
27 |
Correct |
352 ms |
17784 KB |
Output is correct |
28 |
Correct |
239 ms |
20176 KB |
Output is correct |
29 |
Correct |
274 ms |
16376 KB |
Output is correct |
30 |
Correct |
292 ms |
18296 KB |
Output is correct |
31 |
Correct |
339 ms |
18332 KB |
Output is correct |
32 |
Correct |
375 ms |
18296 KB |
Output is correct |
33 |
Correct |
288 ms |
17272 KB |
Output is correct |
34 |
Correct |
351 ms |
17144 KB |
Output is correct |
35 |
Correct |
346 ms |
17784 KB |
Output is correct |
36 |
Correct |
221 ms |
16248 KB |
Output is correct |
37 |
Correct |
282 ms |
18168 KB |
Output is correct |
38 |
Correct |
283 ms |
16388 KB |
Output is correct |
39 |
Correct |
324 ms |
17420 KB |
Output is correct |
40 |
Correct |
317 ms |
17400 KB |
Output is correct |
41 |
Correct |
145 ms |
23148 KB |
Output is correct |
42 |
Correct |
151 ms |
23276 KB |
Output is correct |
43 |
Correct |
153 ms |
23148 KB |
Output is correct |
44 |
Correct |
172 ms |
23148 KB |
Output is correct |
45 |
Correct |
150 ms |
23228 KB |
Output is correct |
46 |
Correct |
185 ms |
23232 KB |
Output is correct |
47 |
Correct |
172 ms |
23148 KB |
Output is correct |
48 |
Correct |
136 ms |
23276 KB |
Output is correct |
49 |
Correct |
145 ms |
23148 KB |
Output is correct |
50 |
Correct |
144 ms |
23148 KB |
Output is correct |
51 |
Correct |
158 ms |
23148 KB |
Output is correct |
52 |
Correct |
169 ms |
23148 KB |
Output is correct |
53 |
Correct |
145 ms |
23020 KB |
Output is correct |
54 |
Correct |
183 ms |
23148 KB |
Output is correct |
55 |
Correct |
173 ms |
23148 KB |
Output is correct |
56 |
Correct |
136 ms |
23148 KB |
Output is correct |
57 |
Correct |
143 ms |
23148 KB |
Output is correct |
58 |
Correct |
145 ms |
23024 KB |
Output is correct |
59 |
Correct |
153 ms |
23148 KB |
Output is correct |
60 |
Correct |
157 ms |
23148 KB |
Output is correct |
61 |
Correct |
144 ms |
23020 KB |
Output is correct |
62 |
Correct |
183 ms |
23100 KB |
Output is correct |
63 |
Correct |
175 ms |
23096 KB |
Output is correct |
64 |
Correct |
135 ms |
23020 KB |
Output is correct |
65 |
Correct |
164 ms |
23148 KB |
Output is correct |
66 |
Correct |
162 ms |
23100 KB |
Output is correct |
67 |
Correct |
165 ms |
23256 KB |
Output is correct |
68 |
Correct |
163 ms |
23148 KB |
Output is correct |
69 |
Correct |
164 ms |
23148 KB |
Output is correct |
70 |
Correct |
164 ms |
23148 KB |
Output is correct |
71 |
Correct |
584 ms |
47800 KB |
Output is correct |
72 |
Correct |
615 ms |
47804 KB |
Output is correct |
73 |
Correct |
800 ms |
46188 KB |
Output is correct |
74 |
Correct |
1106 ms |
46700 KB |
Output is correct |
75 |
Correct |
611 ms |
48492 KB |
Output is correct |
76 |
Correct |
1369 ms |
46956 KB |
Output is correct |
77 |
Correct |
1159 ms |
46804 KB |
Output is correct |
78 |
Correct |
426 ms |
50540 KB |
Output is correct |
79 |
Correct |
565 ms |
44652 KB |
Output is correct |
80 |
Correct |
585 ms |
47724 KB |
Output is correct |
81 |
Correct |
890 ms |
47832 KB |
Output is correct |
82 |
Correct |
1119 ms |
46652 KB |
Output is correct |
83 |
Correct |
585 ms |
45880 KB |
Output is correct |
84 |
Correct |
1116 ms |
46604 KB |
Output is correct |
85 |
Correct |
1172 ms |
46860 KB |
Output is correct |
86 |
Correct |
405 ms |
44524 KB |
Output is correct |
87 |
Correct |
595 ms |
47724 KB |
Output is correct |
88 |
Correct |
594 ms |
44528 KB |
Output is correct |
89 |
Correct |
761 ms |
46268 KB |
Output is correct |
90 |
Correct |
821 ms |
46872 KB |
Output is correct |
91 |
Correct |
652 ms |
45760 KB |
Output is correct |
92 |
Correct |
1357 ms |
46908 KB |
Output is correct |
93 |
Correct |
1161 ms |
46872 KB |
Output is correct |
94 |
Correct |
430 ms |
44556 KB |
Output is correct |
95 |
Correct |
1000 ms |
46648 KB |
Output is correct |
96 |
Correct |
978 ms |
46708 KB |
Output is correct |
97 |
Correct |
972 ms |
46880 KB |
Output is correct |
98 |
Correct |
978 ms |
46592 KB |
Output is correct |
99 |
Correct |
973 ms |
46660 KB |
Output is correct |
100 |
Correct |
972 ms |
46612 KB |
Output is correct |