#include<bits/stdc++.h>
using namespace std;
const int maxn = (1<<20);
int n, q;
int sub[maxn], super[maxn], val[maxn];
int gt(char x){
if(x == '0' || x == '1')return x-'0';
else return 2;
}
int main(){
ios::sync_with_stdio(false);
cin >> n >> q;
for(int i = 0; i < (1<<n); i++){
char x;
cin >> x;
sub[i] = super[i] = val[i] = x-'0';
}
for(int i = 0; i < n; i++){
for(int j = 0; j < (1<<n); j++){
if(j&(1<<i)){
super[j^(1<<i)]+=super[j];
sub[j]+=sub[j^(1<<i)];
}
}
}
while(q--){
string str;
cin >> str;
reverse(str.begin(), str.end());
int mask[3] = {0, 0, 0};
long long ans = 0;
for(int i = 0; i < n; i++){
mask[gt(str[i])]|=(1<<i);
}
if(__builtin_popcount(mask[1]) <= 6){
for(int i = mask[1];;i=(i-1)&mask[1]){
if(__builtin_popcount(i)&1)ans -= sub[mask[2]^mask[1]^i];
else ans += sub[mask[2]^mask[1]^i];
if(!i)break;
}
}
else if(__builtin_popcount(mask[2]) <= 6){
for(int i = mask[2];;i=(i-1)&mask[2]){
ans += val[i^mask[1]];
if(!i)break;
}
}
else {
for(int i = mask[0];;i=(i-1)&mask[0]){
if(__builtin_popcount(i)&1)ans -= super[mask[1]^i];
else ans += super[mask[1]^i];
if(!i)break;
}
}
cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4524 KB |
Output is correct |
11 |
Correct |
1376 ms |
8412 KB |
Output is correct |
12 |
Correct |
1241 ms |
8044 KB |
Output is correct |
13 |
Correct |
1249 ms |
7436 KB |
Output is correct |
14 |
Correct |
1246 ms |
7660 KB |
Output is correct |
15 |
Correct |
1243 ms |
8892 KB |
Output is correct |
16 |
Correct |
1297 ms |
7804 KB |
Output is correct |
17 |
Correct |
1251 ms |
7820 KB |
Output is correct |
18 |
Correct |
1234 ms |
9676 KB |
Output is correct |
19 |
Correct |
1291 ms |
6552 KB |
Output is correct |
20 |
Correct |
1277 ms |
8420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4524 KB |
Output is correct |
11 |
Correct |
1376 ms |
8412 KB |
Output is correct |
12 |
Correct |
1241 ms |
8044 KB |
Output is correct |
13 |
Correct |
1249 ms |
7436 KB |
Output is correct |
14 |
Correct |
1246 ms |
7660 KB |
Output is correct |
15 |
Correct |
1243 ms |
8892 KB |
Output is correct |
16 |
Correct |
1297 ms |
7804 KB |
Output is correct |
17 |
Correct |
1251 ms |
7820 KB |
Output is correct |
18 |
Correct |
1234 ms |
9676 KB |
Output is correct |
19 |
Correct |
1291 ms |
6552 KB |
Output is correct |
20 |
Correct |
1277 ms |
8420 KB |
Output is correct |
21 |
Correct |
1428 ms |
8448 KB |
Output is correct |
22 |
Correct |
1232 ms |
8796 KB |
Output is correct |
23 |
Correct |
1271 ms |
7712 KB |
Output is correct |
24 |
Correct |
1259 ms |
7764 KB |
Output is correct |
25 |
Correct |
1279 ms |
9524 KB |
Output is correct |
26 |
Correct |
1324 ms |
8024 KB |
Output is correct |
27 |
Correct |
1294 ms |
7932 KB |
Output is correct |
28 |
Correct |
1220 ms |
10592 KB |
Output is correct |
29 |
Correct |
1247 ms |
6444 KB |
Output is correct |
30 |
Correct |
1311 ms |
8852 KB |
Output is correct |
31 |
Correct |
1299 ms |
8564 KB |
Output is correct |
32 |
Correct |
1261 ms |
8704 KB |
Output is correct |
33 |
Correct |
1222 ms |
7632 KB |
Output is correct |
34 |
Correct |
1303 ms |
7880 KB |
Output is correct |
35 |
Correct |
1295 ms |
7920 KB |
Output is correct |
36 |
Correct |
1266 ms |
6736 KB |
Output is correct |
37 |
Correct |
1212 ms |
8480 KB |
Output is correct |
38 |
Correct |
1299 ms |
6780 KB |
Output is correct |
39 |
Correct |
1439 ms |
7548 KB |
Output is correct |
40 |
Correct |
1323 ms |
7664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4524 KB |
Output is correct |
11 |
Correct |
103 ms |
12776 KB |
Output is correct |
12 |
Correct |
97 ms |
12884 KB |
Output is correct |
13 |
Correct |
113 ms |
12888 KB |
Output is correct |
14 |
Correct |
112 ms |
12796 KB |
Output is correct |
15 |
Correct |
100 ms |
13008 KB |
Output is correct |
16 |
Correct |
114 ms |
12756 KB |
Output is correct |
17 |
Correct |
107 ms |
12828 KB |
Output is correct |
18 |
Correct |
99 ms |
13140 KB |
Output is correct |
19 |
Correct |
116 ms |
13088 KB |
Output is correct |
20 |
Correct |
98 ms |
12880 KB |
Output is correct |
21 |
Correct |
107 ms |
13100 KB |
Output is correct |
22 |
Correct |
117 ms |
12740 KB |
Output is correct |
23 |
Correct |
101 ms |
12828 KB |
Output is correct |
24 |
Correct |
103 ms |
12756 KB |
Output is correct |
25 |
Correct |
107 ms |
12880 KB |
Output is correct |
26 |
Correct |
100 ms |
12760 KB |
Output is correct |
27 |
Correct |
108 ms |
12880 KB |
Output is correct |
28 |
Correct |
99 ms |
12804 KB |
Output is correct |
29 |
Correct |
104 ms |
12764 KB |
Output is correct |
30 |
Correct |
102 ms |
12856 KB |
Output is correct |
31 |
Correct |
107 ms |
13396 KB |
Output is correct |
32 |
Correct |
125 ms |
12892 KB |
Output is correct |
33 |
Correct |
117 ms |
12888 KB |
Output is correct |
34 |
Correct |
99 ms |
12892 KB |
Output is correct |
35 |
Correct |
114 ms |
13396 KB |
Output is correct |
36 |
Correct |
103 ms |
12884 KB |
Output is correct |
37 |
Correct |
106 ms |
12804 KB |
Output is correct |
38 |
Correct |
111 ms |
12884 KB |
Output is correct |
39 |
Correct |
109 ms |
13140 KB |
Output is correct |
40 |
Correct |
105 ms |
12884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
2 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
3 ms |
4444 KB |
Output is correct |
8 |
Correct |
2 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4524 KB |
Output is correct |
11 |
Correct |
1376 ms |
8412 KB |
Output is correct |
12 |
Correct |
1241 ms |
8044 KB |
Output is correct |
13 |
Correct |
1249 ms |
7436 KB |
Output is correct |
14 |
Correct |
1246 ms |
7660 KB |
Output is correct |
15 |
Correct |
1243 ms |
8892 KB |
Output is correct |
16 |
Correct |
1297 ms |
7804 KB |
Output is correct |
17 |
Correct |
1251 ms |
7820 KB |
Output is correct |
18 |
Correct |
1234 ms |
9676 KB |
Output is correct |
19 |
Correct |
1291 ms |
6552 KB |
Output is correct |
20 |
Correct |
1277 ms |
8420 KB |
Output is correct |
21 |
Correct |
1428 ms |
8448 KB |
Output is correct |
22 |
Correct |
1232 ms |
8796 KB |
Output is correct |
23 |
Correct |
1271 ms |
7712 KB |
Output is correct |
24 |
Correct |
1259 ms |
7764 KB |
Output is correct |
25 |
Correct |
1279 ms |
9524 KB |
Output is correct |
26 |
Correct |
1324 ms |
8024 KB |
Output is correct |
27 |
Correct |
1294 ms |
7932 KB |
Output is correct |
28 |
Correct |
1220 ms |
10592 KB |
Output is correct |
29 |
Correct |
1247 ms |
6444 KB |
Output is correct |
30 |
Correct |
1311 ms |
8852 KB |
Output is correct |
31 |
Correct |
1299 ms |
8564 KB |
Output is correct |
32 |
Correct |
1261 ms |
8704 KB |
Output is correct |
33 |
Correct |
1222 ms |
7632 KB |
Output is correct |
34 |
Correct |
1303 ms |
7880 KB |
Output is correct |
35 |
Correct |
1295 ms |
7920 KB |
Output is correct |
36 |
Correct |
1266 ms |
6736 KB |
Output is correct |
37 |
Correct |
1212 ms |
8480 KB |
Output is correct |
38 |
Correct |
1299 ms |
6780 KB |
Output is correct |
39 |
Correct |
1439 ms |
7548 KB |
Output is correct |
40 |
Correct |
1323 ms |
7664 KB |
Output is correct |
41 |
Correct |
103 ms |
12776 KB |
Output is correct |
42 |
Correct |
97 ms |
12884 KB |
Output is correct |
43 |
Correct |
113 ms |
12888 KB |
Output is correct |
44 |
Correct |
112 ms |
12796 KB |
Output is correct |
45 |
Correct |
100 ms |
13008 KB |
Output is correct |
46 |
Correct |
114 ms |
12756 KB |
Output is correct |
47 |
Correct |
107 ms |
12828 KB |
Output is correct |
48 |
Correct |
99 ms |
13140 KB |
Output is correct |
49 |
Correct |
116 ms |
13088 KB |
Output is correct |
50 |
Correct |
98 ms |
12880 KB |
Output is correct |
51 |
Correct |
107 ms |
13100 KB |
Output is correct |
52 |
Correct |
117 ms |
12740 KB |
Output is correct |
53 |
Correct |
101 ms |
12828 KB |
Output is correct |
54 |
Correct |
103 ms |
12756 KB |
Output is correct |
55 |
Correct |
107 ms |
12880 KB |
Output is correct |
56 |
Correct |
100 ms |
12760 KB |
Output is correct |
57 |
Correct |
108 ms |
12880 KB |
Output is correct |
58 |
Correct |
99 ms |
12804 KB |
Output is correct |
59 |
Correct |
104 ms |
12764 KB |
Output is correct |
60 |
Correct |
102 ms |
12856 KB |
Output is correct |
61 |
Correct |
107 ms |
13396 KB |
Output is correct |
62 |
Correct |
125 ms |
12892 KB |
Output is correct |
63 |
Correct |
117 ms |
12888 KB |
Output is correct |
64 |
Correct |
99 ms |
12892 KB |
Output is correct |
65 |
Correct |
114 ms |
13396 KB |
Output is correct |
66 |
Correct |
103 ms |
12884 KB |
Output is correct |
67 |
Correct |
106 ms |
12804 KB |
Output is correct |
68 |
Correct |
111 ms |
12884 KB |
Output is correct |
69 |
Correct |
109 ms |
13140 KB |
Output is correct |
70 |
Correct |
105 ms |
12884 KB |
Output is correct |
71 |
Correct |
1355 ms |
17732 KB |
Output is correct |
72 |
Correct |
1364 ms |
17820 KB |
Output is correct |
73 |
Correct |
1450 ms |
16348 KB |
Output is correct |
74 |
Correct |
1639 ms |
16704 KB |
Output is correct |
75 |
Correct |
1472 ms |
18684 KB |
Output is correct |
76 |
Correct |
1580 ms |
17148 KB |
Output is correct |
77 |
Correct |
1487 ms |
16724 KB |
Output is correct |
78 |
Correct |
1393 ms |
20544 KB |
Output is correct |
79 |
Correct |
1346 ms |
14712 KB |
Output is correct |
80 |
Correct |
1409 ms |
17696 KB |
Output is correct |
81 |
Correct |
1476 ms |
17812 KB |
Output is correct |
82 |
Correct |
1582 ms |
16836 KB |
Output is correct |
83 |
Correct |
1367 ms |
16152 KB |
Output is correct |
84 |
Correct |
1516 ms |
16768 KB |
Output is correct |
85 |
Correct |
1697 ms |
16848 KB |
Output is correct |
86 |
Correct |
1316 ms |
14892 KB |
Output is correct |
87 |
Correct |
1342 ms |
17556 KB |
Output is correct |
88 |
Correct |
1339 ms |
14648 KB |
Output is correct |
89 |
Correct |
1469 ms |
16276 KB |
Output is correct |
90 |
Correct |
1426 ms |
16528 KB |
Output is correct |
91 |
Correct |
1391 ms |
15884 KB |
Output is correct |
92 |
Correct |
1567 ms |
17296 KB |
Output is correct |
93 |
Correct |
1537 ms |
17104 KB |
Output is correct |
94 |
Correct |
1346 ms |
14648 KB |
Output is correct |
95 |
Correct |
1477 ms |
16824 KB |
Output is correct |
96 |
Correct |
1563 ms |
16864 KB |
Output is correct |
97 |
Correct |
1496 ms |
16672 KB |
Output is correct |
98 |
Correct |
1486 ms |
16644 KB |
Output is correct |
99 |
Correct |
1487 ms |
16804 KB |
Output is correct |
100 |
Correct |
1491 ms |
16716 KB |
Output is correct |