// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
#define int long long
#define pii pair<int,int>
const int inf=1e18;
const int mod=998244353;
const int maxn=100005;
const int maxl=20;
const int maxa=1000005;
int power(int a,int n){
int res=1;
while(n){
if(n&1) res=res*a%mod;
a=a*a%mod;n>>=1;
}
return res;
}
int n,q,f1[(1<<20)+5],f2[(1<<20)+5],a[(1<<20)+5];
void solve(){
cin >> n >> q;
for(int i=0;i<(1<<n);i++){
char c;cin >> c;
f1[i]=f2[i]=a[i]=c-'0';
}
for(int i=0;i<n;i++){
for(int j=0;j<(1<<n);j++){
if(j&(1<<i)) f1[j]+=f1[j^(1<<i)];
else f2[j]+=f2[j^(1<<i)];
}
}
for(int _=1;_<=q;_++){
string s;cin >> s;
int num=0,m0=0,m1=0,m2=0,cnt0=0,cnt1=0,cnt2=0;
for(int i=0;i<n;i++){
if(s[i]!='?'){
num=num*2+(s[i]-'0');
if(s[i]=='0'){cnt0++;m0+=(1<<(n-i-1));}
else{cnt1++;;m1+=(1<<(n-i-1));}
}
else{num=num*2+1;m2+=(1<<(n-i-1));cnt2++;}
}
int res=0;
if(cnt0<=cnt1 && cnt0<=cnt2){
num^=m2;
for(int i=m0;;i=(i-1)&m0){
if(__builtin_popcount(i)&1) res-=f2[num^i];
else res+=f2[num^i];
if(i==0) break;
}
}
else if(cnt1<=cnt0 && cnt1<=cnt2){
for(int i=m1;;i=(i-1)&m1){
if(__builtin_popcount(i)&1) res-=f1[num^i];
else res+=f1[num^i];
if(i==0) break;
}
}
else{
for(int i=m2;;i=(i-1)&m2){
res+=a[num^i];
if(i==0) break;
}
}
cout << res << '\n';
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int t=1;//cin >> t;
while(t--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
183 ms |
4276 KB |
Output is correct |
12 |
Correct |
203 ms |
4004 KB |
Output is correct |
13 |
Correct |
199 ms |
3252 KB |
Output is correct |
14 |
Correct |
228 ms |
3252 KB |
Output is correct |
15 |
Correct |
227 ms |
4428 KB |
Output is correct |
16 |
Correct |
275 ms |
3428 KB |
Output is correct |
17 |
Correct |
243 ms |
3492 KB |
Output is correct |
18 |
Correct |
152 ms |
5364 KB |
Output is correct |
19 |
Correct |
181 ms |
2276 KB |
Output is correct |
20 |
Correct |
193 ms |
3960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
183 ms |
4276 KB |
Output is correct |
12 |
Correct |
203 ms |
4004 KB |
Output is correct |
13 |
Correct |
199 ms |
3252 KB |
Output is correct |
14 |
Correct |
228 ms |
3252 KB |
Output is correct |
15 |
Correct |
227 ms |
4428 KB |
Output is correct |
16 |
Correct |
275 ms |
3428 KB |
Output is correct |
17 |
Correct |
243 ms |
3492 KB |
Output is correct |
18 |
Correct |
152 ms |
5364 KB |
Output is correct |
19 |
Correct |
181 ms |
2276 KB |
Output is correct |
20 |
Correct |
193 ms |
3960 KB |
Output is correct |
21 |
Correct |
233 ms |
4464 KB |
Output is correct |
22 |
Correct |
220 ms |
4600 KB |
Output is correct |
23 |
Correct |
249 ms |
3688 KB |
Output is correct |
24 |
Correct |
277 ms |
3448 KB |
Output is correct |
25 |
Correct |
236 ms |
5456 KB |
Output is correct |
26 |
Correct |
273 ms |
3972 KB |
Output is correct |
27 |
Correct |
272 ms |
4044 KB |
Output is correct |
28 |
Correct |
158 ms |
6508 KB |
Output is correct |
29 |
Correct |
195 ms |
2424 KB |
Output is correct |
30 |
Correct |
204 ms |
4632 KB |
Output is correct |
31 |
Correct |
265 ms |
4592 KB |
Output is correct |
32 |
Correct |
287 ms |
4480 KB |
Output is correct |
33 |
Correct |
213 ms |
3404 KB |
Output is correct |
34 |
Correct |
277 ms |
3516 KB |
Output is correct |
35 |
Correct |
262 ms |
3884 KB |
Output is correct |
36 |
Correct |
156 ms |
2584 KB |
Output is correct |
37 |
Correct |
200 ms |
4448 KB |
Output is correct |
38 |
Correct |
202 ms |
2452 KB |
Output is correct |
39 |
Correct |
245 ms |
3708 KB |
Output is correct |
40 |
Correct |
275 ms |
3536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
78 ms |
25188 KB |
Output is correct |
12 |
Correct |
82 ms |
25068 KB |
Output is correct |
13 |
Correct |
74 ms |
25036 KB |
Output is correct |
14 |
Correct |
91 ms |
25032 KB |
Output is correct |
15 |
Correct |
69 ms |
25164 KB |
Output is correct |
16 |
Correct |
102 ms |
25208 KB |
Output is correct |
17 |
Correct |
91 ms |
25132 KB |
Output is correct |
18 |
Correct |
63 ms |
25328 KB |
Output is correct |
19 |
Correct |
68 ms |
24948 KB |
Output is correct |
20 |
Correct |
68 ms |
25148 KB |
Output is correct |
21 |
Correct |
90 ms |
25164 KB |
Output is correct |
22 |
Correct |
115 ms |
25104 KB |
Output is correct |
23 |
Correct |
72 ms |
25056 KB |
Output is correct |
24 |
Correct |
96 ms |
25056 KB |
Output is correct |
25 |
Correct |
90 ms |
25128 KB |
Output is correct |
26 |
Correct |
67 ms |
24928 KB |
Output is correct |
27 |
Correct |
68 ms |
25184 KB |
Output is correct |
28 |
Correct |
68 ms |
24928 KB |
Output is correct |
29 |
Correct |
75 ms |
25104 KB |
Output is correct |
30 |
Correct |
79 ms |
25064 KB |
Output is correct |
31 |
Correct |
76 ms |
24964 KB |
Output is correct |
32 |
Correct |
100 ms |
25056 KB |
Output is correct |
33 |
Correct |
94 ms |
25164 KB |
Output is correct |
34 |
Correct |
63 ms |
24924 KB |
Output is correct |
35 |
Correct |
82 ms |
25064 KB |
Output is correct |
36 |
Correct |
83 ms |
25092 KB |
Output is correct |
37 |
Correct |
90 ms |
25060 KB |
Output is correct |
38 |
Correct |
90 ms |
25316 KB |
Output is correct |
39 |
Correct |
83 ms |
25068 KB |
Output is correct |
40 |
Correct |
82 ms |
25124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
183 ms |
4276 KB |
Output is correct |
12 |
Correct |
203 ms |
4004 KB |
Output is correct |
13 |
Correct |
199 ms |
3252 KB |
Output is correct |
14 |
Correct |
228 ms |
3252 KB |
Output is correct |
15 |
Correct |
227 ms |
4428 KB |
Output is correct |
16 |
Correct |
275 ms |
3428 KB |
Output is correct |
17 |
Correct |
243 ms |
3492 KB |
Output is correct |
18 |
Correct |
152 ms |
5364 KB |
Output is correct |
19 |
Correct |
181 ms |
2276 KB |
Output is correct |
20 |
Correct |
193 ms |
3960 KB |
Output is correct |
21 |
Correct |
233 ms |
4464 KB |
Output is correct |
22 |
Correct |
220 ms |
4600 KB |
Output is correct |
23 |
Correct |
249 ms |
3688 KB |
Output is correct |
24 |
Correct |
277 ms |
3448 KB |
Output is correct |
25 |
Correct |
236 ms |
5456 KB |
Output is correct |
26 |
Correct |
273 ms |
3972 KB |
Output is correct |
27 |
Correct |
272 ms |
4044 KB |
Output is correct |
28 |
Correct |
158 ms |
6508 KB |
Output is correct |
29 |
Correct |
195 ms |
2424 KB |
Output is correct |
30 |
Correct |
204 ms |
4632 KB |
Output is correct |
31 |
Correct |
265 ms |
4592 KB |
Output is correct |
32 |
Correct |
287 ms |
4480 KB |
Output is correct |
33 |
Correct |
213 ms |
3404 KB |
Output is correct |
34 |
Correct |
277 ms |
3516 KB |
Output is correct |
35 |
Correct |
262 ms |
3884 KB |
Output is correct |
36 |
Correct |
156 ms |
2584 KB |
Output is correct |
37 |
Correct |
200 ms |
4448 KB |
Output is correct |
38 |
Correct |
202 ms |
2452 KB |
Output is correct |
39 |
Correct |
245 ms |
3708 KB |
Output is correct |
40 |
Correct |
275 ms |
3536 KB |
Output is correct |
41 |
Correct |
78 ms |
25188 KB |
Output is correct |
42 |
Correct |
82 ms |
25068 KB |
Output is correct |
43 |
Correct |
74 ms |
25036 KB |
Output is correct |
44 |
Correct |
91 ms |
25032 KB |
Output is correct |
45 |
Correct |
69 ms |
25164 KB |
Output is correct |
46 |
Correct |
102 ms |
25208 KB |
Output is correct |
47 |
Correct |
91 ms |
25132 KB |
Output is correct |
48 |
Correct |
63 ms |
25328 KB |
Output is correct |
49 |
Correct |
68 ms |
24948 KB |
Output is correct |
50 |
Correct |
68 ms |
25148 KB |
Output is correct |
51 |
Correct |
90 ms |
25164 KB |
Output is correct |
52 |
Correct |
115 ms |
25104 KB |
Output is correct |
53 |
Correct |
72 ms |
25056 KB |
Output is correct |
54 |
Correct |
96 ms |
25056 KB |
Output is correct |
55 |
Correct |
90 ms |
25128 KB |
Output is correct |
56 |
Correct |
67 ms |
24928 KB |
Output is correct |
57 |
Correct |
68 ms |
25184 KB |
Output is correct |
58 |
Correct |
68 ms |
24928 KB |
Output is correct |
59 |
Correct |
75 ms |
25104 KB |
Output is correct |
60 |
Correct |
79 ms |
25064 KB |
Output is correct |
61 |
Correct |
76 ms |
24964 KB |
Output is correct |
62 |
Correct |
100 ms |
25056 KB |
Output is correct |
63 |
Correct |
94 ms |
25164 KB |
Output is correct |
64 |
Correct |
63 ms |
24924 KB |
Output is correct |
65 |
Correct |
82 ms |
25064 KB |
Output is correct |
66 |
Correct |
83 ms |
25092 KB |
Output is correct |
67 |
Correct |
90 ms |
25060 KB |
Output is correct |
68 |
Correct |
90 ms |
25316 KB |
Output is correct |
69 |
Correct |
83 ms |
25068 KB |
Output is correct |
70 |
Correct |
82 ms |
25124 KB |
Output is correct |
71 |
Correct |
394 ms |
29892 KB |
Output is correct |
72 |
Correct |
376 ms |
30028 KB |
Output is correct |
73 |
Correct |
531 ms |
28564 KB |
Output is correct |
74 |
Correct |
892 ms |
28952 KB |
Output is correct |
75 |
Correct |
437 ms |
30940 KB |
Output is correct |
76 |
Correct |
1016 ms |
29344 KB |
Output is correct |
77 |
Correct |
808 ms |
29092 KB |
Output is correct |
78 |
Correct |
250 ms |
32944 KB |
Output is correct |
79 |
Correct |
361 ms |
26956 KB |
Output is correct |
80 |
Correct |
377 ms |
30028 KB |
Output is correct |
81 |
Correct |
597 ms |
29936 KB |
Output is correct |
82 |
Correct |
894 ms |
28940 KB |
Output is correct |
83 |
Correct |
368 ms |
28044 KB |
Output is correct |
84 |
Correct |
970 ms |
28928 KB |
Output is correct |
85 |
Correct |
821 ms |
29084 KB |
Output is correct |
86 |
Correct |
256 ms |
26840 KB |
Output is correct |
87 |
Correct |
357 ms |
29940 KB |
Output is correct |
88 |
Correct |
371 ms |
26952 KB |
Output is correct |
89 |
Correct |
527 ms |
28584 KB |
Output is correct |
90 |
Correct |
629 ms |
28940 KB |
Output is correct |
91 |
Correct |
376 ms |
28124 KB |
Output is correct |
92 |
Correct |
1075 ms |
29152 KB |
Output is correct |
93 |
Correct |
822 ms |
29100 KB |
Output is correct |
94 |
Correct |
262 ms |
26852 KB |
Output is correct |
95 |
Correct |
709 ms |
29024 KB |
Output is correct |
96 |
Correct |
660 ms |
29020 KB |
Output is correct |
97 |
Correct |
678 ms |
29032 KB |
Output is correct |
98 |
Correct |
659 ms |
29236 KB |
Output is correct |
99 |
Correct |
664 ms |
29124 KB |
Output is correct |
100 |
Correct |
654 ms |
29168 KB |
Output is correct |