#include <bits/stdc++.h>
using namespace std;
void main_program();
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
main_program();
}
const int maxn = 1 << 20;
int lg, n, q;
vector<int> v;
vector<int> subsetZeta(vector<int> V){
for (int j = 0; j < lg; j++){
for (int i = 0; i < n; i++){
if (((i >> j) & 1)){
V[i] += V[i ^ (1 << j)];
}
}
}
return V;
}
vector<int> supersetZeta(vector<int> V){
for (int j = 0; j < lg; j++){
for (int i = 0; i < n; i++){
if (((i >> j) & 1) == 0){
V[i] += V[i ^ (1 << j)];
}
}
}
return V;
}
void main_program(){
cin >> lg >> q;
n = 1 << lg;
v.resize(n);
string s; cin >> s;
for (int i = 0; i < n; i++) v[i] = (s[i] - '0');
vector<int> subset = subsetZeta(v), superset = supersetZeta(v);
for (int _ = 0; _ < q; _++){
string Q; cin >> Q;
int mask0 = 0, mask1 = 0, mask2 = 0;
for (int i = 0; i < lg; i++){
if (Q[i] == '0') mask0 |= (1 << (lg-1 - i));
if (Q[i] == '1') mask1 |= (1 << (lg-1 - i));
if (Q[i] == '?') mask2 |= (1 << (lg-1 - i));
}
int popcnt0 = __builtin_popcount(mask0);
int popcnt1 = __builtin_popcount(mask1);
int popcnt2 = __builtin_popcount(mask2);
int mn = min({popcnt0, popcnt1, popcnt2});
if (popcnt0 == mn){
int sum = 0;
for (int submask = mask0; submask > 0; submask = (submask - 1) & mask0){
int num = mask1 + submask;
int sign = ((__builtin_popcount(submask) & 1)) ? -1 : 1;
sum += superset[num] * sign;
}
{
int submask = 0;
int num = mask1 + submask;
int sign = ((__builtin_popcount(submask) & 1)) ? -1 : 1;
sum += superset[num] * sign;
}
cout << sum << "\n";
}
else if (popcnt1 == mn){
int sum = 0;
for (int submask = mask1; submask > 0; submask = (submask - 1) & mask1){
int num = mask2 + submask;
int sign = ((__builtin_popcount(submask) & 1) == (popcnt1 & 1)) ? 1 : -1;
sum += subset[num] * sign;
}
{
int submask = 0;
int num = mask2 + submask;
int sign = ((__builtin_popcount(submask) & 1) == (popcnt1 & 1)) ? 1 : -1;
sum += subset[num] * sign;
}
cout << sum << "\n";
}
else{
int sum = 0;
for (int submask = mask2; submask > 0; submask = (submask - 1) & mask2){
int num = mask1 + submask;
sum += v[num];
}
{
int submask = 0;
int num = mask1 + submask;
sum += v[num];
}
cout << sum << "\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
190 ms |
8724 KB |
Output is correct |
12 |
Correct |
185 ms |
8340 KB |
Output is correct |
13 |
Correct |
203 ms |
7504 KB |
Output is correct |
14 |
Correct |
224 ms |
7764 KB |
Output is correct |
15 |
Correct |
194 ms |
8792 KB |
Output is correct |
16 |
Correct |
230 ms |
7968 KB |
Output is correct |
17 |
Correct |
215 ms |
7892 KB |
Output is correct |
18 |
Correct |
163 ms |
9960 KB |
Output is correct |
19 |
Correct |
161 ms |
6716 KB |
Output is correct |
20 |
Correct |
188 ms |
8412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
190 ms |
8724 KB |
Output is correct |
12 |
Correct |
185 ms |
8340 KB |
Output is correct |
13 |
Correct |
203 ms |
7504 KB |
Output is correct |
14 |
Correct |
224 ms |
7764 KB |
Output is correct |
15 |
Correct |
194 ms |
8792 KB |
Output is correct |
16 |
Correct |
230 ms |
7968 KB |
Output is correct |
17 |
Correct |
215 ms |
7892 KB |
Output is correct |
18 |
Correct |
163 ms |
9960 KB |
Output is correct |
19 |
Correct |
161 ms |
6716 KB |
Output is correct |
20 |
Correct |
188 ms |
8412 KB |
Output is correct |
21 |
Correct |
199 ms |
10152 KB |
Output is correct |
22 |
Correct |
196 ms |
10192 KB |
Output is correct |
23 |
Correct |
241 ms |
9148 KB |
Output is correct |
24 |
Correct |
285 ms |
9104 KB |
Output is correct |
25 |
Correct |
228 ms |
11060 KB |
Output is correct |
26 |
Correct |
254 ms |
9520 KB |
Output is correct |
27 |
Correct |
262 ms |
9504 KB |
Output is correct |
28 |
Correct |
150 ms |
12112 KB |
Output is correct |
29 |
Correct |
184 ms |
8044 KB |
Output is correct |
30 |
Correct |
227 ms |
10324 KB |
Output is correct |
31 |
Correct |
238 ms |
10048 KB |
Output is correct |
32 |
Correct |
259 ms |
10092 KB |
Output is correct |
33 |
Correct |
207 ms |
8952 KB |
Output is correct |
34 |
Correct |
250 ms |
9044 KB |
Output is correct |
35 |
Correct |
260 ms |
9704 KB |
Output is correct |
36 |
Correct |
142 ms |
8216 KB |
Output is correct |
37 |
Correct |
188 ms |
10124 KB |
Output is correct |
38 |
Correct |
199 ms |
8012 KB |
Output is correct |
39 |
Correct |
228 ms |
9200 KB |
Output is correct |
40 |
Correct |
270 ms |
9112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
52 ms |
15424 KB |
Output is correct |
12 |
Correct |
76 ms |
15348 KB |
Output is correct |
13 |
Correct |
57 ms |
15372 KB |
Output is correct |
14 |
Correct |
72 ms |
15236 KB |
Output is correct |
15 |
Correct |
52 ms |
15420 KB |
Output is correct |
16 |
Correct |
62 ms |
15328 KB |
Output is correct |
17 |
Correct |
64 ms |
15348 KB |
Output is correct |
18 |
Correct |
47 ms |
15348 KB |
Output is correct |
19 |
Correct |
50 ms |
15140 KB |
Output is correct |
20 |
Correct |
62 ms |
15448 KB |
Output is correct |
21 |
Correct |
58 ms |
15436 KB |
Output is correct |
22 |
Correct |
65 ms |
15508 KB |
Output is correct |
23 |
Correct |
57 ms |
15188 KB |
Output is correct |
24 |
Correct |
62 ms |
15348 KB |
Output is correct |
25 |
Correct |
60 ms |
15352 KB |
Output is correct |
26 |
Correct |
52 ms |
15228 KB |
Output is correct |
27 |
Correct |
52 ms |
15352 KB |
Output is correct |
28 |
Correct |
53 ms |
15092 KB |
Output is correct |
29 |
Correct |
54 ms |
15332 KB |
Output is correct |
30 |
Correct |
62 ms |
15348 KB |
Output is correct |
31 |
Correct |
52 ms |
15192 KB |
Output is correct |
32 |
Correct |
62 ms |
15352 KB |
Output is correct |
33 |
Correct |
61 ms |
15168 KB |
Output is correct |
34 |
Correct |
46 ms |
15176 KB |
Output is correct |
35 |
Correct |
57 ms |
15352 KB |
Output is correct |
36 |
Correct |
68 ms |
15352 KB |
Output is correct |
37 |
Correct |
66 ms |
15384 KB |
Output is correct |
38 |
Correct |
58 ms |
15364 KB |
Output is correct |
39 |
Correct |
57 ms |
15204 KB |
Output is correct |
40 |
Correct |
58 ms |
15348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
190 ms |
8724 KB |
Output is correct |
12 |
Correct |
185 ms |
8340 KB |
Output is correct |
13 |
Correct |
203 ms |
7504 KB |
Output is correct |
14 |
Correct |
224 ms |
7764 KB |
Output is correct |
15 |
Correct |
194 ms |
8792 KB |
Output is correct |
16 |
Correct |
230 ms |
7968 KB |
Output is correct |
17 |
Correct |
215 ms |
7892 KB |
Output is correct |
18 |
Correct |
163 ms |
9960 KB |
Output is correct |
19 |
Correct |
161 ms |
6716 KB |
Output is correct |
20 |
Correct |
188 ms |
8412 KB |
Output is correct |
21 |
Correct |
199 ms |
10152 KB |
Output is correct |
22 |
Correct |
196 ms |
10192 KB |
Output is correct |
23 |
Correct |
241 ms |
9148 KB |
Output is correct |
24 |
Correct |
285 ms |
9104 KB |
Output is correct |
25 |
Correct |
228 ms |
11060 KB |
Output is correct |
26 |
Correct |
254 ms |
9520 KB |
Output is correct |
27 |
Correct |
262 ms |
9504 KB |
Output is correct |
28 |
Correct |
150 ms |
12112 KB |
Output is correct |
29 |
Correct |
184 ms |
8044 KB |
Output is correct |
30 |
Correct |
227 ms |
10324 KB |
Output is correct |
31 |
Correct |
238 ms |
10048 KB |
Output is correct |
32 |
Correct |
259 ms |
10092 KB |
Output is correct |
33 |
Correct |
207 ms |
8952 KB |
Output is correct |
34 |
Correct |
250 ms |
9044 KB |
Output is correct |
35 |
Correct |
260 ms |
9704 KB |
Output is correct |
36 |
Correct |
142 ms |
8216 KB |
Output is correct |
37 |
Correct |
188 ms |
10124 KB |
Output is correct |
38 |
Correct |
199 ms |
8012 KB |
Output is correct |
39 |
Correct |
228 ms |
9200 KB |
Output is correct |
40 |
Correct |
270 ms |
9112 KB |
Output is correct |
41 |
Correct |
52 ms |
15424 KB |
Output is correct |
42 |
Correct |
76 ms |
15348 KB |
Output is correct |
43 |
Correct |
57 ms |
15372 KB |
Output is correct |
44 |
Correct |
72 ms |
15236 KB |
Output is correct |
45 |
Correct |
52 ms |
15420 KB |
Output is correct |
46 |
Correct |
62 ms |
15328 KB |
Output is correct |
47 |
Correct |
64 ms |
15348 KB |
Output is correct |
48 |
Correct |
47 ms |
15348 KB |
Output is correct |
49 |
Correct |
50 ms |
15140 KB |
Output is correct |
50 |
Correct |
62 ms |
15448 KB |
Output is correct |
51 |
Correct |
58 ms |
15436 KB |
Output is correct |
52 |
Correct |
65 ms |
15508 KB |
Output is correct |
53 |
Correct |
57 ms |
15188 KB |
Output is correct |
54 |
Correct |
62 ms |
15348 KB |
Output is correct |
55 |
Correct |
60 ms |
15352 KB |
Output is correct |
56 |
Correct |
52 ms |
15228 KB |
Output is correct |
57 |
Correct |
52 ms |
15352 KB |
Output is correct |
58 |
Correct |
53 ms |
15092 KB |
Output is correct |
59 |
Correct |
54 ms |
15332 KB |
Output is correct |
60 |
Correct |
62 ms |
15348 KB |
Output is correct |
61 |
Correct |
52 ms |
15192 KB |
Output is correct |
62 |
Correct |
62 ms |
15352 KB |
Output is correct |
63 |
Correct |
61 ms |
15168 KB |
Output is correct |
64 |
Correct |
46 ms |
15176 KB |
Output is correct |
65 |
Correct |
57 ms |
15352 KB |
Output is correct |
66 |
Correct |
68 ms |
15352 KB |
Output is correct |
67 |
Correct |
66 ms |
15384 KB |
Output is correct |
68 |
Correct |
58 ms |
15364 KB |
Output is correct |
69 |
Correct |
57 ms |
15204 KB |
Output is correct |
70 |
Correct |
58 ms |
15348 KB |
Output is correct |
71 |
Correct |
312 ms |
27400 KB |
Output is correct |
72 |
Correct |
313 ms |
27472 KB |
Output is correct |
73 |
Correct |
418 ms |
26004 KB |
Output is correct |
74 |
Correct |
614 ms |
26296 KB |
Output is correct |
75 |
Correct |
325 ms |
28320 KB |
Output is correct |
76 |
Correct |
586 ms |
26592 KB |
Output is correct |
77 |
Correct |
567 ms |
18344 KB |
Output is correct |
78 |
Correct |
232 ms |
21900 KB |
Output is correct |
79 |
Correct |
316 ms |
15860 KB |
Output is correct |
80 |
Correct |
353 ms |
18888 KB |
Output is correct |
81 |
Correct |
390 ms |
18792 KB |
Output is correct |
82 |
Correct |
603 ms |
17768 KB |
Output is correct |
83 |
Correct |
335 ms |
17012 KB |
Output is correct |
84 |
Correct |
689 ms |
17844 KB |
Output is correct |
85 |
Correct |
585 ms |
18124 KB |
Output is correct |
86 |
Correct |
244 ms |
15884 KB |
Output is correct |
87 |
Correct |
325 ms |
18856 KB |
Output is correct |
88 |
Correct |
311 ms |
15888 KB |
Output is correct |
89 |
Correct |
394 ms |
17752 KB |
Output is correct |
90 |
Correct |
434 ms |
18152 KB |
Output is correct |
91 |
Correct |
344 ms |
16960 KB |
Output is correct |
92 |
Correct |
658 ms |
18236 KB |
Output is correct |
93 |
Correct |
621 ms |
18140 KB |
Output is correct |
94 |
Correct |
207 ms |
15720 KB |
Output is correct |
95 |
Correct |
451 ms |
17908 KB |
Output is correct |
96 |
Correct |
547 ms |
17920 KB |
Output is correct |
97 |
Correct |
447 ms |
18040 KB |
Output is correct |
98 |
Correct |
553 ms |
17996 KB |
Output is correct |
99 |
Correct |
527 ms |
18012 KB |
Output is correct |
100 |
Correct |
465 ms |
17912 KB |
Output is correct |