#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
#include <cstdlib>
#include <cstdint>
using namespace std;
#define ll long long
#define f first
//#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
using namespace std;
#define int long long
#define double long double
const int mod=998244353,mxn=3e5+5,lg=60,inf=1e18,minf=-1e18,Mxn=1e6+50000;
int dp1[Mxn+10],dp0[Mxn+10],cost[Mxn+10];
int32_t main(){
fastio
int n,q;cin>>n>>q;
string a;cin>>a;
for(int i=0;i<(1LL<<n);i++)cost[i]=dp1[i]=dp0[i]=(a[i]-'0');
for(int i=0;i<n;i++)for(int j=0;j<(1LL<<n);j++)if(j&(1LL<<i))dp1[j]+=dp1[j^(1LL<<i)];
for(int i=0;i<n;i++)for(int j=(1LL<<n)-1;j>=0;j--)if(!(j&(1LL<<i)))dp0[j]+=dp0[j+(1LL<<i)];
while(q--){
cin>>a;
reverse(all(a));
int x=0,y=0;
vector<int>o,z,c;
for(int i=0;i<n;i++){
if(a[i]=='1')x+=(1LL<<i),o.pb(i),y+=(1LL<<i);
else if(a[i]=='?')x+=(1LL<<i),c.pb(i);
else z.pb(i);
}
int m=0;
int ans=0;
if(c.size()<=6){
m=c.size();
for(int i=0;i<(1LL<<m);i++){
for(int j=0;j<m;j++)if((i&(1LL<<j))==0)x-=(1LL<<c[j]);
ans+=cost[x];
for(int j=0;j<m;j++)if((i&(1LL<<j))==0)x+=(1LL<<c[j]);
}
}
else if(o.size()<z.size()){
ans=dp1[x];
m=o.size();
for(int i=0;i<(1LL<<m)-1;i++){
int cnt=0;
for(int j=0;j<m;j++)if((i&(1LL<<j))==0)x-=(1LL<<o[j]),cnt++;
if(cnt%2)ans-=dp1[x];
else ans+=dp1[x];
for(int j=0;j<m;j++)if((i&(1LL<<j))==0)x+=(1LL<<o[j]);
}
/*
*/
}
else{
ans=dp0[y];
m=z.size();
for(int i=1;i<(1LL<<m);i++){
int cnt=0;
for(int j=0;j<m;j++)if((i&(1LL<<j)))y+=(1LL<<z[j]),cnt++;
if(cnt%2)ans-=dp0[y];
else ans+=dp0[y];
for(int j=0;j<m;j++)if((i&(1LL<<j)))y-=(1LL<<z[j]);
}
/*
*/
}
cout<<ans<<'\n';
//return 0;
//cout<<dp1[x]<<" "<<dp0[y]<<'\n';
}
}
/*
if cnt? <= sqrt(m) we can do (2^4)? bruteforce (maybe even 5)
else we can do sos dp and somehow remove the overcount
we can turn all ? into one and get sos of that bit
overcount when "1" bit that is originaly turned on switch to 0(for subset)
we can switch them off and subtract from the original answer so it cost 2^(cnt1){
we need to somehow do pie
starting from original answer-> if we turn off 1 bit
ans-(turn of x bit)-(turn of y bit)+(turn of x and y bit)
so if cnt%2==1 then we subtract
else we add back in
}
cnt1 can be at most 16,worst case is all 1 (2^16)
we can also do sos dp where 0 is now the "switchable" value
so if cnt0<cnt1 we take do from 0 dp else from 1
tc-> worst case q*(2^m)*m
pre com time is m*(2^m+1)
q*(x*(2^x)+(2^(m-x)/2))
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4696 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4576 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4696 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4576 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
667 ms |
14192 KB |
Output is correct |
12 |
Correct |
706 ms |
13744 KB |
Output is correct |
13 |
Correct |
474 ms |
12984 KB |
Output is correct |
14 |
Correct |
468 ms |
13140 KB |
Output is correct |
15 |
Correct |
1080 ms |
14340 KB |
Output is correct |
16 |
Correct |
616 ms |
13392 KB |
Output is correct |
17 |
Correct |
602 ms |
13340 KB |
Output is correct |
18 |
Correct |
257 ms |
15188 KB |
Output is correct |
19 |
Correct |
367 ms |
12372 KB |
Output is correct |
20 |
Correct |
731 ms |
13936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4696 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4576 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
667 ms |
14192 KB |
Output is correct |
12 |
Correct |
706 ms |
13744 KB |
Output is correct |
13 |
Correct |
474 ms |
12984 KB |
Output is correct |
14 |
Correct |
468 ms |
13140 KB |
Output is correct |
15 |
Correct |
1080 ms |
14340 KB |
Output is correct |
16 |
Correct |
616 ms |
13392 KB |
Output is correct |
17 |
Correct |
602 ms |
13340 KB |
Output is correct |
18 |
Correct |
257 ms |
15188 KB |
Output is correct |
19 |
Correct |
367 ms |
12372 KB |
Output is correct |
20 |
Correct |
731 ms |
13936 KB |
Output is correct |
21 |
Correct |
394 ms |
15956 KB |
Output is correct |
22 |
Correct |
710 ms |
16024 KB |
Output is correct |
23 |
Correct |
680 ms |
14960 KB |
Output is correct |
24 |
Correct |
630 ms |
14652 KB |
Output is correct |
25 |
Correct |
433 ms |
16908 KB |
Output is correct |
26 |
Correct |
814 ms |
15464 KB |
Output is correct |
27 |
Correct |
882 ms |
18752 KB |
Output is correct |
28 |
Correct |
276 ms |
21296 KB |
Output is correct |
29 |
Correct |
390 ms |
17372 KB |
Output is correct |
30 |
Correct |
740 ms |
19316 KB |
Output is correct |
31 |
Correct |
1273 ms |
19356 KB |
Output is correct |
32 |
Correct |
804 ms |
19284 KB |
Output is correct |
33 |
Correct |
528 ms |
18260 KB |
Output is correct |
34 |
Correct |
632 ms |
18516 KB |
Output is correct |
35 |
Correct |
787 ms |
19024 KB |
Output is correct |
36 |
Correct |
275 ms |
17212 KB |
Output is correct |
37 |
Correct |
1105 ms |
19424 KB |
Output is correct |
38 |
Correct |
412 ms |
17256 KB |
Output is correct |
39 |
Correct |
705 ms |
18824 KB |
Output is correct |
40 |
Correct |
622 ms |
18256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4696 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4576 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
52 ms |
28404 KB |
Output is correct |
12 |
Correct |
55 ms |
28540 KB |
Output is correct |
13 |
Correct |
77 ms |
28276 KB |
Output is correct |
14 |
Correct |
100 ms |
28400 KB |
Output is correct |
15 |
Correct |
56 ms |
28400 KB |
Output is correct |
16 |
Correct |
117 ms |
28400 KB |
Output is correct |
17 |
Correct |
111 ms |
28492 KB |
Output is correct |
18 |
Correct |
45 ms |
28660 KB |
Output is correct |
19 |
Correct |
59 ms |
28696 KB |
Output is correct |
20 |
Correct |
58 ms |
27636 KB |
Output is correct |
21 |
Correct |
68 ms |
27636 KB |
Output is correct |
22 |
Correct |
128 ms |
27636 KB |
Output is correct |
23 |
Correct |
65 ms |
27636 KB |
Output is correct |
24 |
Correct |
110 ms |
27696 KB |
Output is correct |
25 |
Correct |
123 ms |
27632 KB |
Output is correct |
26 |
Correct |
44 ms |
27388 KB |
Output is correct |
27 |
Correct |
53 ms |
27532 KB |
Output is correct |
28 |
Correct |
55 ms |
27572 KB |
Output is correct |
29 |
Correct |
85 ms |
27632 KB |
Output is correct |
30 |
Correct |
117 ms |
27648 KB |
Output is correct |
31 |
Correct |
63 ms |
27472 KB |
Output is correct |
32 |
Correct |
138 ms |
27640 KB |
Output is correct |
33 |
Correct |
118 ms |
27636 KB |
Output is correct |
34 |
Correct |
43 ms |
27420 KB |
Output is correct |
35 |
Correct |
93 ms |
27632 KB |
Output is correct |
36 |
Correct |
101 ms |
27688 KB |
Output is correct |
37 |
Correct |
94 ms |
27492 KB |
Output is correct |
38 |
Correct |
100 ms |
27724 KB |
Output is correct |
39 |
Correct |
89 ms |
27680 KB |
Output is correct |
40 |
Correct |
101 ms |
27668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4696 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4576 KB |
Output is correct |
7 |
Correct |
2 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
2 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4444 KB |
Output is correct |
11 |
Correct |
667 ms |
14192 KB |
Output is correct |
12 |
Correct |
706 ms |
13744 KB |
Output is correct |
13 |
Correct |
474 ms |
12984 KB |
Output is correct |
14 |
Correct |
468 ms |
13140 KB |
Output is correct |
15 |
Correct |
1080 ms |
14340 KB |
Output is correct |
16 |
Correct |
616 ms |
13392 KB |
Output is correct |
17 |
Correct |
602 ms |
13340 KB |
Output is correct |
18 |
Correct |
257 ms |
15188 KB |
Output is correct |
19 |
Correct |
367 ms |
12372 KB |
Output is correct |
20 |
Correct |
731 ms |
13936 KB |
Output is correct |
21 |
Correct |
394 ms |
15956 KB |
Output is correct |
22 |
Correct |
710 ms |
16024 KB |
Output is correct |
23 |
Correct |
680 ms |
14960 KB |
Output is correct |
24 |
Correct |
630 ms |
14652 KB |
Output is correct |
25 |
Correct |
433 ms |
16908 KB |
Output is correct |
26 |
Correct |
814 ms |
15464 KB |
Output is correct |
27 |
Correct |
882 ms |
18752 KB |
Output is correct |
28 |
Correct |
276 ms |
21296 KB |
Output is correct |
29 |
Correct |
390 ms |
17372 KB |
Output is correct |
30 |
Correct |
740 ms |
19316 KB |
Output is correct |
31 |
Correct |
1273 ms |
19356 KB |
Output is correct |
32 |
Correct |
804 ms |
19284 KB |
Output is correct |
33 |
Correct |
528 ms |
18260 KB |
Output is correct |
34 |
Correct |
632 ms |
18516 KB |
Output is correct |
35 |
Correct |
787 ms |
19024 KB |
Output is correct |
36 |
Correct |
275 ms |
17212 KB |
Output is correct |
37 |
Correct |
1105 ms |
19424 KB |
Output is correct |
38 |
Correct |
412 ms |
17256 KB |
Output is correct |
39 |
Correct |
705 ms |
18824 KB |
Output is correct |
40 |
Correct |
622 ms |
18256 KB |
Output is correct |
41 |
Correct |
52 ms |
28404 KB |
Output is correct |
42 |
Correct |
55 ms |
28540 KB |
Output is correct |
43 |
Correct |
77 ms |
28276 KB |
Output is correct |
44 |
Correct |
100 ms |
28400 KB |
Output is correct |
45 |
Correct |
56 ms |
28400 KB |
Output is correct |
46 |
Correct |
117 ms |
28400 KB |
Output is correct |
47 |
Correct |
111 ms |
28492 KB |
Output is correct |
48 |
Correct |
45 ms |
28660 KB |
Output is correct |
49 |
Correct |
59 ms |
28696 KB |
Output is correct |
50 |
Correct |
58 ms |
27636 KB |
Output is correct |
51 |
Correct |
68 ms |
27636 KB |
Output is correct |
52 |
Correct |
128 ms |
27636 KB |
Output is correct |
53 |
Correct |
65 ms |
27636 KB |
Output is correct |
54 |
Correct |
110 ms |
27696 KB |
Output is correct |
55 |
Correct |
123 ms |
27632 KB |
Output is correct |
56 |
Correct |
44 ms |
27388 KB |
Output is correct |
57 |
Correct |
53 ms |
27532 KB |
Output is correct |
58 |
Correct |
55 ms |
27572 KB |
Output is correct |
59 |
Correct |
85 ms |
27632 KB |
Output is correct |
60 |
Correct |
117 ms |
27648 KB |
Output is correct |
61 |
Correct |
63 ms |
27472 KB |
Output is correct |
62 |
Correct |
138 ms |
27640 KB |
Output is correct |
63 |
Correct |
118 ms |
27636 KB |
Output is correct |
64 |
Correct |
43 ms |
27420 KB |
Output is correct |
65 |
Correct |
93 ms |
27632 KB |
Output is correct |
66 |
Correct |
101 ms |
27688 KB |
Output is correct |
67 |
Correct |
94 ms |
27492 KB |
Output is correct |
68 |
Correct |
100 ms |
27724 KB |
Output is correct |
69 |
Correct |
89 ms |
27680 KB |
Output is correct |
70 |
Correct |
101 ms |
27668 KB |
Output is correct |
71 |
Correct |
543 ms |
42228 KB |
Output is correct |
72 |
Correct |
617 ms |
42232 KB |
Output is correct |
73 |
Correct |
1075 ms |
41124 KB |
Output is correct |
74 |
Correct |
1443 ms |
41376 KB |
Output is correct |
75 |
Correct |
622 ms |
43096 KB |
Output is correct |
76 |
Correct |
1966 ms |
41588 KB |
Output is correct |
77 |
Correct |
1698 ms |
41488 KB |
Output is correct |
78 |
Correct |
341 ms |
45100 KB |
Output is correct |
79 |
Correct |
553 ms |
39200 KB |
Output is correct |
80 |
Correct |
625 ms |
42284 KB |
Output is correct |
81 |
Correct |
941 ms |
42392 KB |
Output is correct |
82 |
Correct |
1434 ms |
41272 KB |
Output is correct |
83 |
Correct |
850 ms |
40212 KB |
Output is correct |
84 |
Correct |
1694 ms |
41416 KB |
Output is correct |
85 |
Correct |
1828 ms |
51784 KB |
Output is correct |
86 |
Correct |
366 ms |
49684 KB |
Output is correct |
87 |
Correct |
542 ms |
52616 KB |
Output is correct |
88 |
Correct |
592 ms |
49736 KB |
Output is correct |
89 |
Correct |
1157 ms |
51460 KB |
Output is correct |
90 |
Correct |
1519 ms |
51696 KB |
Output is correct |
91 |
Correct |
817 ms |
50800 KB |
Output is correct |
92 |
Execution timed out |
2056 ms |
49668 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |