#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define BIT(x,i) (((x)>>(i))&1)
using namespace std;
typedef long long ll;
int dp[1<<20];
int dp1[1<<20];
int l,q;
int sum=0;
string a;
signed main(){
// freopen("input.inp","r",stdin);
// freopen("output.out","w",stdout);
cin.tie(0),cout.tie(0)->sync_with_stdio(0);
cin >> l >> q;
cin >> a;
for(int i=0;i<(1<<l);i++){
int num=a[i]-'0';
dp[i]=num;
dp1[i]=num;
}
for(int i=0;i<l;i++){
for(int j=0;j<(1<<l);j++){
if((1<<i)&j)dp[j]+=dp[j^(1<<i)];
else dp1[j]+=dp1[j^(1<<i)];
}
}
for(int i=1;i<=q;i++){
string s;
cin >> s;
int mask0=0;
int num0=0,num1=0,num2=0,mask1=0,mask2=0;
int sum=0;
for(int j=0;j<l;j++){
if(s[l-1-j]=='?'){
mask2^=(1<<j);
num2++;
}
if(s[l-1-j]=='0'){
mask0^=(1<<j);
num0++;
}
if(s[l-1-j]=='1'){
mask1^=(1<<j);
num1++;
}
}
if(num0<=min(num1,num2)){
for(int j=mask0;j>=0;j=(j-1)&mask0){
if(__builtin_popcount(j)%2==0){
sum+=dp1[mask1^j];
}
else{
sum-=dp1[mask1^j];
}
if(j==0)break;
}
}
else if(num1<=min(num0,num2)){
for(int j=mask1;j>=0;j=(j-1)&mask1){
if((__builtin_popcount(j)&1)==(__builtin_popcount(mask1)&1)){
sum+=dp[j^mask2];
}
else{
sum-=dp[j^mask2];
}
if(j==0)break;
}
}
else{
for(int j=mask2;j>=0;j=(j-1)&mask2){
sum+=a[j^mask1]-'0';
if(j==0)break;
}
}
cout << sum << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
168 ms |
16052 KB |
Output is correct |
12 |
Correct |
181 ms |
15732 KB |
Output is correct |
13 |
Correct |
194 ms |
14928 KB |
Output is correct |
14 |
Correct |
234 ms |
15184 KB |
Output is correct |
15 |
Correct |
195 ms |
16024 KB |
Output is correct |
16 |
Correct |
223 ms |
15332 KB |
Output is correct |
17 |
Correct |
217 ms |
15184 KB |
Output is correct |
18 |
Correct |
171 ms |
17228 KB |
Output is correct |
19 |
Correct |
160 ms |
14160 KB |
Output is correct |
20 |
Correct |
175 ms |
15696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
168 ms |
16052 KB |
Output is correct |
12 |
Correct |
181 ms |
15732 KB |
Output is correct |
13 |
Correct |
194 ms |
14928 KB |
Output is correct |
14 |
Correct |
234 ms |
15184 KB |
Output is correct |
15 |
Correct |
195 ms |
16024 KB |
Output is correct |
16 |
Correct |
223 ms |
15332 KB |
Output is correct |
17 |
Correct |
217 ms |
15184 KB |
Output is correct |
18 |
Correct |
171 ms |
17228 KB |
Output is correct |
19 |
Correct |
160 ms |
14160 KB |
Output is correct |
20 |
Correct |
175 ms |
15696 KB |
Output is correct |
21 |
Correct |
195 ms |
18880 KB |
Output is correct |
22 |
Correct |
209 ms |
19276 KB |
Output is correct |
23 |
Correct |
227 ms |
18132 KB |
Output is correct |
24 |
Correct |
275 ms |
18168 KB |
Output is correct |
25 |
Correct |
230 ms |
20048 KB |
Output is correct |
26 |
Correct |
269 ms |
18624 KB |
Output is correct |
27 |
Correct |
256 ms |
18536 KB |
Output is correct |
28 |
Correct |
159 ms |
21100 KB |
Output is correct |
29 |
Correct |
186 ms |
17068 KB |
Output is correct |
30 |
Correct |
203 ms |
19032 KB |
Output is correct |
31 |
Correct |
255 ms |
19024 KB |
Output is correct |
32 |
Correct |
277 ms |
18984 KB |
Output is correct |
33 |
Correct |
208 ms |
18000 KB |
Output is correct |
34 |
Correct |
267 ms |
18096 KB |
Output is correct |
35 |
Correct |
251 ms |
18548 KB |
Output is correct |
36 |
Correct |
146 ms |
17020 KB |
Output is correct |
37 |
Correct |
201 ms |
19028 KB |
Output is correct |
38 |
Correct |
197 ms |
17012 KB |
Output is correct |
39 |
Correct |
242 ms |
18160 KB |
Output is correct |
40 |
Correct |
271 ms |
18000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
35 ms |
12080 KB |
Output is correct |
12 |
Correct |
37 ms |
12084 KB |
Output is correct |
13 |
Correct |
41 ms |
12032 KB |
Output is correct |
14 |
Correct |
49 ms |
12020 KB |
Output is correct |
15 |
Correct |
41 ms |
11980 KB |
Output is correct |
16 |
Correct |
54 ms |
12020 KB |
Output is correct |
17 |
Correct |
41 ms |
11840 KB |
Output is correct |
18 |
Correct |
31 ms |
12088 KB |
Output is correct |
19 |
Correct |
36 ms |
11764 KB |
Output is correct |
20 |
Correct |
35 ms |
12088 KB |
Output is correct |
21 |
Correct |
44 ms |
12012 KB |
Output is correct |
22 |
Correct |
51 ms |
12036 KB |
Output is correct |
23 |
Correct |
37 ms |
11860 KB |
Output is correct |
24 |
Correct |
55 ms |
11956 KB |
Output is correct |
25 |
Correct |
42 ms |
12020 KB |
Output is correct |
26 |
Correct |
31 ms |
11944 KB |
Output is correct |
27 |
Correct |
36 ms |
12096 KB |
Output is correct |
28 |
Correct |
44 ms |
11964 KB |
Output is correct |
29 |
Correct |
39 ms |
12016 KB |
Output is correct |
30 |
Correct |
40 ms |
11900 KB |
Output is correct |
31 |
Correct |
34 ms |
11848 KB |
Output is correct |
32 |
Correct |
61 ms |
12060 KB |
Output is correct |
33 |
Correct |
47 ms |
11856 KB |
Output is correct |
34 |
Correct |
32 ms |
11772 KB |
Output is correct |
35 |
Correct |
43 ms |
11852 KB |
Output is correct |
36 |
Correct |
41 ms |
12020 KB |
Output is correct |
37 |
Correct |
65 ms |
11840 KB |
Output is correct |
38 |
Correct |
39 ms |
11840 KB |
Output is correct |
39 |
Correct |
43 ms |
11856 KB |
Output is correct |
40 |
Correct |
40 ms |
12024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
168 ms |
16052 KB |
Output is correct |
12 |
Correct |
181 ms |
15732 KB |
Output is correct |
13 |
Correct |
194 ms |
14928 KB |
Output is correct |
14 |
Correct |
234 ms |
15184 KB |
Output is correct |
15 |
Correct |
195 ms |
16024 KB |
Output is correct |
16 |
Correct |
223 ms |
15332 KB |
Output is correct |
17 |
Correct |
217 ms |
15184 KB |
Output is correct |
18 |
Correct |
171 ms |
17228 KB |
Output is correct |
19 |
Correct |
160 ms |
14160 KB |
Output is correct |
20 |
Correct |
175 ms |
15696 KB |
Output is correct |
21 |
Correct |
195 ms |
18880 KB |
Output is correct |
22 |
Correct |
209 ms |
19276 KB |
Output is correct |
23 |
Correct |
227 ms |
18132 KB |
Output is correct |
24 |
Correct |
275 ms |
18168 KB |
Output is correct |
25 |
Correct |
230 ms |
20048 KB |
Output is correct |
26 |
Correct |
269 ms |
18624 KB |
Output is correct |
27 |
Correct |
256 ms |
18536 KB |
Output is correct |
28 |
Correct |
159 ms |
21100 KB |
Output is correct |
29 |
Correct |
186 ms |
17068 KB |
Output is correct |
30 |
Correct |
203 ms |
19032 KB |
Output is correct |
31 |
Correct |
255 ms |
19024 KB |
Output is correct |
32 |
Correct |
277 ms |
18984 KB |
Output is correct |
33 |
Correct |
208 ms |
18000 KB |
Output is correct |
34 |
Correct |
267 ms |
18096 KB |
Output is correct |
35 |
Correct |
251 ms |
18548 KB |
Output is correct |
36 |
Correct |
146 ms |
17020 KB |
Output is correct |
37 |
Correct |
201 ms |
19028 KB |
Output is correct |
38 |
Correct |
197 ms |
17012 KB |
Output is correct |
39 |
Correct |
242 ms |
18160 KB |
Output is correct |
40 |
Correct |
271 ms |
18000 KB |
Output is correct |
41 |
Correct |
35 ms |
12080 KB |
Output is correct |
42 |
Correct |
37 ms |
12084 KB |
Output is correct |
43 |
Correct |
41 ms |
12032 KB |
Output is correct |
44 |
Correct |
49 ms |
12020 KB |
Output is correct |
45 |
Correct |
41 ms |
11980 KB |
Output is correct |
46 |
Correct |
54 ms |
12020 KB |
Output is correct |
47 |
Correct |
41 ms |
11840 KB |
Output is correct |
48 |
Correct |
31 ms |
12088 KB |
Output is correct |
49 |
Correct |
36 ms |
11764 KB |
Output is correct |
50 |
Correct |
35 ms |
12088 KB |
Output is correct |
51 |
Correct |
44 ms |
12012 KB |
Output is correct |
52 |
Correct |
51 ms |
12036 KB |
Output is correct |
53 |
Correct |
37 ms |
11860 KB |
Output is correct |
54 |
Correct |
55 ms |
11956 KB |
Output is correct |
55 |
Correct |
42 ms |
12020 KB |
Output is correct |
56 |
Correct |
31 ms |
11944 KB |
Output is correct |
57 |
Correct |
36 ms |
12096 KB |
Output is correct |
58 |
Correct |
44 ms |
11964 KB |
Output is correct |
59 |
Correct |
39 ms |
12016 KB |
Output is correct |
60 |
Correct |
40 ms |
11900 KB |
Output is correct |
61 |
Correct |
34 ms |
11848 KB |
Output is correct |
62 |
Correct |
61 ms |
12060 KB |
Output is correct |
63 |
Correct |
47 ms |
11856 KB |
Output is correct |
64 |
Correct |
32 ms |
11772 KB |
Output is correct |
65 |
Correct |
43 ms |
11852 KB |
Output is correct |
66 |
Correct |
41 ms |
12020 KB |
Output is correct |
67 |
Correct |
65 ms |
11840 KB |
Output is correct |
68 |
Correct |
39 ms |
11840 KB |
Output is correct |
69 |
Correct |
43 ms |
11856 KB |
Output is correct |
70 |
Correct |
40 ms |
12024 KB |
Output is correct |
71 |
Correct |
353 ms |
34188 KB |
Output is correct |
72 |
Correct |
380 ms |
34216 KB |
Output is correct |
73 |
Correct |
433 ms |
32960 KB |
Output is correct |
74 |
Correct |
674 ms |
30528 KB |
Output is correct |
75 |
Correct |
352 ms |
32372 KB |
Output is correct |
76 |
Correct |
597 ms |
30708 KB |
Output is correct |
77 |
Correct |
526 ms |
30588 KB |
Output is correct |
78 |
Correct |
259 ms |
34256 KB |
Output is correct |
79 |
Correct |
317 ms |
28416 KB |
Output is correct |
80 |
Correct |
411 ms |
31548 KB |
Output is correct |
81 |
Correct |
462 ms |
31248 KB |
Output is correct |
82 |
Correct |
660 ms |
30424 KB |
Output is correct |
83 |
Correct |
351 ms |
29564 KB |
Output is correct |
84 |
Correct |
639 ms |
30284 KB |
Output is correct |
85 |
Correct |
560 ms |
30732 KB |
Output is correct |
86 |
Correct |
213 ms |
28212 KB |
Output is correct |
87 |
Correct |
329 ms |
31284 KB |
Output is correct |
88 |
Correct |
298 ms |
28400 KB |
Output is correct |
89 |
Correct |
497 ms |
30256 KB |
Output is correct |
90 |
Correct |
438 ms |
30496 KB |
Output is correct |
91 |
Correct |
333 ms |
29700 KB |
Output is correct |
92 |
Correct |
649 ms |
30912 KB |
Output is correct |
93 |
Correct |
574 ms |
30452 KB |
Output is correct |
94 |
Correct |
222 ms |
28220 KB |
Output is correct |
95 |
Correct |
546 ms |
30508 KB |
Output is correct |
96 |
Correct |
570 ms |
30416 KB |
Output is correct |
97 |
Correct |
565 ms |
30464 KB |
Output is correct |
98 |
Correct |
519 ms |
30364 KB |
Output is correct |
99 |
Correct |
518 ms |
30316 KB |
Output is correct |
100 |
Correct |
520 ms |
30556 KB |
Output is correct |