#include<iostream>
#include<algorithm>
#include<string>
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
const int N = 1 << 20;
int l, q;
string s;
int pw[20 + 1], lg[N];
int a[N], f[N], g[N]; // f chứa con (1 = 01), g chứa cha (0 = 01)
void prep() {
pw[0] = 1;
for (int i = 1; i <= l; ++i) pw[i] = pw[i - 1] << 1;
for (int msk = 0; msk < pw[l]; ++msk) {
lg[msk] = lg[msk >> 1] + (msk & 1);
a[msk] = f[msk] = g[msk] = s[msk] ^ 48;
}
for (int j = 0; j < l; ++j)
for (int msk = 0; msk < pw[l]; ++msk)
if (msk >> j & 1)
f[msk] += f[msk ^ (1 << j)];
else g[msk] += g[msk | (1 << j)];
}
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> l >> q >> s;
prep();
while (q--) {
string t; cin >> t;
int m0(0), m1(0), m2(0);
for (char c : t) {
m0 <<= 1, m1 <<= 1, m2 <<= 1;
if (c == '0') ++m0;
else if (c == '1') ++m1;
else ++m2;
}
int ans(0);
if (m2 <= l / 3) { // brute ?
for (int m = m2; true; m = (m - 1) & m2) {
ans += a[m | m1];
if (m == 0) break;
}
}
else if (m0 == l / 3) { // inc-exc 0, dp sos 1 and ? => dùng g
for (int m = m0; true; m = (m - 1) & m0) {
if (lg[m] & 1)
ans -= g[m | m1];
else ans += g[m | m1];
if (m == 0) break;
}
}
else { // inc-exc 1, dp sos 0 and ? => dùng f
for (int m = m1, k = lg[m1] & 1; true; m = (m - 1) & m1) {
if ((lg[m] & 1) == k)
ans += f[m | m2];
else ans -= f[m | m2];
if (m == 0) break;
}
}
cout << ans << '\n';
}
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
224 ms |
6348 KB |
Output is correct |
12 |
Correct |
188 ms |
6080 KB |
Output is correct |
13 |
Correct |
212 ms |
5312 KB |
Output is correct |
14 |
Correct |
215 ms |
5572 KB |
Output is correct |
15 |
Correct |
189 ms |
6328 KB |
Output is correct |
16 |
Correct |
237 ms |
5752 KB |
Output is correct |
17 |
Correct |
253 ms |
5652 KB |
Output is correct |
18 |
Correct |
152 ms |
7440 KB |
Output is correct |
19 |
Correct |
176 ms |
4280 KB |
Output is correct |
20 |
Correct |
291 ms |
5956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
224 ms |
6348 KB |
Output is correct |
12 |
Correct |
188 ms |
6080 KB |
Output is correct |
13 |
Correct |
212 ms |
5312 KB |
Output is correct |
14 |
Correct |
215 ms |
5572 KB |
Output is correct |
15 |
Correct |
189 ms |
6328 KB |
Output is correct |
16 |
Correct |
237 ms |
5752 KB |
Output is correct |
17 |
Correct |
253 ms |
5652 KB |
Output is correct |
18 |
Correct |
152 ms |
7440 KB |
Output is correct |
19 |
Correct |
176 ms |
4280 KB |
Output is correct |
20 |
Correct |
291 ms |
5956 KB |
Output is correct |
21 |
Correct |
300 ms |
6540 KB |
Output is correct |
22 |
Correct |
211 ms |
6416 KB |
Output is correct |
23 |
Correct |
268 ms |
5420 KB |
Output is correct |
24 |
Correct |
246 ms |
5176 KB |
Output is correct |
25 |
Correct |
218 ms |
7276 KB |
Output is correct |
26 |
Correct |
346 ms |
5784 KB |
Output is correct |
27 |
Correct |
307 ms |
5700 KB |
Output is correct |
28 |
Correct |
162 ms |
8276 KB |
Output is correct |
29 |
Correct |
197 ms |
4228 KB |
Output is correct |
30 |
Correct |
562 ms |
6560 KB |
Output is correct |
31 |
Correct |
268 ms |
6260 KB |
Output is correct |
32 |
Correct |
260 ms |
6212 KB |
Output is correct |
33 |
Correct |
654 ms |
5180 KB |
Output is correct |
34 |
Correct |
309 ms |
5224 KB |
Output is correct |
35 |
Correct |
309 ms |
5696 KB |
Output is correct |
36 |
Correct |
175 ms |
4204 KB |
Output is correct |
37 |
Correct |
202 ms |
6244 KB |
Output is correct |
38 |
Correct |
218 ms |
4268 KB |
Output is correct |
39 |
Correct |
325 ms |
5572 KB |
Output is correct |
40 |
Correct |
269 ms |
5272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
212 ms |
18952 KB |
Output is correct |
12 |
Correct |
43 ms |
18976 KB |
Output is correct |
13 |
Correct |
55 ms |
18892 KB |
Output is correct |
14 |
Correct |
53 ms |
18916 KB |
Output is correct |
15 |
Correct |
44 ms |
18972 KB |
Output is correct |
16 |
Correct |
113 ms |
18916 KB |
Output is correct |
17 |
Correct |
96 ms |
18916 KB |
Output is correct |
18 |
Correct |
43 ms |
19104 KB |
Output is correct |
19 |
Correct |
49 ms |
18784 KB |
Output is correct |
20 |
Correct |
485 ms |
18920 KB |
Output is correct |
21 |
Correct |
53 ms |
18916 KB |
Output is correct |
22 |
Correct |
71 ms |
18896 KB |
Output is correct |
23 |
Correct |
1255 ms |
18984 KB |
Output is correct |
24 |
Correct |
116 ms |
19176 KB |
Output is correct |
25 |
Correct |
98 ms |
18972 KB |
Output is correct |
26 |
Correct |
41 ms |
18772 KB |
Output is correct |
27 |
Correct |
44 ms |
18904 KB |
Output is correct |
28 |
Correct |
49 ms |
18760 KB |
Output is correct |
29 |
Correct |
357 ms |
18916 KB |
Output is correct |
30 |
Correct |
75 ms |
18916 KB |
Output is correct |
31 |
Correct |
61 ms |
18844 KB |
Output is correct |
32 |
Correct |
59 ms |
18916 KB |
Output is correct |
33 |
Correct |
126 ms |
18976 KB |
Output is correct |
34 |
Correct |
47 ms |
18852 KB |
Output is correct |
35 |
Correct |
202 ms |
18928 KB |
Output is correct |
36 |
Correct |
153 ms |
18968 KB |
Output is correct |
37 |
Correct |
191 ms |
18936 KB |
Output is correct |
38 |
Correct |
166 ms |
18960 KB |
Output is correct |
39 |
Correct |
194 ms |
18940 KB |
Output is correct |
40 |
Correct |
164 ms |
18956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
224 ms |
6348 KB |
Output is correct |
12 |
Correct |
188 ms |
6080 KB |
Output is correct |
13 |
Correct |
212 ms |
5312 KB |
Output is correct |
14 |
Correct |
215 ms |
5572 KB |
Output is correct |
15 |
Correct |
189 ms |
6328 KB |
Output is correct |
16 |
Correct |
237 ms |
5752 KB |
Output is correct |
17 |
Correct |
253 ms |
5652 KB |
Output is correct |
18 |
Correct |
152 ms |
7440 KB |
Output is correct |
19 |
Correct |
176 ms |
4280 KB |
Output is correct |
20 |
Correct |
291 ms |
5956 KB |
Output is correct |
21 |
Correct |
300 ms |
6540 KB |
Output is correct |
22 |
Correct |
211 ms |
6416 KB |
Output is correct |
23 |
Correct |
268 ms |
5420 KB |
Output is correct |
24 |
Correct |
246 ms |
5176 KB |
Output is correct |
25 |
Correct |
218 ms |
7276 KB |
Output is correct |
26 |
Correct |
346 ms |
5784 KB |
Output is correct |
27 |
Correct |
307 ms |
5700 KB |
Output is correct |
28 |
Correct |
162 ms |
8276 KB |
Output is correct |
29 |
Correct |
197 ms |
4228 KB |
Output is correct |
30 |
Correct |
562 ms |
6560 KB |
Output is correct |
31 |
Correct |
268 ms |
6260 KB |
Output is correct |
32 |
Correct |
260 ms |
6212 KB |
Output is correct |
33 |
Correct |
654 ms |
5180 KB |
Output is correct |
34 |
Correct |
309 ms |
5224 KB |
Output is correct |
35 |
Correct |
309 ms |
5696 KB |
Output is correct |
36 |
Correct |
175 ms |
4204 KB |
Output is correct |
37 |
Correct |
202 ms |
6244 KB |
Output is correct |
38 |
Correct |
218 ms |
4268 KB |
Output is correct |
39 |
Correct |
325 ms |
5572 KB |
Output is correct |
40 |
Correct |
269 ms |
5272 KB |
Output is correct |
41 |
Correct |
212 ms |
18952 KB |
Output is correct |
42 |
Correct |
43 ms |
18976 KB |
Output is correct |
43 |
Correct |
55 ms |
18892 KB |
Output is correct |
44 |
Correct |
53 ms |
18916 KB |
Output is correct |
45 |
Correct |
44 ms |
18972 KB |
Output is correct |
46 |
Correct |
113 ms |
18916 KB |
Output is correct |
47 |
Correct |
96 ms |
18916 KB |
Output is correct |
48 |
Correct |
43 ms |
19104 KB |
Output is correct |
49 |
Correct |
49 ms |
18784 KB |
Output is correct |
50 |
Correct |
485 ms |
18920 KB |
Output is correct |
51 |
Correct |
53 ms |
18916 KB |
Output is correct |
52 |
Correct |
71 ms |
18896 KB |
Output is correct |
53 |
Correct |
1255 ms |
18984 KB |
Output is correct |
54 |
Correct |
116 ms |
19176 KB |
Output is correct |
55 |
Correct |
98 ms |
18972 KB |
Output is correct |
56 |
Correct |
41 ms |
18772 KB |
Output is correct |
57 |
Correct |
44 ms |
18904 KB |
Output is correct |
58 |
Correct |
49 ms |
18760 KB |
Output is correct |
59 |
Correct |
357 ms |
18916 KB |
Output is correct |
60 |
Correct |
75 ms |
18916 KB |
Output is correct |
61 |
Correct |
61 ms |
18844 KB |
Output is correct |
62 |
Correct |
59 ms |
18916 KB |
Output is correct |
63 |
Correct |
126 ms |
18976 KB |
Output is correct |
64 |
Correct |
47 ms |
18852 KB |
Output is correct |
65 |
Correct |
202 ms |
18928 KB |
Output is correct |
66 |
Correct |
153 ms |
18968 KB |
Output is correct |
67 |
Correct |
191 ms |
18936 KB |
Output is correct |
68 |
Correct |
166 ms |
18960 KB |
Output is correct |
69 |
Correct |
194 ms |
18940 KB |
Output is correct |
70 |
Correct |
164 ms |
18956 KB |
Output is correct |
71 |
Execution timed out |
2004 ms |
21816 KB |
Time limit exceeded |
72 |
Halted |
0 ms |
0 KB |
- |