#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
#define pll pair<long long,long long>
#define eb emplace_back
int a[1<<20];
int f[1<<20];
int g[1<<20];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
int n,q;
cin>>n>>q;
string s;
cin>>s;
for1(i,0,(1<<n)-1){
a[i]=(s[i]-'0');
f[i]=g[i]=a[i];
}
for1(j,0,n-1){
for1(i,0,(1<<n)-1){
if (i>>j&1)f[i]=(f[i]+f[i-(1<<j)]);
}
for2(i,(1<<n)-1,0){
if (!(i>>j&1))g[i]=(g[i]+g[i+(1<<j)]);
}
}
for1(i,1,q){
string t;
cin>>t;
int bit0=0,bit1=0,bit=0,mask=0,mask1=0,mask0=0;
reverse(all(t));
for1(j,0,n-1){
if (t[j]=='0'){
bit0++;
mask0|=(1<<j);
}
if (t[j]=='1'){
bit1++;
mask1|=(1<<j);
}
if (t[j]=='?'){
bit++;
mask|=(1<<j);
}
}
int ans=0;
if (mask==0){
cout<<a[mask1]<<'\n';
continue;
}
//cout<<mask0<<" "<<mask1<<" "<<mask<<'\n';
//if (bit>20){
if (bit<=6){
for(int ss=mask;ss;ss=(ss-1)&mask){
ans+=(a[mask1|ss]);
}
ans+=a[mask1];
}
else if (bit1==min({bit,bit0,bit1})){
//else if (bit1>20){
ans=(ans+f[mask1|mask]);
int n1=__builtin_popcount(mask1);
for(int ss=mask1;ss;ss=(ss-1)&mask1){
if (ss==mask1)continue;
int num=__builtin_popcount(ss);
int ck=((num-n1)%2+2)%2;
if (ck==0)ans=(ans+f[ss|mask]);
else ans=(ans-f[ss|mask]);
//cout<<ss<<'\n';
}
if (mask1!=0){
if (n1%2==1)ans=(ans-f[mask]);
else ans=(ans+f[mask]);
}
}
else if (bit0==min({bit,bit0,bit1})){
//else if (bit0<=6){
ans=(ans+g[mask1]);
//cout<<g[0]<<'\n';
for(int ss=mask0;ss;ss=(ss-1)&mask0){
int num=__builtin_popcount(ss);
if (num%2==0)ans=(ans+g[ss|mask1]);
else ans=(ans-g[ss|mask1]);
}
}
cout<<ans<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
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 |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 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 |
184 ms |
15032 KB |
Output is correct |
12 |
Correct |
214 ms |
14744 KB |
Output is correct |
13 |
Correct |
225 ms |
13900 KB |
Output is correct |
14 |
Correct |
197 ms |
13984 KB |
Output is correct |
15 |
Correct |
226 ms |
15052 KB |
Output is correct |
16 |
Correct |
212 ms |
14200 KB |
Output is correct |
17 |
Correct |
214 ms |
14140 KB |
Output is correct |
18 |
Correct |
157 ms |
16048 KB |
Output is correct |
19 |
Correct |
162 ms |
13032 KB |
Output is correct |
20 |
Correct |
194 ms |
14752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 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 |
184 ms |
15032 KB |
Output is correct |
12 |
Correct |
214 ms |
14744 KB |
Output is correct |
13 |
Correct |
225 ms |
13900 KB |
Output is correct |
14 |
Correct |
197 ms |
13984 KB |
Output is correct |
15 |
Correct |
226 ms |
15052 KB |
Output is correct |
16 |
Correct |
212 ms |
14200 KB |
Output is correct |
17 |
Correct |
214 ms |
14140 KB |
Output is correct |
18 |
Correct |
157 ms |
16048 KB |
Output is correct |
19 |
Correct |
162 ms |
13032 KB |
Output is correct |
20 |
Correct |
194 ms |
14752 KB |
Output is correct |
21 |
Correct |
194 ms |
18088 KB |
Output is correct |
22 |
Correct |
224 ms |
18252 KB |
Output is correct |
23 |
Correct |
238 ms |
17300 KB |
Output is correct |
24 |
Correct |
231 ms |
17100 KB |
Output is correct |
25 |
Correct |
220 ms |
19132 KB |
Output is correct |
26 |
Correct |
240 ms |
17536 KB |
Output is correct |
27 |
Correct |
249 ms |
17528 KB |
Output is correct |
28 |
Correct |
156 ms |
20072 KB |
Output is correct |
29 |
Correct |
184 ms |
16068 KB |
Output is correct |
30 |
Correct |
227 ms |
18256 KB |
Output is correct |
31 |
Correct |
265 ms |
18068 KB |
Output is correct |
32 |
Correct |
238 ms |
18124 KB |
Output is correct |
33 |
Correct |
204 ms |
16976 KB |
Output is correct |
34 |
Correct |
231 ms |
17104 KB |
Output is correct |
35 |
Correct |
252 ms |
17560 KB |
Output is correct |
36 |
Correct |
159 ms |
16112 KB |
Output is correct |
37 |
Correct |
265 ms |
18080 KB |
Output is correct |
38 |
Correct |
208 ms |
16052 KB |
Output is correct |
39 |
Correct |
234 ms |
17204 KB |
Output is correct |
40 |
Correct |
264 ms |
17096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 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 |
62 ms |
16020 KB |
Output is correct |
12 |
Correct |
58 ms |
16064 KB |
Output is correct |
13 |
Correct |
60 ms |
15936 KB |
Output is correct |
14 |
Correct |
72 ms |
15920 KB |
Output is correct |
15 |
Correct |
56 ms |
16108 KB |
Output is correct |
16 |
Correct |
78 ms |
15936 KB |
Output is correct |
17 |
Correct |
71 ms |
16012 KB |
Output is correct |
18 |
Correct |
53 ms |
16192 KB |
Output is correct |
19 |
Correct |
58 ms |
15820 KB |
Output is correct |
20 |
Correct |
56 ms |
16060 KB |
Output is correct |
21 |
Correct |
77 ms |
16064 KB |
Output is correct |
22 |
Correct |
70 ms |
15936 KB |
Output is correct |
23 |
Correct |
58 ms |
15932 KB |
Output is correct |
24 |
Correct |
75 ms |
15940 KB |
Output is correct |
25 |
Correct |
73 ms |
15932 KB |
Output is correct |
26 |
Correct |
59 ms |
15860 KB |
Output is correct |
27 |
Correct |
60 ms |
16048 KB |
Output is correct |
28 |
Correct |
61 ms |
15808 KB |
Output is correct |
29 |
Correct |
74 ms |
15932 KB |
Output is correct |
30 |
Correct |
72 ms |
15996 KB |
Output is correct |
31 |
Correct |
64 ms |
15932 KB |
Output is correct |
32 |
Correct |
78 ms |
15932 KB |
Output is correct |
33 |
Correct |
84 ms |
15932 KB |
Output is correct |
34 |
Correct |
54 ms |
15872 KB |
Output is correct |
35 |
Correct |
72 ms |
16020 KB |
Output is correct |
36 |
Correct |
76 ms |
15936 KB |
Output is correct |
37 |
Correct |
84 ms |
15936 KB |
Output is correct |
38 |
Correct |
76 ms |
15980 KB |
Output is correct |
39 |
Correct |
81 ms |
15940 KB |
Output is correct |
40 |
Correct |
75 ms |
15980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 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 |
184 ms |
15032 KB |
Output is correct |
12 |
Correct |
214 ms |
14744 KB |
Output is correct |
13 |
Correct |
225 ms |
13900 KB |
Output is correct |
14 |
Correct |
197 ms |
13984 KB |
Output is correct |
15 |
Correct |
226 ms |
15052 KB |
Output is correct |
16 |
Correct |
212 ms |
14200 KB |
Output is correct |
17 |
Correct |
214 ms |
14140 KB |
Output is correct |
18 |
Correct |
157 ms |
16048 KB |
Output is correct |
19 |
Correct |
162 ms |
13032 KB |
Output is correct |
20 |
Correct |
194 ms |
14752 KB |
Output is correct |
21 |
Correct |
194 ms |
18088 KB |
Output is correct |
22 |
Correct |
224 ms |
18252 KB |
Output is correct |
23 |
Correct |
238 ms |
17300 KB |
Output is correct |
24 |
Correct |
231 ms |
17100 KB |
Output is correct |
25 |
Correct |
220 ms |
19132 KB |
Output is correct |
26 |
Correct |
240 ms |
17536 KB |
Output is correct |
27 |
Correct |
249 ms |
17528 KB |
Output is correct |
28 |
Correct |
156 ms |
20072 KB |
Output is correct |
29 |
Correct |
184 ms |
16068 KB |
Output is correct |
30 |
Correct |
227 ms |
18256 KB |
Output is correct |
31 |
Correct |
265 ms |
18068 KB |
Output is correct |
32 |
Correct |
238 ms |
18124 KB |
Output is correct |
33 |
Correct |
204 ms |
16976 KB |
Output is correct |
34 |
Correct |
231 ms |
17104 KB |
Output is correct |
35 |
Correct |
252 ms |
17560 KB |
Output is correct |
36 |
Correct |
159 ms |
16112 KB |
Output is correct |
37 |
Correct |
265 ms |
18080 KB |
Output is correct |
38 |
Correct |
208 ms |
16052 KB |
Output is correct |
39 |
Correct |
234 ms |
17204 KB |
Output is correct |
40 |
Correct |
264 ms |
17096 KB |
Output is correct |
41 |
Correct |
62 ms |
16020 KB |
Output is correct |
42 |
Correct |
58 ms |
16064 KB |
Output is correct |
43 |
Correct |
60 ms |
15936 KB |
Output is correct |
44 |
Correct |
72 ms |
15920 KB |
Output is correct |
45 |
Correct |
56 ms |
16108 KB |
Output is correct |
46 |
Correct |
78 ms |
15936 KB |
Output is correct |
47 |
Correct |
71 ms |
16012 KB |
Output is correct |
48 |
Correct |
53 ms |
16192 KB |
Output is correct |
49 |
Correct |
58 ms |
15820 KB |
Output is correct |
50 |
Correct |
56 ms |
16060 KB |
Output is correct |
51 |
Correct |
77 ms |
16064 KB |
Output is correct |
52 |
Correct |
70 ms |
15936 KB |
Output is correct |
53 |
Correct |
58 ms |
15932 KB |
Output is correct |
54 |
Correct |
75 ms |
15940 KB |
Output is correct |
55 |
Correct |
73 ms |
15932 KB |
Output is correct |
56 |
Correct |
59 ms |
15860 KB |
Output is correct |
57 |
Correct |
60 ms |
16048 KB |
Output is correct |
58 |
Correct |
61 ms |
15808 KB |
Output is correct |
59 |
Correct |
74 ms |
15932 KB |
Output is correct |
60 |
Correct |
72 ms |
15996 KB |
Output is correct |
61 |
Correct |
64 ms |
15932 KB |
Output is correct |
62 |
Correct |
78 ms |
15932 KB |
Output is correct |
63 |
Correct |
84 ms |
15932 KB |
Output is correct |
64 |
Correct |
54 ms |
15872 KB |
Output is correct |
65 |
Correct |
72 ms |
16020 KB |
Output is correct |
66 |
Correct |
76 ms |
15936 KB |
Output is correct |
67 |
Correct |
84 ms |
15936 KB |
Output is correct |
68 |
Correct |
76 ms |
15980 KB |
Output is correct |
69 |
Correct |
81 ms |
15940 KB |
Output is correct |
70 |
Correct |
75 ms |
15980 KB |
Output is correct |
71 |
Correct |
375 ms |
40264 KB |
Output is correct |
72 |
Correct |
384 ms |
40464 KB |
Output is correct |
73 |
Correct |
421 ms |
38904 KB |
Output is correct |
74 |
Correct |
598 ms |
39308 KB |
Output is correct |
75 |
Correct |
357 ms |
41272 KB |
Output is correct |
76 |
Correct |
724 ms |
39804 KB |
Output is correct |
77 |
Correct |
615 ms |
39464 KB |
Output is correct |
78 |
Correct |
249 ms |
43360 KB |
Output is correct |
79 |
Correct |
324 ms |
37276 KB |
Output is correct |
80 |
Correct |
340 ms |
40432 KB |
Output is correct |
81 |
Correct |
534 ms |
40332 KB |
Output is correct |
82 |
Correct |
697 ms |
39364 KB |
Output is correct |
83 |
Correct |
361 ms |
38488 KB |
Output is correct |
84 |
Correct |
491 ms |
39296 KB |
Output is correct |
85 |
Correct |
783 ms |
39568 KB |
Output is correct |
86 |
Correct |
258 ms |
37264 KB |
Output is correct |
87 |
Correct |
374 ms |
40284 KB |
Output is correct |
88 |
Correct |
326 ms |
37304 KB |
Output is correct |
89 |
Correct |
445 ms |
38980 KB |
Output is correct |
90 |
Correct |
446 ms |
39260 KB |
Output is correct |
91 |
Correct |
375 ms |
38508 KB |
Output is correct |
92 |
Correct |
759 ms |
39632 KB |
Output is correct |
93 |
Correct |
607 ms |
39548 KB |
Output is correct |
94 |
Correct |
286 ms |
37340 KB |
Output is correct |
95 |
Correct |
619 ms |
39372 KB |
Output is correct |
96 |
Correct |
589 ms |
39420 KB |
Output is correct |
97 |
Correct |
511 ms |
39432 KB |
Output is correct |
98 |
Correct |
567 ms |
39432 KB |
Output is correct |
99 |
Correct |
520 ms |
39416 KB |
Output is correct |
100 |
Correct |
638 ms |
39388 KB |
Output is correct |