#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int L, Q; cin >> L >> Q;
vector dp(2, vector<int>(1 << L));
vector<int> A(1 << L);
for (int i = 0; i < (1 << L); ++i) {
char c; cin >> c;
A[i] = c - '0';
for (int j = 0; j < 2; ++j) {
dp[j][i] += A[i];
}
}
for (int i = 0; i < L; ++i) {
for (int mask = 0; mask < (1 << L); ++mask) {
int c = mask >> i & 1;
dp[c][mask] += dp[c][mask ^ (1 << i)];
}
}
auto conv = [&](char c) {
return c == '?' ? 2 : c - '0';
};
while (Q--) {
array<int, 3> m{}, fre{};
for (int i = L - 1; i > -1; --i) {
char c; cin >> c;
int x = conv(c);
++fre[x];
m[x] += 1 << i;
}
long long res = 0;
if (fre[0] <= fre[1] && fre[0] <= fre[2]) {
for (int sm = m[0]; sm; sm = (sm - 1) & m[0]) {
int cnt = __builtin_popcount(sm);
res += (cnt & 1 ? -1 : 1) * dp[0][sm | m[1]];
}
res += dp[0][m[1]];
} else if (fre[1] <= fre[0] && fre[0] <= fre[2]) {
for (int sm = m[1]; sm; sm = (sm - 1) & m[1]) {
int M = m[1] ^ sm;
int cnt = __builtin_popcount(M);
M |= m[2];
res += (cnt % 2 == fre[1] % 2 ? 1 : -1) * dp[1][M];
}
res += dp[1][m[1] | m[2]];
} else {
for (int sm = m[2]; sm; sm = (sm - 1) & m[2]) {
res += A[sm | m[1]];
}
res += A[m[1]];
}
cout << res << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
134 ms |
15208 KB |
Output is correct |
12 |
Correct |
157 ms |
14708 KB |
Output is correct |
13 |
Correct |
158 ms |
13944 KB |
Output is correct |
14 |
Correct |
155 ms |
14160 KB |
Output is correct |
15 |
Correct |
161 ms |
15188 KB |
Output is correct |
16 |
Correct |
170 ms |
14384 KB |
Output is correct |
17 |
Correct |
169 ms |
14320 KB |
Output is correct |
18 |
Correct |
151 ms |
16024 KB |
Output is correct |
19 |
Correct |
134 ms |
13140 KB |
Output is correct |
20 |
Correct |
137 ms |
14672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
134 ms |
15208 KB |
Output is correct |
12 |
Correct |
157 ms |
14708 KB |
Output is correct |
13 |
Correct |
158 ms |
13944 KB |
Output is correct |
14 |
Correct |
155 ms |
14160 KB |
Output is correct |
15 |
Correct |
161 ms |
15188 KB |
Output is correct |
16 |
Correct |
170 ms |
14384 KB |
Output is correct |
17 |
Correct |
169 ms |
14320 KB |
Output is correct |
18 |
Correct |
151 ms |
16024 KB |
Output is correct |
19 |
Correct |
134 ms |
13140 KB |
Output is correct |
20 |
Correct |
137 ms |
14672 KB |
Output is correct |
21 |
Correct |
163 ms |
18132 KB |
Output is correct |
22 |
Correct |
204 ms |
18192 KB |
Output is correct |
23 |
Correct |
191 ms |
17284 KB |
Output is correct |
24 |
Correct |
181 ms |
17232 KB |
Output is correct |
25 |
Correct |
185 ms |
19028 KB |
Output is correct |
26 |
Correct |
212 ms |
17688 KB |
Output is correct |
27 |
Correct |
213 ms |
17488 KB |
Output is correct |
28 |
Correct |
199 ms |
20012 KB |
Output is correct |
29 |
Correct |
165 ms |
16212 KB |
Output is correct |
30 |
Correct |
192 ms |
18172 KB |
Output is correct |
31 |
Correct |
192 ms |
18256 KB |
Output is correct |
32 |
Correct |
206 ms |
18048 KB |
Output is correct |
33 |
Correct |
192 ms |
16984 KB |
Output is correct |
34 |
Correct |
199 ms |
17132 KB |
Output is correct |
35 |
Correct |
206 ms |
17628 KB |
Output is correct |
36 |
Correct |
163 ms |
15972 KB |
Output is correct |
37 |
Correct |
228 ms |
18080 KB |
Output is correct |
38 |
Correct |
171 ms |
16212 KB |
Output is correct |
39 |
Correct |
190 ms |
17488 KB |
Output is correct |
40 |
Correct |
216 ms |
17232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
49 ms |
14920 KB |
Output is correct |
12 |
Correct |
76 ms |
15064 KB |
Output is correct |
13 |
Correct |
57 ms |
14808 KB |
Output is correct |
14 |
Correct |
64 ms |
14916 KB |
Output is correct |
15 |
Correct |
50 ms |
14920 KB |
Output is correct |
16 |
Correct |
89 ms |
14916 KB |
Output is correct |
17 |
Correct |
95 ms |
14812 KB |
Output is correct |
18 |
Correct |
48 ms |
15072 KB |
Output is correct |
19 |
Correct |
74 ms |
14664 KB |
Output is correct |
20 |
Correct |
53 ms |
14920 KB |
Output is correct |
21 |
Correct |
57 ms |
15016 KB |
Output is correct |
22 |
Correct |
68 ms |
14916 KB |
Output is correct |
23 |
Correct |
56 ms |
14792 KB |
Output is correct |
24 |
Correct |
60 ms |
14812 KB |
Output is correct |
25 |
Correct |
67 ms |
14812 KB |
Output is correct |
26 |
Correct |
53 ms |
14864 KB |
Output is correct |
27 |
Correct |
56 ms |
15052 KB |
Output is correct |
28 |
Correct |
51 ms |
14916 KB |
Output is correct |
29 |
Correct |
54 ms |
14920 KB |
Output is correct |
30 |
Correct |
56 ms |
14808 KB |
Output is correct |
31 |
Correct |
81 ms |
14800 KB |
Output is correct |
32 |
Correct |
63 ms |
14920 KB |
Output is correct |
33 |
Correct |
71 ms |
14920 KB |
Output is correct |
34 |
Correct |
49 ms |
14680 KB |
Output is correct |
35 |
Correct |
56 ms |
14936 KB |
Output is correct |
36 |
Correct |
63 ms |
14916 KB |
Output is correct |
37 |
Correct |
66 ms |
14812 KB |
Output is correct |
38 |
Correct |
61 ms |
14920 KB |
Output is correct |
39 |
Correct |
61 ms |
15012 KB |
Output is correct |
40 |
Correct |
67 ms |
14916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
134 ms |
15208 KB |
Output is correct |
12 |
Correct |
157 ms |
14708 KB |
Output is correct |
13 |
Correct |
158 ms |
13944 KB |
Output is correct |
14 |
Correct |
155 ms |
14160 KB |
Output is correct |
15 |
Correct |
161 ms |
15188 KB |
Output is correct |
16 |
Correct |
170 ms |
14384 KB |
Output is correct |
17 |
Correct |
169 ms |
14320 KB |
Output is correct |
18 |
Correct |
151 ms |
16024 KB |
Output is correct |
19 |
Correct |
134 ms |
13140 KB |
Output is correct |
20 |
Correct |
137 ms |
14672 KB |
Output is correct |
21 |
Correct |
163 ms |
18132 KB |
Output is correct |
22 |
Correct |
204 ms |
18192 KB |
Output is correct |
23 |
Correct |
191 ms |
17284 KB |
Output is correct |
24 |
Correct |
181 ms |
17232 KB |
Output is correct |
25 |
Correct |
185 ms |
19028 KB |
Output is correct |
26 |
Correct |
212 ms |
17688 KB |
Output is correct |
27 |
Correct |
213 ms |
17488 KB |
Output is correct |
28 |
Correct |
199 ms |
20012 KB |
Output is correct |
29 |
Correct |
165 ms |
16212 KB |
Output is correct |
30 |
Correct |
192 ms |
18172 KB |
Output is correct |
31 |
Correct |
192 ms |
18256 KB |
Output is correct |
32 |
Correct |
206 ms |
18048 KB |
Output is correct |
33 |
Correct |
192 ms |
16984 KB |
Output is correct |
34 |
Correct |
199 ms |
17132 KB |
Output is correct |
35 |
Correct |
206 ms |
17628 KB |
Output is correct |
36 |
Correct |
163 ms |
15972 KB |
Output is correct |
37 |
Correct |
228 ms |
18080 KB |
Output is correct |
38 |
Correct |
171 ms |
16212 KB |
Output is correct |
39 |
Correct |
190 ms |
17488 KB |
Output is correct |
40 |
Correct |
216 ms |
17232 KB |
Output is correct |
41 |
Correct |
49 ms |
14920 KB |
Output is correct |
42 |
Correct |
76 ms |
15064 KB |
Output is correct |
43 |
Correct |
57 ms |
14808 KB |
Output is correct |
44 |
Correct |
64 ms |
14916 KB |
Output is correct |
45 |
Correct |
50 ms |
14920 KB |
Output is correct |
46 |
Correct |
89 ms |
14916 KB |
Output is correct |
47 |
Correct |
95 ms |
14812 KB |
Output is correct |
48 |
Correct |
48 ms |
15072 KB |
Output is correct |
49 |
Correct |
74 ms |
14664 KB |
Output is correct |
50 |
Correct |
53 ms |
14920 KB |
Output is correct |
51 |
Correct |
57 ms |
15016 KB |
Output is correct |
52 |
Correct |
68 ms |
14916 KB |
Output is correct |
53 |
Correct |
56 ms |
14792 KB |
Output is correct |
54 |
Correct |
60 ms |
14812 KB |
Output is correct |
55 |
Correct |
67 ms |
14812 KB |
Output is correct |
56 |
Correct |
53 ms |
14864 KB |
Output is correct |
57 |
Correct |
56 ms |
15052 KB |
Output is correct |
58 |
Correct |
51 ms |
14916 KB |
Output is correct |
59 |
Correct |
54 ms |
14920 KB |
Output is correct |
60 |
Correct |
56 ms |
14808 KB |
Output is correct |
61 |
Correct |
81 ms |
14800 KB |
Output is correct |
62 |
Correct |
63 ms |
14920 KB |
Output is correct |
63 |
Correct |
71 ms |
14920 KB |
Output is correct |
64 |
Correct |
49 ms |
14680 KB |
Output is correct |
65 |
Correct |
56 ms |
14936 KB |
Output is correct |
66 |
Correct |
63 ms |
14916 KB |
Output is correct |
67 |
Correct |
66 ms |
14812 KB |
Output is correct |
68 |
Correct |
61 ms |
14920 KB |
Output is correct |
69 |
Correct |
61 ms |
15012 KB |
Output is correct |
70 |
Correct |
67 ms |
14916 KB |
Output is correct |
71 |
Correct |
316 ms |
39260 KB |
Output is correct |
72 |
Correct |
509 ms |
39300 KB |
Output is correct |
73 |
Correct |
395 ms |
37840 KB |
Output is correct |
74 |
Correct |
580 ms |
38236 KB |
Output is correct |
75 |
Correct |
348 ms |
40152 KB |
Output is correct |
76 |
Correct |
552 ms |
38472 KB |
Output is correct |
77 |
Correct |
501 ms |
38472 KB |
Output is correct |
78 |
Correct |
266 ms |
42232 KB |
Output is correct |
79 |
Correct |
276 ms |
36288 KB |
Output is correct |
80 |
Correct |
344 ms |
39392 KB |
Output is correct |
81 |
Correct |
369 ms |
39128 KB |
Output is correct |
82 |
Correct |
580 ms |
38312 KB |
Output is correct |
83 |
Correct |
323 ms |
37484 KB |
Output is correct |
84 |
Correct |
492 ms |
38096 KB |
Output is correct |
85 |
Correct |
541 ms |
38284 KB |
Output is correct |
86 |
Correct |
247 ms |
36164 KB |
Output is correct |
87 |
Correct |
289 ms |
39236 KB |
Output is correct |
88 |
Correct |
303 ms |
36172 KB |
Output is correct |
89 |
Correct |
410 ms |
37960 KB |
Output is correct |
90 |
Correct |
400 ms |
38284 KB |
Output is correct |
91 |
Correct |
286 ms |
37448 KB |
Output is correct |
92 |
Correct |
502 ms |
38624 KB |
Output is correct |
93 |
Correct |
439 ms |
38744 KB |
Output is correct |
94 |
Correct |
246 ms |
36164 KB |
Output is correct |
95 |
Correct |
435 ms |
38348 KB |
Output is correct |
96 |
Correct |
424 ms |
38364 KB |
Output is correct |
97 |
Correct |
397 ms |
38212 KB |
Output is correct |
98 |
Correct |
398 ms |
38216 KB |
Output is correct |
99 |
Correct |
400 ms |
38256 KB |
Output is correct |
100 |
Correct |
388 ms |
38208 KB |
Output is correct |