#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 |
328 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 |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 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 |
328 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 |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
200 ms |
5864 KB |
Output is correct |
12 |
Correct |
193 ms |
5572 KB |
Output is correct |
13 |
Correct |
228 ms |
4864 KB |
Output is correct |
14 |
Correct |
220 ms |
4772 KB |
Output is correct |
15 |
Correct |
210 ms |
5988 KB |
Output is correct |
16 |
Correct |
235 ms |
5068 KB |
Output is correct |
17 |
Correct |
236 ms |
5064 KB |
Output is correct |
18 |
Correct |
155 ms |
6852 KB |
Output is correct |
19 |
Correct |
190 ms |
3912 KB |
Output is correct |
20 |
Correct |
199 ms |
5488 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 |
328 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 |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
200 ms |
5864 KB |
Output is correct |
12 |
Correct |
193 ms |
5572 KB |
Output is correct |
13 |
Correct |
228 ms |
4864 KB |
Output is correct |
14 |
Correct |
220 ms |
4772 KB |
Output is correct |
15 |
Correct |
210 ms |
5988 KB |
Output is correct |
16 |
Correct |
235 ms |
5068 KB |
Output is correct |
17 |
Correct |
236 ms |
5064 KB |
Output is correct |
18 |
Correct |
155 ms |
6852 KB |
Output is correct |
19 |
Correct |
190 ms |
3912 KB |
Output is correct |
20 |
Correct |
199 ms |
5488 KB |
Output is correct |
21 |
Correct |
223 ms |
5912 KB |
Output is correct |
22 |
Correct |
225 ms |
6184 KB |
Output is correct |
23 |
Correct |
265 ms |
5100 KB |
Output is correct |
24 |
Correct |
264 ms |
4928 KB |
Output is correct |
25 |
Correct |
218 ms |
7032 KB |
Output is correct |
26 |
Correct |
299 ms |
5504 KB |
Output is correct |
27 |
Correct |
300 ms |
5408 KB |
Output is correct |
28 |
Correct |
170 ms |
7976 KB |
Output is correct |
29 |
Correct |
206 ms |
3992 KB |
Output is correct |
30 |
Correct |
250 ms |
6116 KB |
Output is correct |
31 |
Correct |
265 ms |
5956 KB |
Output is correct |
32 |
Correct |
276 ms |
6032 KB |
Output is correct |
33 |
Correct |
562 ms |
5008 KB |
Output is correct |
34 |
Correct |
301 ms |
4988 KB |
Output is correct |
35 |
Correct |
312 ms |
5484 KB |
Output is correct |
36 |
Correct |
174 ms |
3912 KB |
Output is correct |
37 |
Correct |
202 ms |
6020 KB |
Output is correct |
38 |
Correct |
214 ms |
4028 KB |
Output is correct |
39 |
Correct |
331 ms |
5148 KB |
Output is correct |
40 |
Correct |
280 ms |
5124 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 |
328 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 |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
48 ms |
19244 KB |
Output is correct |
12 |
Correct |
49 ms |
19280 KB |
Output is correct |
13 |
Correct |
57 ms |
19208 KB |
Output is correct |
14 |
Correct |
60 ms |
19176 KB |
Output is correct |
15 |
Correct |
48 ms |
19272 KB |
Output is correct |
16 |
Correct |
107 ms |
19268 KB |
Output is correct |
17 |
Correct |
102 ms |
19180 KB |
Output is correct |
18 |
Correct |
41 ms |
19360 KB |
Output is correct |
19 |
Correct |
47 ms |
19060 KB |
Output is correct |
20 |
Correct |
48 ms |
19172 KB |
Output is correct |
21 |
Correct |
57 ms |
19168 KB |
Output is correct |
22 |
Correct |
76 ms |
19172 KB |
Output is correct |
23 |
Correct |
1404 ms |
19284 KB |
Output is correct |
24 |
Correct |
109 ms |
19196 KB |
Output is correct |
25 |
Correct |
108 ms |
19328 KB |
Output is correct |
26 |
Correct |
43 ms |
19044 KB |
Output is correct |
27 |
Correct |
49 ms |
19172 KB |
Output is correct |
28 |
Correct |
49 ms |
19112 KB |
Output is correct |
29 |
Correct |
234 ms |
19176 KB |
Output is correct |
30 |
Correct |
69 ms |
19128 KB |
Output is correct |
31 |
Correct |
48 ms |
19172 KB |
Output is correct |
32 |
Correct |
66 ms |
19240 KB |
Output is correct |
33 |
Correct |
103 ms |
19192 KB |
Output is correct |
34 |
Correct |
41 ms |
19048 KB |
Output is correct |
35 |
Correct |
138 ms |
19104 KB |
Output is correct |
36 |
Correct |
114 ms |
19144 KB |
Output is correct |
37 |
Correct |
123 ms |
19212 KB |
Output is correct |
38 |
Correct |
116 ms |
19184 KB |
Output is correct |
39 |
Correct |
118 ms |
19204 KB |
Output is correct |
40 |
Correct |
119 ms |
19128 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 |
328 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 |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
200 ms |
5864 KB |
Output is correct |
12 |
Correct |
193 ms |
5572 KB |
Output is correct |
13 |
Correct |
228 ms |
4864 KB |
Output is correct |
14 |
Correct |
220 ms |
4772 KB |
Output is correct |
15 |
Correct |
210 ms |
5988 KB |
Output is correct |
16 |
Correct |
235 ms |
5068 KB |
Output is correct |
17 |
Correct |
236 ms |
5064 KB |
Output is correct |
18 |
Correct |
155 ms |
6852 KB |
Output is correct |
19 |
Correct |
190 ms |
3912 KB |
Output is correct |
20 |
Correct |
199 ms |
5488 KB |
Output is correct |
21 |
Correct |
223 ms |
5912 KB |
Output is correct |
22 |
Correct |
225 ms |
6184 KB |
Output is correct |
23 |
Correct |
265 ms |
5100 KB |
Output is correct |
24 |
Correct |
264 ms |
4928 KB |
Output is correct |
25 |
Correct |
218 ms |
7032 KB |
Output is correct |
26 |
Correct |
299 ms |
5504 KB |
Output is correct |
27 |
Correct |
300 ms |
5408 KB |
Output is correct |
28 |
Correct |
170 ms |
7976 KB |
Output is correct |
29 |
Correct |
206 ms |
3992 KB |
Output is correct |
30 |
Correct |
250 ms |
6116 KB |
Output is correct |
31 |
Correct |
265 ms |
5956 KB |
Output is correct |
32 |
Correct |
276 ms |
6032 KB |
Output is correct |
33 |
Correct |
562 ms |
5008 KB |
Output is correct |
34 |
Correct |
301 ms |
4988 KB |
Output is correct |
35 |
Correct |
312 ms |
5484 KB |
Output is correct |
36 |
Correct |
174 ms |
3912 KB |
Output is correct |
37 |
Correct |
202 ms |
6020 KB |
Output is correct |
38 |
Correct |
214 ms |
4028 KB |
Output is correct |
39 |
Correct |
331 ms |
5148 KB |
Output is correct |
40 |
Correct |
280 ms |
5124 KB |
Output is correct |
41 |
Correct |
48 ms |
19244 KB |
Output is correct |
42 |
Correct |
49 ms |
19280 KB |
Output is correct |
43 |
Correct |
57 ms |
19208 KB |
Output is correct |
44 |
Correct |
60 ms |
19176 KB |
Output is correct |
45 |
Correct |
48 ms |
19272 KB |
Output is correct |
46 |
Correct |
107 ms |
19268 KB |
Output is correct |
47 |
Correct |
102 ms |
19180 KB |
Output is correct |
48 |
Correct |
41 ms |
19360 KB |
Output is correct |
49 |
Correct |
47 ms |
19060 KB |
Output is correct |
50 |
Correct |
48 ms |
19172 KB |
Output is correct |
51 |
Correct |
57 ms |
19168 KB |
Output is correct |
52 |
Correct |
76 ms |
19172 KB |
Output is correct |
53 |
Correct |
1404 ms |
19284 KB |
Output is correct |
54 |
Correct |
109 ms |
19196 KB |
Output is correct |
55 |
Correct |
108 ms |
19328 KB |
Output is correct |
56 |
Correct |
43 ms |
19044 KB |
Output is correct |
57 |
Correct |
49 ms |
19172 KB |
Output is correct |
58 |
Correct |
49 ms |
19112 KB |
Output is correct |
59 |
Correct |
234 ms |
19176 KB |
Output is correct |
60 |
Correct |
69 ms |
19128 KB |
Output is correct |
61 |
Correct |
48 ms |
19172 KB |
Output is correct |
62 |
Correct |
66 ms |
19240 KB |
Output is correct |
63 |
Correct |
103 ms |
19192 KB |
Output is correct |
64 |
Correct |
41 ms |
19048 KB |
Output is correct |
65 |
Correct |
138 ms |
19104 KB |
Output is correct |
66 |
Correct |
114 ms |
19144 KB |
Output is correct |
67 |
Correct |
123 ms |
19212 KB |
Output is correct |
68 |
Correct |
116 ms |
19184 KB |
Output is correct |
69 |
Correct |
118 ms |
19204 KB |
Output is correct |
70 |
Correct |
119 ms |
19128 KB |
Output is correct |
71 |
Correct |
343 ms |
30136 KB |
Output is correct |
72 |
Correct |
334 ms |
25092 KB |
Output is correct |
73 |
Correct |
554 ms |
24788 KB |
Output is correct |
74 |
Correct |
726 ms |
24804 KB |
Output is correct |
75 |
Correct |
356 ms |
25992 KB |
Output is correct |
76 |
Correct |
1281 ms |
24296 KB |
Output is correct |
77 |
Correct |
1694 ms |
24428 KB |
Output is correct |
78 |
Correct |
226 ms |
28004 KB |
Output is correct |
79 |
Correct |
319 ms |
22048 KB |
Output is correct |
80 |
Correct |
336 ms |
25068 KB |
Output is correct |
81 |
Correct |
487 ms |
25112 KB |
Output is correct |
82 |
Correct |
721 ms |
24000 KB |
Output is correct |
83 |
Execution timed out |
2064 ms |
20348 KB |
Time limit exceeded |
84 |
Halted |
0 ms |
0 KB |
- |