/*
Siêu tóm tắt: tổng của tất cả các mask theo yêu cầu
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 1e6 + 5, L = 21, LMX = (1 << 21) + 5;
const int oo = 1e18, mod = 1e9 + 7;
int n, m, k, l, q, a[LMX], f[LMX], g[LMX];
string s;
vector<int> arr;
void process() {
cin >> l >> q >> s;
m = (1 << l);
for (int i = 0; i < m; ++i) {
a[i] = s[i] - '0';
}
for (int i = 0; i < m; ++i) {
f[i] = a[i];
g[i] = a[i];
}
for (int j = 0; j < l; ++j) {
for (int i = 0; i < m; ++i) {
if ((i >> j) & 1) {
f[i] += f[i ^ (1 << j)];
g[i ^ (1 << j)] += g[i];
}
}
}
while (q--) {
vector<int> t(3, 0);
string tt;
cin >> tt;
for (int i = l - 1; i >= 0; --i) {
t[tt[i] == '?' ? 2 : tt[i] - '0'] |= (1 << (l - i - 1));
}
if (__builtin_popcount(t[1]) <= 6) {
int ans = 0;
int ppc = __builtin_popcount(t[1]);
for (int z = t[1]; ; z = (z - 1) & t[1]) {
ans += (((ppc ^ __builtin_popcount(z)) & 1) ? -1 : 1) * f[t[2] | z];
if (z == 0) {
break;
}
}
cout << ans << "\n";
} else if (__builtin_popcount(t[0]) <= 6) {
int ans = 0;
int ppc = __builtin_popcount(t[0]);
for (int z = t[0]; ; z = (z - 1) & t[0]) {
ans += (((ppc ^ __builtin_popcount(z)) & 1) ? -1 : 1) * g[t[1] ^ t[0] ^ z];
if (z == 0) {
break;
}
}
cout << ans << "\n";
} else {
assert(__builtin_popcount(t[2]) <= 6);
int ans = 0;
for (int z = t[2]; ; z = (z - 1) & t[2]) {
ans += a[t[1] | z];
if (z == 0) {
break;
}
}
cout << ans << "\n";
}
}
return;
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4568 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4568 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
270 ms |
19128 KB |
Output is correct |
12 |
Correct |
185 ms |
18940 KB |
Output is correct |
13 |
Correct |
197 ms |
18336 KB |
Output is correct |
14 |
Correct |
187 ms |
18252 KB |
Output is correct |
15 |
Correct |
221 ms |
19308 KB |
Output is correct |
16 |
Correct |
233 ms |
18260 KB |
Output is correct |
17 |
Correct |
237 ms |
18612 KB |
Output is correct |
18 |
Correct |
150 ms |
20188 KB |
Output is correct |
19 |
Correct |
247 ms |
17308 KB |
Output is correct |
20 |
Correct |
289 ms |
18772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4568 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
270 ms |
19128 KB |
Output is correct |
12 |
Correct |
185 ms |
18940 KB |
Output is correct |
13 |
Correct |
197 ms |
18336 KB |
Output is correct |
14 |
Correct |
187 ms |
18252 KB |
Output is correct |
15 |
Correct |
221 ms |
19308 KB |
Output is correct |
16 |
Correct |
233 ms |
18260 KB |
Output is correct |
17 |
Correct |
237 ms |
18612 KB |
Output is correct |
18 |
Correct |
150 ms |
20188 KB |
Output is correct |
19 |
Correct |
247 ms |
17308 KB |
Output is correct |
20 |
Correct |
289 ms |
18772 KB |
Output is correct |
21 |
Correct |
412 ms |
22204 KB |
Output is correct |
22 |
Correct |
233 ms |
22524 KB |
Output is correct |
23 |
Correct |
244 ms |
21332 KB |
Output is correct |
24 |
Correct |
245 ms |
21300 KB |
Output is correct |
25 |
Correct |
230 ms |
23124 KB |
Output is correct |
26 |
Correct |
311 ms |
21612 KB |
Output is correct |
27 |
Correct |
279 ms |
21784 KB |
Output is correct |
28 |
Correct |
163 ms |
24196 KB |
Output is correct |
29 |
Correct |
366 ms |
20316 KB |
Output is correct |
30 |
Correct |
284 ms |
22352 KB |
Output is correct |
31 |
Correct |
266 ms |
22096 KB |
Output is correct |
32 |
Correct |
254 ms |
22288 KB |
Output is correct |
33 |
Correct |
203 ms |
21076 KB |
Output is correct |
34 |
Correct |
266 ms |
21692 KB |
Output is correct |
35 |
Correct |
289 ms |
21684 KB |
Output is correct |
36 |
Correct |
162 ms |
20160 KB |
Output is correct |
37 |
Correct |
203 ms |
22100 KB |
Output is correct |
38 |
Correct |
312 ms |
20184 KB |
Output is correct |
39 |
Correct |
388 ms |
21736 KB |
Output is correct |
40 |
Correct |
281 ms |
21484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4568 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
43 ms |
32752 KB |
Output is correct |
12 |
Correct |
43 ms |
32812 KB |
Output is correct |
13 |
Correct |
43 ms |
32760 KB |
Output is correct |
14 |
Correct |
57 ms |
32756 KB |
Output is correct |
15 |
Correct |
40 ms |
32752 KB |
Output is correct |
16 |
Correct |
74 ms |
32580 KB |
Output is correct |
17 |
Correct |
58 ms |
32752 KB |
Output is correct |
18 |
Correct |
34 ms |
32816 KB |
Output is correct |
19 |
Correct |
37 ms |
32496 KB |
Output is correct |
20 |
Correct |
39 ms |
32804 KB |
Output is correct |
21 |
Correct |
45 ms |
32568 KB |
Output is correct |
22 |
Correct |
49 ms |
32764 KB |
Output is correct |
23 |
Correct |
39 ms |
32504 KB |
Output is correct |
24 |
Correct |
61 ms |
32548 KB |
Output is correct |
25 |
Correct |
56 ms |
32752 KB |
Output is correct |
26 |
Correct |
34 ms |
32512 KB |
Output is correct |
27 |
Correct |
40 ms |
32564 KB |
Output is correct |
28 |
Correct |
47 ms |
32432 KB |
Output is correct |
29 |
Correct |
43 ms |
32500 KB |
Output is correct |
30 |
Correct |
49 ms |
32660 KB |
Output is correct |
31 |
Correct |
42 ms |
32500 KB |
Output is correct |
32 |
Correct |
63 ms |
32748 KB |
Output is correct |
33 |
Correct |
56 ms |
32752 KB |
Output is correct |
34 |
Correct |
34 ms |
32500 KB |
Output is correct |
35 |
Correct |
54 ms |
32752 KB |
Output is correct |
36 |
Correct |
55 ms |
32612 KB |
Output is correct |
37 |
Correct |
51 ms |
32552 KB |
Output is correct |
38 |
Correct |
51 ms |
32560 KB |
Output is correct |
39 |
Correct |
51 ms |
32756 KB |
Output is correct |
40 |
Correct |
60 ms |
33172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4568 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
270 ms |
19128 KB |
Output is correct |
12 |
Correct |
185 ms |
18940 KB |
Output is correct |
13 |
Correct |
197 ms |
18336 KB |
Output is correct |
14 |
Correct |
187 ms |
18252 KB |
Output is correct |
15 |
Correct |
221 ms |
19308 KB |
Output is correct |
16 |
Correct |
233 ms |
18260 KB |
Output is correct |
17 |
Correct |
237 ms |
18612 KB |
Output is correct |
18 |
Correct |
150 ms |
20188 KB |
Output is correct |
19 |
Correct |
247 ms |
17308 KB |
Output is correct |
20 |
Correct |
289 ms |
18772 KB |
Output is correct |
21 |
Correct |
412 ms |
22204 KB |
Output is correct |
22 |
Correct |
233 ms |
22524 KB |
Output is correct |
23 |
Correct |
244 ms |
21332 KB |
Output is correct |
24 |
Correct |
245 ms |
21300 KB |
Output is correct |
25 |
Correct |
230 ms |
23124 KB |
Output is correct |
26 |
Correct |
311 ms |
21612 KB |
Output is correct |
27 |
Correct |
279 ms |
21784 KB |
Output is correct |
28 |
Correct |
163 ms |
24196 KB |
Output is correct |
29 |
Correct |
366 ms |
20316 KB |
Output is correct |
30 |
Correct |
284 ms |
22352 KB |
Output is correct |
31 |
Correct |
266 ms |
22096 KB |
Output is correct |
32 |
Correct |
254 ms |
22288 KB |
Output is correct |
33 |
Correct |
203 ms |
21076 KB |
Output is correct |
34 |
Correct |
266 ms |
21692 KB |
Output is correct |
35 |
Correct |
289 ms |
21684 KB |
Output is correct |
36 |
Correct |
162 ms |
20160 KB |
Output is correct |
37 |
Correct |
203 ms |
22100 KB |
Output is correct |
38 |
Correct |
312 ms |
20184 KB |
Output is correct |
39 |
Correct |
388 ms |
21736 KB |
Output is correct |
40 |
Correct |
281 ms |
21484 KB |
Output is correct |
41 |
Correct |
43 ms |
32752 KB |
Output is correct |
42 |
Correct |
43 ms |
32812 KB |
Output is correct |
43 |
Correct |
43 ms |
32760 KB |
Output is correct |
44 |
Correct |
57 ms |
32756 KB |
Output is correct |
45 |
Correct |
40 ms |
32752 KB |
Output is correct |
46 |
Correct |
74 ms |
32580 KB |
Output is correct |
47 |
Correct |
58 ms |
32752 KB |
Output is correct |
48 |
Correct |
34 ms |
32816 KB |
Output is correct |
49 |
Correct |
37 ms |
32496 KB |
Output is correct |
50 |
Correct |
39 ms |
32804 KB |
Output is correct |
51 |
Correct |
45 ms |
32568 KB |
Output is correct |
52 |
Correct |
49 ms |
32764 KB |
Output is correct |
53 |
Correct |
39 ms |
32504 KB |
Output is correct |
54 |
Correct |
61 ms |
32548 KB |
Output is correct |
55 |
Correct |
56 ms |
32752 KB |
Output is correct |
56 |
Correct |
34 ms |
32512 KB |
Output is correct |
57 |
Correct |
40 ms |
32564 KB |
Output is correct |
58 |
Correct |
47 ms |
32432 KB |
Output is correct |
59 |
Correct |
43 ms |
32500 KB |
Output is correct |
60 |
Correct |
49 ms |
32660 KB |
Output is correct |
61 |
Correct |
42 ms |
32500 KB |
Output is correct |
62 |
Correct |
63 ms |
32748 KB |
Output is correct |
63 |
Correct |
56 ms |
32752 KB |
Output is correct |
64 |
Correct |
34 ms |
32500 KB |
Output is correct |
65 |
Correct |
54 ms |
32752 KB |
Output is correct |
66 |
Correct |
55 ms |
32612 KB |
Output is correct |
67 |
Correct |
51 ms |
32552 KB |
Output is correct |
68 |
Correct |
51 ms |
32560 KB |
Output is correct |
69 |
Correct |
51 ms |
32756 KB |
Output is correct |
70 |
Correct |
60 ms |
33172 KB |
Output is correct |
71 |
Correct |
310 ms |
57072 KB |
Output is correct |
72 |
Correct |
324 ms |
56996 KB |
Output is correct |
73 |
Correct |
411 ms |
55536 KB |
Output is correct |
74 |
Correct |
544 ms |
55852 KB |
Output is correct |
75 |
Correct |
366 ms |
57832 KB |
Output is correct |
76 |
Correct |
761 ms |
56368 KB |
Output is correct |
77 |
Correct |
881 ms |
56164 KB |
Output is correct |
78 |
Correct |
227 ms |
59892 KB |
Output is correct |
79 |
Correct |
229 ms |
54004 KB |
Output is correct |
80 |
Correct |
347 ms |
57108 KB |
Output is correct |
81 |
Correct |
499 ms |
56956 KB |
Output is correct |
82 |
Correct |
590 ms |
55908 KB |
Output is correct |
83 |
Correct |
374 ms |
55096 KB |
Output is correct |
84 |
Correct |
871 ms |
56036 KB |
Output is correct |
85 |
Correct |
772 ms |
56256 KB |
Output is correct |
86 |
Correct |
222 ms |
53812 KB |
Output is correct |
87 |
Correct |
315 ms |
56792 KB |
Output is correct |
88 |
Correct |
294 ms |
53944 KB |
Output is correct |
89 |
Correct |
429 ms |
55644 KB |
Output is correct |
90 |
Correct |
404 ms |
55972 KB |
Output is correct |
91 |
Correct |
340 ms |
55440 KB |
Output is correct |
92 |
Correct |
584 ms |
56464 KB |
Output is correct |
93 |
Correct |
730 ms |
56044 KB |
Output is correct |
94 |
Correct |
216 ms |
54000 KB |
Output is correct |
95 |
Correct |
630 ms |
55988 KB |
Output is correct |
96 |
Correct |
648 ms |
56068 KB |
Output is correct |
97 |
Correct |
557 ms |
56120 KB |
Output is correct |
98 |
Correct |
626 ms |
55948 KB |
Output is correct |
99 |
Correct |
656 ms |
56360 KB |
Output is correct |
100 |
Correct |
672 ms |
56060 KB |
Output is correct |