#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 4e18+9;
void solve(){
int n, q;
cin >> n >> q;
string s;
cin >> s;
int full = (1<<n)-1;
vector<int> v(full+1);
for(int i = 0; i <= full; i++){
v[i] = s[i]-'0';
}
vector<int> f(full+1, 0), g(full+1, 0);
for(int mask = 0; mask <= full; mask++){
f[mask] = v[mask];
g[mask] = v[mask];
}
for(int i = 0; i < n; i++){
for(int mask = 0; mask <= full; mask++){
if(mask&(1<<i)) f[mask] += f[mask^(1<<i)];
}
for(int mask = full; mask >= 0; mask--){
if(!(mask&(1<<i))) g[mask] += g[mask^(1<<i)];
}
}
for(int i = 1; i <= q; i++){
string s;
cin >> s;
reverse(s.begin(), s.end());
int A, B, C;
A = B = C = 0;
int mask0 = 0, mask1 = 0, maskc = 0;
vector<int> a, b, c;
for(int i = 0; i < n; i++){
if(s[i] == '0'){
A++;
a.push_back(i);
}
if(s[i] == '1'){
B++;
b.push_back(i);
mask0 += (1<<i);
}
if(s[i] == '?'){
C++;
c.push_back(i);
mask1 += (1<<i);
}else{
maskc += (1<<i)*(s[i]-'0');
}
}
int mn = min({A, B, C});
int res = 0;
if(mn == A){
for(int mask = 0; mask < (1<<A); mask++){
int nmask = mask0;
for(int i = 0; i < A; i++){
if(mask&(1<<i)){
nmask += (1<<a[i]);
}
}
if(__builtin_popcount(mask)&1){
res -= g[nmask];
}else{
res += g[nmask];
}
}
cout << res << "\n";
continue;
}
if(mn == B){
for(int mask = 0; mask < (1<<B); mask++){
int nmask = mask1;
for(int i = 0; i < B; i++){
if(!(mask&(1<<i))){
nmask += (1<<b[i]);
}
}
if(__builtin_popcount(mask)&1){
res -= f[nmask];
}else{
res += f[nmask];
}
}
cout << res << "\n";
continue;
}
if(mn == C){
for(int mask = 0; mask < (1<<C); mask++){
int nmask = maskc;
for(int i = 0; i < C; i++){
if(mask&(1<<i)){
nmask += (1<<c[i]);
}
}
res += v[nmask];
}
cout << res << "\n";
continue;
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
338 ms |
4388 KB |
Output is correct |
12 |
Correct |
294 ms |
4076 KB |
Output is correct |
13 |
Correct |
354 ms |
3264 KB |
Output is correct |
14 |
Correct |
386 ms |
3404 KB |
Output is correct |
15 |
Correct |
323 ms |
4424 KB |
Output is correct |
16 |
Correct |
392 ms |
3660 KB |
Output is correct |
17 |
Correct |
379 ms |
3400 KB |
Output is correct |
18 |
Correct |
229 ms |
5248 KB |
Output is correct |
19 |
Correct |
289 ms |
2300 KB |
Output is correct |
20 |
Correct |
324 ms |
3912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
338 ms |
4388 KB |
Output is correct |
12 |
Correct |
294 ms |
4076 KB |
Output is correct |
13 |
Correct |
354 ms |
3264 KB |
Output is correct |
14 |
Correct |
386 ms |
3404 KB |
Output is correct |
15 |
Correct |
323 ms |
4424 KB |
Output is correct |
16 |
Correct |
392 ms |
3660 KB |
Output is correct |
17 |
Correct |
379 ms |
3400 KB |
Output is correct |
18 |
Correct |
229 ms |
5248 KB |
Output is correct |
19 |
Correct |
289 ms |
2300 KB |
Output is correct |
20 |
Correct |
324 ms |
3912 KB |
Output is correct |
21 |
Correct |
343 ms |
4532 KB |
Output is correct |
22 |
Correct |
332 ms |
4680 KB |
Output is correct |
23 |
Correct |
453 ms |
3612 KB |
Output is correct |
24 |
Correct |
515 ms |
3516 KB |
Output is correct |
25 |
Correct |
404 ms |
5704 KB |
Output is correct |
26 |
Correct |
501 ms |
3912 KB |
Output is correct |
27 |
Correct |
478 ms |
3912 KB |
Output is correct |
28 |
Correct |
214 ms |
6472 KB |
Output is correct |
29 |
Correct |
310 ms |
2460 KB |
Output is correct |
30 |
Correct |
362 ms |
4680 KB |
Output is correct |
31 |
Correct |
440 ms |
4428 KB |
Output is correct |
32 |
Correct |
482 ms |
4424 KB |
Output is correct |
33 |
Correct |
399 ms |
3400 KB |
Output is correct |
34 |
Correct |
479 ms |
3400 KB |
Output is correct |
35 |
Correct |
473 ms |
3824 KB |
Output is correct |
36 |
Correct |
206 ms |
2376 KB |
Output is correct |
37 |
Correct |
330 ms |
4436 KB |
Output is correct |
38 |
Correct |
344 ms |
3680 KB |
Output is correct |
39 |
Correct |
431 ms |
3576 KB |
Output is correct |
40 |
Correct |
518 ms |
3360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
61 ms |
14972 KB |
Output is correct |
12 |
Correct |
65 ms |
14988 KB |
Output is correct |
13 |
Correct |
79 ms |
15080 KB |
Output is correct |
14 |
Correct |
102 ms |
15080 KB |
Output is correct |
15 |
Correct |
66 ms |
15084 KB |
Output is correct |
16 |
Correct |
91 ms |
15080 KB |
Output is correct |
17 |
Correct |
86 ms |
14964 KB |
Output is correct |
18 |
Correct |
47 ms |
15232 KB |
Output is correct |
19 |
Correct |
56 ms |
14928 KB |
Output is correct |
20 |
Correct |
63 ms |
15848 KB |
Output is correct |
21 |
Correct |
67 ms |
15084 KB |
Output is correct |
22 |
Correct |
90 ms |
15080 KB |
Output is correct |
23 |
Correct |
79 ms |
16276 KB |
Output is correct |
24 |
Correct |
88 ms |
14960 KB |
Output is correct |
25 |
Correct |
83 ms |
15080 KB |
Output is correct |
26 |
Correct |
49 ms |
15004 KB |
Output is correct |
27 |
Correct |
60 ms |
16476 KB |
Output is correct |
28 |
Correct |
62 ms |
14836 KB |
Output is correct |
29 |
Correct |
82 ms |
16360 KB |
Output is correct |
30 |
Correct |
86 ms |
15080 KB |
Output is correct |
31 |
Correct |
62 ms |
15080 KB |
Output is correct |
32 |
Correct |
91 ms |
16360 KB |
Output is correct |
33 |
Correct |
81 ms |
15080 KB |
Output is correct |
34 |
Correct |
48 ms |
14836 KB |
Output is correct |
35 |
Correct |
78 ms |
15080 KB |
Output is correct |
36 |
Correct |
81 ms |
16360 KB |
Output is correct |
37 |
Correct |
76 ms |
14960 KB |
Output is correct |
38 |
Correct |
78 ms |
16312 KB |
Output is correct |
39 |
Correct |
71 ms |
15084 KB |
Output is correct |
40 |
Correct |
76 ms |
16360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
338 ms |
4388 KB |
Output is correct |
12 |
Correct |
294 ms |
4076 KB |
Output is correct |
13 |
Correct |
354 ms |
3264 KB |
Output is correct |
14 |
Correct |
386 ms |
3404 KB |
Output is correct |
15 |
Correct |
323 ms |
4424 KB |
Output is correct |
16 |
Correct |
392 ms |
3660 KB |
Output is correct |
17 |
Correct |
379 ms |
3400 KB |
Output is correct |
18 |
Correct |
229 ms |
5248 KB |
Output is correct |
19 |
Correct |
289 ms |
2300 KB |
Output is correct |
20 |
Correct |
324 ms |
3912 KB |
Output is correct |
21 |
Correct |
343 ms |
4532 KB |
Output is correct |
22 |
Correct |
332 ms |
4680 KB |
Output is correct |
23 |
Correct |
453 ms |
3612 KB |
Output is correct |
24 |
Correct |
515 ms |
3516 KB |
Output is correct |
25 |
Correct |
404 ms |
5704 KB |
Output is correct |
26 |
Correct |
501 ms |
3912 KB |
Output is correct |
27 |
Correct |
478 ms |
3912 KB |
Output is correct |
28 |
Correct |
214 ms |
6472 KB |
Output is correct |
29 |
Correct |
310 ms |
2460 KB |
Output is correct |
30 |
Correct |
362 ms |
4680 KB |
Output is correct |
31 |
Correct |
440 ms |
4428 KB |
Output is correct |
32 |
Correct |
482 ms |
4424 KB |
Output is correct |
33 |
Correct |
399 ms |
3400 KB |
Output is correct |
34 |
Correct |
479 ms |
3400 KB |
Output is correct |
35 |
Correct |
473 ms |
3824 KB |
Output is correct |
36 |
Correct |
206 ms |
2376 KB |
Output is correct |
37 |
Correct |
330 ms |
4436 KB |
Output is correct |
38 |
Correct |
344 ms |
3680 KB |
Output is correct |
39 |
Correct |
431 ms |
3576 KB |
Output is correct |
40 |
Correct |
518 ms |
3360 KB |
Output is correct |
41 |
Correct |
61 ms |
14972 KB |
Output is correct |
42 |
Correct |
65 ms |
14988 KB |
Output is correct |
43 |
Correct |
79 ms |
15080 KB |
Output is correct |
44 |
Correct |
102 ms |
15080 KB |
Output is correct |
45 |
Correct |
66 ms |
15084 KB |
Output is correct |
46 |
Correct |
91 ms |
15080 KB |
Output is correct |
47 |
Correct |
86 ms |
14964 KB |
Output is correct |
48 |
Correct |
47 ms |
15232 KB |
Output is correct |
49 |
Correct |
56 ms |
14928 KB |
Output is correct |
50 |
Correct |
63 ms |
15848 KB |
Output is correct |
51 |
Correct |
67 ms |
15084 KB |
Output is correct |
52 |
Correct |
90 ms |
15080 KB |
Output is correct |
53 |
Correct |
79 ms |
16276 KB |
Output is correct |
54 |
Correct |
88 ms |
14960 KB |
Output is correct |
55 |
Correct |
83 ms |
15080 KB |
Output is correct |
56 |
Correct |
49 ms |
15004 KB |
Output is correct |
57 |
Correct |
60 ms |
16476 KB |
Output is correct |
58 |
Correct |
62 ms |
14836 KB |
Output is correct |
59 |
Correct |
82 ms |
16360 KB |
Output is correct |
60 |
Correct |
86 ms |
15080 KB |
Output is correct |
61 |
Correct |
62 ms |
15080 KB |
Output is correct |
62 |
Correct |
91 ms |
16360 KB |
Output is correct |
63 |
Correct |
81 ms |
15080 KB |
Output is correct |
64 |
Correct |
48 ms |
14836 KB |
Output is correct |
65 |
Correct |
78 ms |
15080 KB |
Output is correct |
66 |
Correct |
81 ms |
16360 KB |
Output is correct |
67 |
Correct |
76 ms |
14960 KB |
Output is correct |
68 |
Correct |
78 ms |
16312 KB |
Output is correct |
69 |
Correct |
71 ms |
15084 KB |
Output is correct |
70 |
Correct |
76 ms |
16360 KB |
Output is correct |
71 |
Correct |
550 ms |
30852 KB |
Output is correct |
72 |
Correct |
509 ms |
20084 KB |
Output is correct |
73 |
Correct |
746 ms |
18428 KB |
Output is correct |
74 |
Correct |
1151 ms |
18804 KB |
Output is correct |
75 |
Correct |
618 ms |
20832 KB |
Output is correct |
76 |
Correct |
1199 ms |
22844 KB |
Output is correct |
77 |
Correct |
969 ms |
19184 KB |
Output is correct |
78 |
Correct |
318 ms |
27544 KB |
Output is correct |
79 |
Correct |
502 ms |
16872 KB |
Output is correct |
80 |
Correct |
574 ms |
19944 KB |
Output is correct |
81 |
Correct |
751 ms |
32208 KB |
Output is correct |
82 |
Correct |
1207 ms |
29708 KB |
Output is correct |
83 |
Correct |
594 ms |
18152 KB |
Output is correct |
84 |
Correct |
1130 ms |
35376 KB |
Output is correct |
85 |
Correct |
1031 ms |
30180 KB |
Output is correct |
86 |
Correct |
350 ms |
27740 KB |
Output is correct |
87 |
Correct |
528 ms |
19944 KB |
Output is correct |
88 |
Correct |
563 ms |
27964 KB |
Output is correct |
89 |
Correct |
773 ms |
35196 KB |
Output is correct |
90 |
Correct |
1036 ms |
29808 KB |
Output is correct |
91 |
Correct |
610 ms |
34792 KB |
Output is correct |
92 |
Correct |
1212 ms |
30236 KB |
Output is correct |
93 |
Correct |
987 ms |
18920 KB |
Output is correct |
94 |
Correct |
325 ms |
27788 KB |
Output is correct |
95 |
Correct |
903 ms |
35620 KB |
Output is correct |
96 |
Correct |
869 ms |
35688 KB |
Output is correct |
97 |
Correct |
822 ms |
29932 KB |
Output is correct |
98 |
Correct |
847 ms |
29868 KB |
Output is correct |
99 |
Correct |
804 ms |
29928 KB |
Output is correct |
100 |
Correct |
846 ms |
35668 KB |
Output is correct |