#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 1e6 + 2;
int f[1 << 20];
int ans[N];
unsigned char type[N]; // 255 if ?, otherwise 0/1 for majority
long long masks[N];
int get_mask(const string& s, char target)
{
int mask = 0;
for (auto ch : s) {
mask <<= 1;
mask |= ch == target;
}
return mask;
}
int main()
{
cin.tie(NULL); ios_base::sync_with_stdio(false);
int l, q; cin >> l >> q;
string toxicity; cin >> toxicity;
for (int i = 0; i < q; ++i) {
string s; cin >> s;
long long maskQ = get_mask(s, '?');
long long mask0 = get_mask(s, '0');
long long mask1 = get_mask(s, '1');
masks[i] = (maskQ << 40) | (mask1 << 20) | mask0;
int cnt[3] = {};
for (auto ch : s)
++cnt[ch == '?' ? 2 : ch - '0'];
int mn = *min_element(cnt, cnt + 3);
if (cnt[2] == mn) {
int maskQ = masks[i] >> 40;
int mask1 = (masks[i] >> 20) & ((1 << 20) - 1);
for (int submask = maskQ; submask > 0; submask = (submask - 1) & maskQ)
ans[i] += toxicity[mask1 | submask] - '0';
ans[i] += toxicity[mask1] - '0'; // all 0
type[i] = 255;
}
else
type[i] = cnt[0] > cnt[1];
}
transform(toxicity.begin(), toxicity.end(), f, [](char ch) { return ch - '0'; });
for (int b = 0; b < l; ++b)
for (int i = 0; i < 1 << l; ++i)
if ((i >> b) & 1)
f[i] += f[i ^ (1 << b)];
for (int i = 0; i < q; ++i)
if (type[i] == 1) {
int maskQ = masks[i] >> 40;
int mask1 = (masks[i] >> 20) & ((1 << 20) - 1);
for (int submask = mask1; submask > 0; submask = (submask - 1) & mask1) // mask of 0 within 1
ans[i] += f[maskQ | (mask1 ^ submask)] * (__builtin_popcount(submask) & 1 ? -1 : 1);
ans[i] += f[maskQ | mask1]; // base: all 1 are ?
}
transform(toxicity.begin(), toxicity.end(), f, [](char ch) { return ch - '0'; });
for (int b = 0; b < l; ++b)
for (int i = (1 << l) - 1; i >= 0; --i)
if (!((i >> b) & 1))
f[i] += f[i ^ (1 << b)];
int fullmask = (1 << l) - 1;
for (int i = 0; i < q; ++i)
if (type[i] == 0) {
int maskQ = masks[i] >> 40;
int mask0 = masks[i] & ((1 << 20) - 1);
for (int submask = mask0; submask > 0; submask = (submask - 1) & mask0) // mask of 1 within 0
ans[i] += f[fullmask ^ (maskQ | (mask0 ^ submask))] * (__builtin_popcount(submask) & 1 ? -1 : 1);
ans[i] += f[fullmask ^ (maskQ | mask0)]; // base: all 0 are ?
}
for (int i = 0; i < q; ++i) cout << ans[i] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 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 |
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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 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 |
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 |
210 ms |
16980 KB |
Output is correct |
12 |
Correct |
208 ms |
16724 KB |
Output is correct |
13 |
Correct |
230 ms |
15840 KB |
Output is correct |
14 |
Correct |
199 ms |
15972 KB |
Output is correct |
15 |
Correct |
218 ms |
17072 KB |
Output is correct |
16 |
Correct |
236 ms |
16240 KB |
Output is correct |
17 |
Correct |
215 ms |
16144 KB |
Output is correct |
18 |
Correct |
215 ms |
17988 KB |
Output is correct |
19 |
Correct |
199 ms |
14980 KB |
Output is correct |
20 |
Correct |
215 ms |
16796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 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 |
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 |
210 ms |
16980 KB |
Output is correct |
12 |
Correct |
208 ms |
16724 KB |
Output is correct |
13 |
Correct |
230 ms |
15840 KB |
Output is correct |
14 |
Correct |
199 ms |
15972 KB |
Output is correct |
15 |
Correct |
218 ms |
17072 KB |
Output is correct |
16 |
Correct |
236 ms |
16240 KB |
Output is correct |
17 |
Correct |
215 ms |
16144 KB |
Output is correct |
18 |
Correct |
215 ms |
17988 KB |
Output is correct |
19 |
Correct |
199 ms |
14980 KB |
Output is correct |
20 |
Correct |
215 ms |
16796 KB |
Output is correct |
21 |
Correct |
231 ms |
16976 KB |
Output is correct |
22 |
Correct |
228 ms |
17172 KB |
Output is correct |
23 |
Correct |
245 ms |
16164 KB |
Output is correct |
24 |
Correct |
240 ms |
16068 KB |
Output is correct |
25 |
Correct |
251 ms |
18060 KB |
Output is correct |
26 |
Correct |
290 ms |
16592 KB |
Output is correct |
27 |
Correct |
247 ms |
16548 KB |
Output is correct |
28 |
Correct |
224 ms |
19012 KB |
Output is correct |
29 |
Correct |
222 ms |
15064 KB |
Output is correct |
30 |
Correct |
241 ms |
17184 KB |
Output is correct |
31 |
Correct |
250 ms |
17088 KB |
Output is correct |
32 |
Correct |
279 ms |
17092 KB |
Output is correct |
33 |
Correct |
248 ms |
15872 KB |
Output is correct |
34 |
Correct |
256 ms |
16068 KB |
Output is correct |
35 |
Correct |
254 ms |
16540 KB |
Output is correct |
36 |
Correct |
209 ms |
15084 KB |
Output is correct |
37 |
Correct |
229 ms |
17144 KB |
Output is correct |
38 |
Correct |
224 ms |
15036 KB |
Output is correct |
39 |
Correct |
254 ms |
16388 KB |
Output is correct |
40 |
Correct |
227 ms |
16024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 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 |
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 |
66 ms |
6400 KB |
Output is correct |
12 |
Correct |
64 ms |
6576 KB |
Output is correct |
13 |
Correct |
58 ms |
6352 KB |
Output is correct |
14 |
Correct |
69 ms |
6324 KB |
Output is correct |
15 |
Correct |
58 ms |
6428 KB |
Output is correct |
16 |
Correct |
71 ms |
6404 KB |
Output is correct |
17 |
Correct |
72 ms |
6480 KB |
Output is correct |
18 |
Correct |
57 ms |
6544 KB |
Output is correct |
19 |
Correct |
55 ms |
6228 KB |
Output is correct |
20 |
Correct |
56 ms |
6432 KB |
Output is correct |
21 |
Correct |
57 ms |
6352 KB |
Output is correct |
22 |
Correct |
74 ms |
6372 KB |
Output is correct |
23 |
Correct |
55 ms |
6356 KB |
Output is correct |
24 |
Correct |
66 ms |
6352 KB |
Output is correct |
25 |
Correct |
69 ms |
6352 KB |
Output is correct |
26 |
Correct |
59 ms |
6252 KB |
Output is correct |
27 |
Correct |
55 ms |
6376 KB |
Output is correct |
28 |
Correct |
57 ms |
6248 KB |
Output is correct |
29 |
Correct |
57 ms |
6372 KB |
Output is correct |
30 |
Correct |
60 ms |
6384 KB |
Output is correct |
31 |
Correct |
60 ms |
6392 KB |
Output is correct |
32 |
Correct |
62 ms |
6348 KB |
Output is correct |
33 |
Correct |
88 ms |
6372 KB |
Output is correct |
34 |
Correct |
61 ms |
6216 KB |
Output is correct |
35 |
Correct |
58 ms |
6372 KB |
Output is correct |
36 |
Correct |
61 ms |
6356 KB |
Output is correct |
37 |
Correct |
66 ms |
6352 KB |
Output is correct |
38 |
Correct |
72 ms |
6376 KB |
Output is correct |
39 |
Correct |
68 ms |
6352 KB |
Output is correct |
40 |
Correct |
62 ms |
6372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 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 |
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 |
210 ms |
16980 KB |
Output is correct |
12 |
Correct |
208 ms |
16724 KB |
Output is correct |
13 |
Correct |
230 ms |
15840 KB |
Output is correct |
14 |
Correct |
199 ms |
15972 KB |
Output is correct |
15 |
Correct |
218 ms |
17072 KB |
Output is correct |
16 |
Correct |
236 ms |
16240 KB |
Output is correct |
17 |
Correct |
215 ms |
16144 KB |
Output is correct |
18 |
Correct |
215 ms |
17988 KB |
Output is correct |
19 |
Correct |
199 ms |
14980 KB |
Output is correct |
20 |
Correct |
215 ms |
16796 KB |
Output is correct |
21 |
Correct |
231 ms |
16976 KB |
Output is correct |
22 |
Correct |
228 ms |
17172 KB |
Output is correct |
23 |
Correct |
245 ms |
16164 KB |
Output is correct |
24 |
Correct |
240 ms |
16068 KB |
Output is correct |
25 |
Correct |
251 ms |
18060 KB |
Output is correct |
26 |
Correct |
290 ms |
16592 KB |
Output is correct |
27 |
Correct |
247 ms |
16548 KB |
Output is correct |
28 |
Correct |
224 ms |
19012 KB |
Output is correct |
29 |
Correct |
222 ms |
15064 KB |
Output is correct |
30 |
Correct |
241 ms |
17184 KB |
Output is correct |
31 |
Correct |
250 ms |
17088 KB |
Output is correct |
32 |
Correct |
279 ms |
17092 KB |
Output is correct |
33 |
Correct |
248 ms |
15872 KB |
Output is correct |
34 |
Correct |
256 ms |
16068 KB |
Output is correct |
35 |
Correct |
254 ms |
16540 KB |
Output is correct |
36 |
Correct |
209 ms |
15084 KB |
Output is correct |
37 |
Correct |
229 ms |
17144 KB |
Output is correct |
38 |
Correct |
224 ms |
15036 KB |
Output is correct |
39 |
Correct |
254 ms |
16388 KB |
Output is correct |
40 |
Correct |
227 ms |
16024 KB |
Output is correct |
41 |
Correct |
66 ms |
6400 KB |
Output is correct |
42 |
Correct |
64 ms |
6576 KB |
Output is correct |
43 |
Correct |
58 ms |
6352 KB |
Output is correct |
44 |
Correct |
69 ms |
6324 KB |
Output is correct |
45 |
Correct |
58 ms |
6428 KB |
Output is correct |
46 |
Correct |
71 ms |
6404 KB |
Output is correct |
47 |
Correct |
72 ms |
6480 KB |
Output is correct |
48 |
Correct |
57 ms |
6544 KB |
Output is correct |
49 |
Correct |
55 ms |
6228 KB |
Output is correct |
50 |
Correct |
56 ms |
6432 KB |
Output is correct |
51 |
Correct |
57 ms |
6352 KB |
Output is correct |
52 |
Correct |
74 ms |
6372 KB |
Output is correct |
53 |
Correct |
55 ms |
6356 KB |
Output is correct |
54 |
Correct |
66 ms |
6352 KB |
Output is correct |
55 |
Correct |
69 ms |
6352 KB |
Output is correct |
56 |
Correct |
59 ms |
6252 KB |
Output is correct |
57 |
Correct |
55 ms |
6376 KB |
Output is correct |
58 |
Correct |
57 ms |
6248 KB |
Output is correct |
59 |
Correct |
57 ms |
6372 KB |
Output is correct |
60 |
Correct |
60 ms |
6384 KB |
Output is correct |
61 |
Correct |
60 ms |
6392 KB |
Output is correct |
62 |
Correct |
62 ms |
6348 KB |
Output is correct |
63 |
Correct |
88 ms |
6372 KB |
Output is correct |
64 |
Correct |
61 ms |
6216 KB |
Output is correct |
65 |
Correct |
58 ms |
6372 KB |
Output is correct |
66 |
Correct |
61 ms |
6356 KB |
Output is correct |
67 |
Correct |
66 ms |
6352 KB |
Output is correct |
68 |
Correct |
72 ms |
6376 KB |
Output is correct |
69 |
Correct |
68 ms |
6352 KB |
Output is correct |
70 |
Correct |
62 ms |
6372 KB |
Output is correct |
71 |
Correct |
381 ms |
44600 KB |
Output is correct |
72 |
Correct |
383 ms |
44976 KB |
Output is correct |
73 |
Correct |
435 ms |
43560 KB |
Output is correct |
74 |
Correct |
667 ms |
43936 KB |
Output is correct |
75 |
Correct |
404 ms |
45964 KB |
Output is correct |
76 |
Correct |
571 ms |
44376 KB |
Output is correct |
77 |
Correct |
512 ms |
44000 KB |
Output is correct |
78 |
Correct |
365 ms |
47960 KB |
Output is correct |
79 |
Correct |
357 ms |
41896 KB |
Output is correct |
80 |
Correct |
362 ms |
44936 KB |
Output is correct |
81 |
Correct |
459 ms |
44944 KB |
Output is correct |
82 |
Correct |
686 ms |
43892 KB |
Output is correct |
83 |
Correct |
434 ms |
43012 KB |
Output is correct |
84 |
Correct |
511 ms |
43836 KB |
Output is correct |
85 |
Correct |
495 ms |
44040 KB |
Output is correct |
86 |
Correct |
354 ms |
41816 KB |
Output is correct |
87 |
Correct |
396 ms |
44924 KB |
Output is correct |
88 |
Correct |
383 ms |
41820 KB |
Output is correct |
89 |
Correct |
435 ms |
43440 KB |
Output is correct |
90 |
Correct |
502 ms |
43824 KB |
Output is correct |
91 |
Correct |
408 ms |
42932 KB |
Output is correct |
92 |
Correct |
621 ms |
44088 KB |
Output is correct |
93 |
Correct |
527 ms |
44004 KB |
Output is correct |
94 |
Correct |
374 ms |
41844 KB |
Output is correct |
95 |
Correct |
462 ms |
44040 KB |
Output is correct |
96 |
Correct |
496 ms |
43928 KB |
Output is correct |
97 |
Correct |
508 ms |
43920 KB |
Output is correct |
98 |
Correct |
468 ms |
43884 KB |
Output is correct |
99 |
Correct |
477 ms |
44020 KB |
Output is correct |
100 |
Correct |
474 ms |
43932 KB |
Output is correct |