#include<bits/stdc++.h>
using namespace std;
#define N 25
int n, q, dp1[(1 << 20) + 1], dp2[(1 << 20) + 1], val[(1 << 20) + 1];
string s;
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> q;
cin >> s;
for (int i = 0; i < (1 << n); i++){
dp1[i] = dp2[i] = val[i] = (s[i] - '0');
}
for (int i = 0; i < n; i++){
for (int msk = 0; msk < 1 << n; msk++){
if ((msk >> i) & 1){
dp1[msk] += dp1[msk ^ (1 << i)];
}
else dp2[msk] += dp2[msk ^ (1 << i)];
}
}
while (q--){
string t;
cin >> t;
reverse(t.begin(), t.end());
int cnt1 = 0, cnta = 0, cnt0 = 0, mska = 0, msk1 = 0, msk0 = 0;
for (int i = 0; i < (int) t.size(); i++){
if (t[i] == '?'){
cnta++;
mska |= (1 << i);
continue;
}
if (t[i] == '1'){
cnt1++;
msk1 |= (1 << i);
continue;
}
cnt0++;
msk0 |= (1 << i);
}
int mi = min({cnt0, cnt1, cnta});
if (cnta == mi){
int res = val[msk1];
for (int msk = mska; msk > 0; msk = (msk - 1) & mska){
res += val[msk1 | msk];
}
cout << res << "\n";
continue;
}
if (mi == cnt0){
int res = dp2[msk1];
for (int msk = msk0; msk > 0; msk = (msk - 1) & msk0){
if (__builtin_popcount(msk) % 2 == 0){
res += dp2[msk1 | msk];
}
else res -= dp2[msk1 | msk];
}
cout << res << "\n";
continue;
}
int res = dp1[mska];
if (cnt1 & 1) res = -dp1[mska];
for (int msk = msk1; msk > 0; msk = (msk - 1) & msk1){
if (__builtin_popcount(msk) % 2 == cnt1 % 2){
res += dp1[mska | msk];
}
else res -= dp1[mska | msk];
}
cout << res << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
4576 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 |
0 ms |
4568 KB |
Output is correct |
10 |
Correct |
0 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
4576 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 |
0 ms |
4568 KB |
Output is correct |
10 |
Correct |
0 ms |
4444 KB |
Output is correct |
11 |
Correct |
122 ms |
19196 KB |
Output is correct |
12 |
Correct |
121 ms |
18772 KB |
Output is correct |
13 |
Correct |
142 ms |
18144 KB |
Output is correct |
14 |
Correct |
130 ms |
18256 KB |
Output is correct |
15 |
Correct |
137 ms |
19284 KB |
Output is correct |
16 |
Correct |
149 ms |
18512 KB |
Output is correct |
17 |
Correct |
147 ms |
18416 KB |
Output is correct |
18 |
Correct |
95 ms |
20300 KB |
Output is correct |
19 |
Correct |
116 ms |
17224 KB |
Output is correct |
20 |
Correct |
130 ms |
18776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
4576 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 |
0 ms |
4568 KB |
Output is correct |
10 |
Correct |
0 ms |
4444 KB |
Output is correct |
11 |
Correct |
122 ms |
19196 KB |
Output is correct |
12 |
Correct |
121 ms |
18772 KB |
Output is correct |
13 |
Correct |
142 ms |
18144 KB |
Output is correct |
14 |
Correct |
130 ms |
18256 KB |
Output is correct |
15 |
Correct |
137 ms |
19284 KB |
Output is correct |
16 |
Correct |
149 ms |
18512 KB |
Output is correct |
17 |
Correct |
147 ms |
18416 KB |
Output is correct |
18 |
Correct |
95 ms |
20300 KB |
Output is correct |
19 |
Correct |
116 ms |
17224 KB |
Output is correct |
20 |
Correct |
130 ms |
18776 KB |
Output is correct |
21 |
Correct |
132 ms |
22176 KB |
Output is correct |
22 |
Correct |
141 ms |
22356 KB |
Output is correct |
23 |
Correct |
164 ms |
21476 KB |
Output is correct |
24 |
Correct |
156 ms |
21076 KB |
Output is correct |
25 |
Correct |
160 ms |
23120 KB |
Output is correct |
26 |
Correct |
171 ms |
21660 KB |
Output is correct |
27 |
Correct |
176 ms |
21764 KB |
Output is correct |
28 |
Correct |
98 ms |
24316 KB |
Output is correct |
29 |
Correct |
136 ms |
20308 KB |
Output is correct |
30 |
Correct |
143 ms |
22356 KB |
Output is correct |
31 |
Correct |
180 ms |
22100 KB |
Output is correct |
32 |
Correct |
189 ms |
22096 KB |
Output is correct |
33 |
Correct |
146 ms |
21072 KB |
Output is correct |
34 |
Correct |
168 ms |
21332 KB |
Output is correct |
35 |
Correct |
173 ms |
21584 KB |
Output is correct |
36 |
Correct |
100 ms |
20308 KB |
Output is correct |
37 |
Correct |
135 ms |
22176 KB |
Output is correct |
38 |
Correct |
145 ms |
20308 KB |
Output is correct |
39 |
Correct |
159 ms |
21328 KB |
Output is correct |
40 |
Correct |
159 ms |
21232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
4576 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 |
0 ms |
4568 KB |
Output is correct |
10 |
Correct |
0 ms |
4444 KB |
Output is correct |
11 |
Correct |
29 ms |
16372 KB |
Output is correct |
12 |
Correct |
26 ms |
16116 KB |
Output is correct |
13 |
Correct |
29 ms |
15960 KB |
Output is correct |
14 |
Correct |
37 ms |
16116 KB |
Output is correct |
15 |
Correct |
29 ms |
16224 KB |
Output is correct |
16 |
Correct |
36 ms |
15956 KB |
Output is correct |
17 |
Correct |
37 ms |
16116 KB |
Output is correct |
18 |
Correct |
23 ms |
16384 KB |
Output is correct |
19 |
Correct |
26 ms |
15944 KB |
Output is correct |
20 |
Correct |
27 ms |
16128 KB |
Output is correct |
21 |
Correct |
30 ms |
16216 KB |
Output is correct |
22 |
Correct |
41 ms |
16116 KB |
Output is correct |
23 |
Correct |
27 ms |
16120 KB |
Output is correct |
24 |
Correct |
35 ms |
16164 KB |
Output is correct |
25 |
Correct |
34 ms |
16112 KB |
Output is correct |
26 |
Correct |
27 ms |
15864 KB |
Output is correct |
27 |
Correct |
27 ms |
16120 KB |
Output is correct |
28 |
Correct |
28 ms |
15872 KB |
Output is correct |
29 |
Correct |
32 ms |
16112 KB |
Output is correct |
30 |
Correct |
32 ms |
16112 KB |
Output is correct |
31 |
Correct |
27 ms |
16116 KB |
Output is correct |
32 |
Correct |
36 ms |
16116 KB |
Output is correct |
33 |
Correct |
34 ms |
16112 KB |
Output is correct |
34 |
Correct |
23 ms |
15860 KB |
Output is correct |
35 |
Correct |
30 ms |
16128 KB |
Output is correct |
36 |
Correct |
32 ms |
16128 KB |
Output is correct |
37 |
Correct |
30 ms |
16128 KB |
Output is correct |
38 |
Correct |
30 ms |
15948 KB |
Output is correct |
39 |
Correct |
30 ms |
15992 KB |
Output is correct |
40 |
Correct |
32 ms |
16116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
0 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
4576 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 |
0 ms |
4568 KB |
Output is correct |
10 |
Correct |
0 ms |
4444 KB |
Output is correct |
11 |
Correct |
122 ms |
19196 KB |
Output is correct |
12 |
Correct |
121 ms |
18772 KB |
Output is correct |
13 |
Correct |
142 ms |
18144 KB |
Output is correct |
14 |
Correct |
130 ms |
18256 KB |
Output is correct |
15 |
Correct |
137 ms |
19284 KB |
Output is correct |
16 |
Correct |
149 ms |
18512 KB |
Output is correct |
17 |
Correct |
147 ms |
18416 KB |
Output is correct |
18 |
Correct |
95 ms |
20300 KB |
Output is correct |
19 |
Correct |
116 ms |
17224 KB |
Output is correct |
20 |
Correct |
130 ms |
18776 KB |
Output is correct |
21 |
Correct |
132 ms |
22176 KB |
Output is correct |
22 |
Correct |
141 ms |
22356 KB |
Output is correct |
23 |
Correct |
164 ms |
21476 KB |
Output is correct |
24 |
Correct |
156 ms |
21076 KB |
Output is correct |
25 |
Correct |
160 ms |
23120 KB |
Output is correct |
26 |
Correct |
171 ms |
21660 KB |
Output is correct |
27 |
Correct |
176 ms |
21764 KB |
Output is correct |
28 |
Correct |
98 ms |
24316 KB |
Output is correct |
29 |
Correct |
136 ms |
20308 KB |
Output is correct |
30 |
Correct |
143 ms |
22356 KB |
Output is correct |
31 |
Correct |
180 ms |
22100 KB |
Output is correct |
32 |
Correct |
189 ms |
22096 KB |
Output is correct |
33 |
Correct |
146 ms |
21072 KB |
Output is correct |
34 |
Correct |
168 ms |
21332 KB |
Output is correct |
35 |
Correct |
173 ms |
21584 KB |
Output is correct |
36 |
Correct |
100 ms |
20308 KB |
Output is correct |
37 |
Correct |
135 ms |
22176 KB |
Output is correct |
38 |
Correct |
145 ms |
20308 KB |
Output is correct |
39 |
Correct |
159 ms |
21328 KB |
Output is correct |
40 |
Correct |
159 ms |
21232 KB |
Output is correct |
41 |
Correct |
29 ms |
16372 KB |
Output is correct |
42 |
Correct |
26 ms |
16116 KB |
Output is correct |
43 |
Correct |
29 ms |
15960 KB |
Output is correct |
44 |
Correct |
37 ms |
16116 KB |
Output is correct |
45 |
Correct |
29 ms |
16224 KB |
Output is correct |
46 |
Correct |
36 ms |
15956 KB |
Output is correct |
47 |
Correct |
37 ms |
16116 KB |
Output is correct |
48 |
Correct |
23 ms |
16384 KB |
Output is correct |
49 |
Correct |
26 ms |
15944 KB |
Output is correct |
50 |
Correct |
27 ms |
16128 KB |
Output is correct |
51 |
Correct |
30 ms |
16216 KB |
Output is correct |
52 |
Correct |
41 ms |
16116 KB |
Output is correct |
53 |
Correct |
27 ms |
16120 KB |
Output is correct |
54 |
Correct |
35 ms |
16164 KB |
Output is correct |
55 |
Correct |
34 ms |
16112 KB |
Output is correct |
56 |
Correct |
27 ms |
15864 KB |
Output is correct |
57 |
Correct |
27 ms |
16120 KB |
Output is correct |
58 |
Correct |
28 ms |
15872 KB |
Output is correct |
59 |
Correct |
32 ms |
16112 KB |
Output is correct |
60 |
Correct |
32 ms |
16112 KB |
Output is correct |
61 |
Correct |
27 ms |
16116 KB |
Output is correct |
62 |
Correct |
36 ms |
16116 KB |
Output is correct |
63 |
Correct |
34 ms |
16112 KB |
Output is correct |
64 |
Correct |
23 ms |
15860 KB |
Output is correct |
65 |
Correct |
30 ms |
16128 KB |
Output is correct |
66 |
Correct |
32 ms |
16128 KB |
Output is correct |
67 |
Correct |
30 ms |
16128 KB |
Output is correct |
68 |
Correct |
30 ms |
15948 KB |
Output is correct |
69 |
Correct |
30 ms |
15992 KB |
Output is correct |
70 |
Correct |
32 ms |
16116 KB |
Output is correct |
71 |
Correct |
220 ms |
40456 KB |
Output is correct |
72 |
Correct |
209 ms |
40584 KB |
Output is correct |
73 |
Correct |
244 ms |
39160 KB |
Output is correct |
74 |
Correct |
439 ms |
39416 KB |
Output is correct |
75 |
Correct |
230 ms |
41456 KB |
Output is correct |
76 |
Correct |
359 ms |
39760 KB |
Output is correct |
77 |
Correct |
312 ms |
39668 KB |
Output is correct |
78 |
Correct |
140 ms |
43368 KB |
Output is correct |
79 |
Correct |
201 ms |
37408 KB |
Output is correct |
80 |
Correct |
227 ms |
40432 KB |
Output is correct |
81 |
Correct |
284 ms |
40404 KB |
Output is correct |
82 |
Correct |
436 ms |
39412 KB |
Output is correct |
83 |
Correct |
210 ms |
38644 KB |
Output is correct |
84 |
Correct |
316 ms |
39408 KB |
Output is correct |
85 |
Correct |
316 ms |
39924 KB |
Output is correct |
86 |
Correct |
153 ms |
37452 KB |
Output is correct |
87 |
Correct |
195 ms |
40280 KB |
Output is correct |
88 |
Correct |
208 ms |
37360 KB |
Output is correct |
89 |
Correct |
256 ms |
39152 KB |
Output is correct |
90 |
Correct |
315 ms |
39504 KB |
Output is correct |
91 |
Correct |
209 ms |
38480 KB |
Output is correct |
92 |
Correct |
354 ms |
39664 KB |
Output is correct |
93 |
Correct |
311 ms |
39608 KB |
Output is correct |
94 |
Correct |
139 ms |
37364 KB |
Output is correct |
95 |
Correct |
272 ms |
39512 KB |
Output is correct |
96 |
Correct |
273 ms |
39412 KB |
Output is correct |
97 |
Correct |
282 ms |
39672 KB |
Output is correct |
98 |
Correct |
274 ms |
39452 KB |
Output is correct |
99 |
Correct |
283 ms |
39668 KB |
Output is correct |
100 |
Correct |
272 ms |
39560 KB |
Output is correct |