This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include<bits/stdc++.h>
#define quick ios::sync_with_stdio(0);cin.tie(0);
#define rep(x,a,b) for(int x=a;x<=b;x++)
#define repd(x,a,b) for(int x=a;x>=b;x--)
#define lowbit(x) (x&-x)
#define sz(x) (int)(x.size())
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define mp make_pair
#define eb emplace_back
using namespace std;
typedef complex<int> P;
#define X real()
#define Y imag()
typedef pair<int,int> pii;
void debug(){
cout<<"\n";
}
template <class T,class ... U >
void debug(T a, U ... b){
cout<<a<<" ",debug(b...);
}
const int M=20;
const int N=(1<<M)+7;
const int INF=1e18;
int dp1[N];
int dp2[N];
int a[N];
int cnt0[N];
int cnt1[N];
int cnt2[N];
int ans=0;
string s;
void dfs(int x,int now,int n){
if(x>=n){
ans+=a[now];
return ;
}
dfs(x+1,now|(1<<cnt0[x]),n);
dfs(x+1,now,n);
}
void dfs2(int x,int flg,int now,int n){
if(x>=n) {
ans+=flg*dp1[now];
return ;
}
dfs2(x+1,flg,now|(1<<cnt1[x]),n);
dfs2(x+1,-flg,now,n);
}
void dfs3(int x,int flg,int now,int n){
if(x>=n){
ans+=flg*dp2[now];
return ;
}
dfs3(x+1,flg,now,n);
dfs3(x+1,-flg,now|(1<<cnt2[x]),n);
}
signed main(){
quick
int n,q;
cin>>n>>q;
string S;
cin>>S;
rep(i,0,(1<<n)-1){
dp1[i]=dp2[i]=a[i]=S[i]-'0';
}
rep(i,0,n-1){
rep(mask,0,(1<<n)-1){
if(mask&(1<<i)){
dp1[mask]+=dp1[mask^(1<<i)];
}
else dp2[mask]+=dp2[mask^(1<<i)];
}
}
while(q--){
cin>>s;
reverse(all(s));
int a,b,c;
int now1,now2,now3;
a=b=c=now1=now2=now3=0;
rep(i,0,n-1){
if(s[i]=='?') cnt0[a++]=i,now2|=(1<<i);
else if(s[i]=='1') cnt1[b++]=i,now1|=(1<<i),now3|=(1<<i);
else cnt2[c++]=i;
}
ans=0;
if(a<=min(b,c)){
dfs(0,now1,a);
}
else if(b<=min(a,c)){
dfs2(0,1,now2,b);
}
else{
dfs3(0,1,now3,c);
}
cout<<ans<<"\n";
}
return 0;
}
Compilation message (stderr)
snake_escaping.cpp:28:15: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
28 | const int INF=1e18;
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |