#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define int long long
int DP1[1048576];
int DP2[1048576];
int arr[1048576];
int idx[6];
inline int preprocess(const string &s,char c){
int ans = 0;
int cnt = 0;
for(int bit=0;bit<min(20,(int32_t)s.size());bit++){
if(s[bit]==c){
idx[cnt++]=bit;
} else if(s[bit]=='?'){
if(c=='1')ans|=(1<<bit);
} else {
if(s[bit]=='1')ans|=(1<<bit);
}
}
return ans;
}
inline int applymask(int qu,int mask,int cnt){
for(int bit=0;bit<min(6ll,cnt);bit++){
if(mask&(1<<bit))qu|=(1<<idx[bit]);
}
return qu;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int l,q;
cin >> l >> q;
for(int mask=0;mask<(1<<l);mask++){
char a;cin>>a;a-='0';
DP1[mask]=DP2[mask]=arr[mask]=a;
}
for(int bit=0;bit<20;bit++){
for(int mask=0;mask<(1<<20);mask++){
if(mask&(1<<bit)){
DP2[mask^(1<<bit)]+=DP2[mask];
} else {
DP1[mask|(1<<bit)]+=DP1[mask];
}
}
}
for(int i=1;i<=q;i++){
string s;cin>>s;
reverse(s.begin(),s.end());
int c1=0,c0=0,cq=0;
for(char&j:s) {
if(j=='1')c1++;
else if(j=='0')c0++;
else if(j=='?')cq++;
}
if(cq<=6){
int qu = preprocess(s,'?');
int ans = 0;
for(int mask=0;mask<(1<<cq);mask++){
ans+=arr[applymask(qu,mask,cq)];
}
cout<<ans<<'\n';
continue;
}
if(c1<=6){
int qu = preprocess(s,'1');
int ans = 0;
for(int mask=0;mask<(1<<c1);mask++){
if(__builtin_parity(mask)==(c1&1)){
ans+=DP1[applymask(qu,mask,c1)];
} else {
ans-=DP1[applymask(qu,mask,c1)];
}
}
cout<<ans<<'\n';
continue;
}
if(c0<=6){
int qu = preprocess(s,'0');
int ans = 0;
for(int mask=0;mask<(1<<c0);mask++){
if(__builtin_parity(mask)){
ans-=DP2[applymask(qu,mask,c0)];
} else {
ans+=DP2[applymask(qu,mask,c0)];
}
}
cout<<ans<<'\n';
continue;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
18520 KB |
Output is correct |
2 |
Correct |
27 ms |
18520 KB |
Output is correct |
3 |
Correct |
27 ms |
18520 KB |
Output is correct |
4 |
Correct |
29 ms |
18520 KB |
Output is correct |
5 |
Correct |
30 ms |
18520 KB |
Output is correct |
6 |
Correct |
26 ms |
18524 KB |
Output is correct |
7 |
Correct |
29 ms |
18524 KB |
Output is correct |
8 |
Correct |
28 ms |
18520 KB |
Output is correct |
9 |
Correct |
29 ms |
18520 KB |
Output is correct |
10 |
Correct |
30 ms |
18520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
18520 KB |
Output is correct |
2 |
Correct |
27 ms |
18520 KB |
Output is correct |
3 |
Correct |
27 ms |
18520 KB |
Output is correct |
4 |
Correct |
29 ms |
18520 KB |
Output is correct |
5 |
Correct |
30 ms |
18520 KB |
Output is correct |
6 |
Correct |
26 ms |
18524 KB |
Output is correct |
7 |
Correct |
29 ms |
18524 KB |
Output is correct |
8 |
Correct |
28 ms |
18520 KB |
Output is correct |
9 |
Correct |
29 ms |
18520 KB |
Output is correct |
10 |
Correct |
30 ms |
18520 KB |
Output is correct |
11 |
Correct |
427 ms |
22748 KB |
Output is correct |
12 |
Correct |
459 ms |
22384 KB |
Output is correct |
13 |
Correct |
337 ms |
21580 KB |
Output is correct |
14 |
Correct |
338 ms |
21612 KB |
Output is correct |
15 |
Correct |
792 ms |
22524 KB |
Output is correct |
16 |
Correct |
424 ms |
21748 KB |
Output is correct |
17 |
Correct |
406 ms |
21728 KB |
Output is correct |
18 |
Correct |
216 ms |
23632 KB |
Output is correct |
19 |
Correct |
241 ms |
20560 KB |
Output is correct |
20 |
Correct |
463 ms |
22444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
18520 KB |
Output is correct |
2 |
Correct |
27 ms |
18520 KB |
Output is correct |
3 |
Correct |
27 ms |
18520 KB |
Output is correct |
4 |
Correct |
29 ms |
18520 KB |
Output is correct |
5 |
Correct |
30 ms |
18520 KB |
Output is correct |
6 |
Correct |
26 ms |
18524 KB |
Output is correct |
7 |
Correct |
29 ms |
18524 KB |
Output is correct |
8 |
Correct |
28 ms |
18520 KB |
Output is correct |
9 |
Correct |
29 ms |
18520 KB |
Output is correct |
10 |
Correct |
30 ms |
18520 KB |
Output is correct |
11 |
Correct |
427 ms |
22748 KB |
Output is correct |
12 |
Correct |
459 ms |
22384 KB |
Output is correct |
13 |
Correct |
337 ms |
21580 KB |
Output is correct |
14 |
Correct |
338 ms |
21612 KB |
Output is correct |
15 |
Correct |
792 ms |
22524 KB |
Output is correct |
16 |
Correct |
424 ms |
21748 KB |
Output is correct |
17 |
Correct |
406 ms |
21728 KB |
Output is correct |
18 |
Correct |
216 ms |
23632 KB |
Output is correct |
19 |
Correct |
241 ms |
20560 KB |
Output is correct |
20 |
Correct |
463 ms |
22444 KB |
Output is correct |
21 |
Correct |
919 ms |
22608 KB |
Output is correct |
22 |
Correct |
479 ms |
22864 KB |
Output is correct |
23 |
Correct |
442 ms |
21940 KB |
Output is correct |
24 |
Correct |
424 ms |
21584 KB |
Output is correct |
25 |
Correct |
359 ms |
23632 KB |
Output is correct |
26 |
Correct |
529 ms |
22100 KB |
Output is correct |
27 |
Correct |
521 ms |
22096 KB |
Output is correct |
28 |
Correct |
236 ms |
24616 KB |
Output is correct |
29 |
Correct |
293 ms |
20904 KB |
Output is correct |
30 |
Correct |
676 ms |
22932 KB |
Output is correct |
31 |
Correct |
859 ms |
22628 KB |
Output is correct |
32 |
Correct |
542 ms |
22628 KB |
Output is correct |
33 |
Correct |
358 ms |
21840 KB |
Output is correct |
34 |
Correct |
431 ms |
21784 KB |
Output is correct |
35 |
Correct |
529 ms |
22252 KB |
Output is correct |
36 |
Correct |
213 ms |
20640 KB |
Output is correct |
37 |
Correct |
806 ms |
22732 KB |
Output is correct |
38 |
Correct |
321 ms |
20724 KB |
Output is correct |
39 |
Correct |
466 ms |
21840 KB |
Output is correct |
40 |
Correct |
424 ms |
21584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
18520 KB |
Output is correct |
2 |
Correct |
27 ms |
18520 KB |
Output is correct |
3 |
Correct |
27 ms |
18520 KB |
Output is correct |
4 |
Correct |
29 ms |
18520 KB |
Output is correct |
5 |
Correct |
30 ms |
18520 KB |
Output is correct |
6 |
Correct |
26 ms |
18524 KB |
Output is correct |
7 |
Correct |
29 ms |
18524 KB |
Output is correct |
8 |
Correct |
28 ms |
18520 KB |
Output is correct |
9 |
Correct |
29 ms |
18520 KB |
Output is correct |
10 |
Correct |
30 ms |
18520 KB |
Output is correct |
11 |
Correct |
53 ms |
25176 KB |
Output is correct |
12 |
Correct |
62 ms |
25320 KB |
Output is correct |
13 |
Correct |
66 ms |
25196 KB |
Output is correct |
14 |
Correct |
86 ms |
25228 KB |
Output is correct |
15 |
Correct |
56 ms |
25308 KB |
Output is correct |
16 |
Correct |
120 ms |
25404 KB |
Output is correct |
17 |
Correct |
105 ms |
25244 KB |
Output is correct |
18 |
Correct |
50 ms |
25428 KB |
Output is correct |
19 |
Correct |
73 ms |
25476 KB |
Output is correct |
20 |
Correct |
57 ms |
25128 KB |
Output is correct |
21 |
Correct |
69 ms |
25060 KB |
Output is correct |
22 |
Correct |
86 ms |
25112 KB |
Output is correct |
23 |
Correct |
60 ms |
25168 KB |
Output is correct |
24 |
Correct |
88 ms |
25168 KB |
Output is correct |
25 |
Correct |
113 ms |
25200 KB |
Output is correct |
26 |
Correct |
50 ms |
24924 KB |
Output is correct |
27 |
Correct |
60 ms |
25168 KB |
Output is correct |
28 |
Correct |
59 ms |
25156 KB |
Output is correct |
29 |
Correct |
72 ms |
25056 KB |
Output is correct |
30 |
Correct |
82 ms |
25128 KB |
Output is correct |
31 |
Correct |
66 ms |
25224 KB |
Output is correct |
32 |
Correct |
93 ms |
25172 KB |
Output is correct |
33 |
Correct |
114 ms |
25052 KB |
Output is correct |
34 |
Correct |
52 ms |
25072 KB |
Output is correct |
35 |
Correct |
97 ms |
25172 KB |
Output is correct |
36 |
Correct |
104 ms |
25220 KB |
Output is correct |
37 |
Correct |
98 ms |
25264 KB |
Output is correct |
38 |
Correct |
105 ms |
25156 KB |
Output is correct |
39 |
Correct |
99 ms |
25068 KB |
Output is correct |
40 |
Correct |
101 ms |
25156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
18520 KB |
Output is correct |
2 |
Correct |
27 ms |
18520 KB |
Output is correct |
3 |
Correct |
27 ms |
18520 KB |
Output is correct |
4 |
Correct |
29 ms |
18520 KB |
Output is correct |
5 |
Correct |
30 ms |
18520 KB |
Output is correct |
6 |
Correct |
26 ms |
18524 KB |
Output is correct |
7 |
Correct |
29 ms |
18524 KB |
Output is correct |
8 |
Correct |
28 ms |
18520 KB |
Output is correct |
9 |
Correct |
29 ms |
18520 KB |
Output is correct |
10 |
Correct |
30 ms |
18520 KB |
Output is correct |
11 |
Correct |
427 ms |
22748 KB |
Output is correct |
12 |
Correct |
459 ms |
22384 KB |
Output is correct |
13 |
Correct |
337 ms |
21580 KB |
Output is correct |
14 |
Correct |
338 ms |
21612 KB |
Output is correct |
15 |
Correct |
792 ms |
22524 KB |
Output is correct |
16 |
Correct |
424 ms |
21748 KB |
Output is correct |
17 |
Correct |
406 ms |
21728 KB |
Output is correct |
18 |
Correct |
216 ms |
23632 KB |
Output is correct |
19 |
Correct |
241 ms |
20560 KB |
Output is correct |
20 |
Correct |
463 ms |
22444 KB |
Output is correct |
21 |
Correct |
919 ms |
22608 KB |
Output is correct |
22 |
Correct |
479 ms |
22864 KB |
Output is correct |
23 |
Correct |
442 ms |
21940 KB |
Output is correct |
24 |
Correct |
424 ms |
21584 KB |
Output is correct |
25 |
Correct |
359 ms |
23632 KB |
Output is correct |
26 |
Correct |
529 ms |
22100 KB |
Output is correct |
27 |
Correct |
521 ms |
22096 KB |
Output is correct |
28 |
Correct |
236 ms |
24616 KB |
Output is correct |
29 |
Correct |
293 ms |
20904 KB |
Output is correct |
30 |
Correct |
676 ms |
22932 KB |
Output is correct |
31 |
Correct |
859 ms |
22628 KB |
Output is correct |
32 |
Correct |
542 ms |
22628 KB |
Output is correct |
33 |
Correct |
358 ms |
21840 KB |
Output is correct |
34 |
Correct |
431 ms |
21784 KB |
Output is correct |
35 |
Correct |
529 ms |
22252 KB |
Output is correct |
36 |
Correct |
213 ms |
20640 KB |
Output is correct |
37 |
Correct |
806 ms |
22732 KB |
Output is correct |
38 |
Correct |
321 ms |
20724 KB |
Output is correct |
39 |
Correct |
466 ms |
21840 KB |
Output is correct |
40 |
Correct |
424 ms |
21584 KB |
Output is correct |
41 |
Correct |
53 ms |
25176 KB |
Output is correct |
42 |
Correct |
62 ms |
25320 KB |
Output is correct |
43 |
Correct |
66 ms |
25196 KB |
Output is correct |
44 |
Correct |
86 ms |
25228 KB |
Output is correct |
45 |
Correct |
56 ms |
25308 KB |
Output is correct |
46 |
Correct |
120 ms |
25404 KB |
Output is correct |
47 |
Correct |
105 ms |
25244 KB |
Output is correct |
48 |
Correct |
50 ms |
25428 KB |
Output is correct |
49 |
Correct |
73 ms |
25476 KB |
Output is correct |
50 |
Correct |
57 ms |
25128 KB |
Output is correct |
51 |
Correct |
69 ms |
25060 KB |
Output is correct |
52 |
Correct |
86 ms |
25112 KB |
Output is correct |
53 |
Correct |
60 ms |
25168 KB |
Output is correct |
54 |
Correct |
88 ms |
25168 KB |
Output is correct |
55 |
Correct |
113 ms |
25200 KB |
Output is correct |
56 |
Correct |
50 ms |
24924 KB |
Output is correct |
57 |
Correct |
60 ms |
25168 KB |
Output is correct |
58 |
Correct |
59 ms |
25156 KB |
Output is correct |
59 |
Correct |
72 ms |
25056 KB |
Output is correct |
60 |
Correct |
82 ms |
25128 KB |
Output is correct |
61 |
Correct |
66 ms |
25224 KB |
Output is correct |
62 |
Correct |
93 ms |
25172 KB |
Output is correct |
63 |
Correct |
114 ms |
25052 KB |
Output is correct |
64 |
Correct |
52 ms |
25072 KB |
Output is correct |
65 |
Correct |
97 ms |
25172 KB |
Output is correct |
66 |
Correct |
104 ms |
25220 KB |
Output is correct |
67 |
Correct |
98 ms |
25264 KB |
Output is correct |
68 |
Correct |
105 ms |
25156 KB |
Output is correct |
69 |
Correct |
99 ms |
25068 KB |
Output is correct |
70 |
Correct |
101 ms |
25156 KB |
Output is correct |
71 |
Correct |
437 ms |
30168 KB |
Output is correct |
72 |
Correct |
436 ms |
30032 KB |
Output is correct |
73 |
Correct |
655 ms |
28940 KB |
Output is correct |
74 |
Correct |
1042 ms |
29088 KB |
Output is correct |
75 |
Correct |
518 ms |
30896 KB |
Output is correct |
76 |
Correct |
1780 ms |
29180 KB |
Output is correct |
77 |
Correct |
1412 ms |
29380 KB |
Output is correct |
78 |
Correct |
315 ms |
33104 KB |
Output is correct |
79 |
Correct |
396 ms |
26988 KB |
Output is correct |
80 |
Correct |
481 ms |
30308 KB |
Output is correct |
81 |
Correct |
709 ms |
29984 KB |
Output is correct |
82 |
Correct |
1022 ms |
29144 KB |
Output is correct |
83 |
Correct |
494 ms |
28240 KB |
Output is correct |
84 |
Correct |
966 ms |
29008 KB |
Output is correct |
85 |
Correct |
1564 ms |
29500 KB |
Output is correct |
86 |
Correct |
308 ms |
26960 KB |
Output is correct |
87 |
Correct |
415 ms |
30240 KB |
Output is correct |
88 |
Correct |
392 ms |
26960 KB |
Output is correct |
89 |
Correct |
665 ms |
28612 KB |
Output is correct |
90 |
Correct |
971 ms |
29020 KB |
Output is correct |
91 |
Correct |
497 ms |
28112 KB |
Output is correct |
92 |
Correct |
1270 ms |
29164 KB |
Output is correct |
93 |
Correct |
1515 ms |
29648 KB |
Output is correct |
94 |
Correct |
277 ms |
26980 KB |
Output is correct |
95 |
Correct |
1160 ms |
29172 KB |
Output is correct |
96 |
Correct |
1166 ms |
29100 KB |
Output is correct |
97 |
Correct |
1179 ms |
29072 KB |
Output is correct |
98 |
Correct |
1170 ms |
29248 KB |
Output is correct |
99 |
Correct |
1092 ms |
29160 KB |
Output is correct |
100 |
Correct |
1155 ms |
29008 KB |
Output is correct |