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 now=0;
int ans=0;
string s;
void dfs(int x,int n){
if(x>=n){
ans+=a[now];
return ;
}
if(s[x]=='0'||s[x]=='?'){
dfs(x+1,n);
}
if(s[x]=='1'||s[x]=='?'){
now+=(1<<x);
dfs(x+1,n);
now-=(1<<x);
}
}
int change=0;
void dfs2(int x,int n){
if(x>=n){
if(change) ans-=dp1[now];
else ans+=dp1[now];
return ;
}
if(s[x]=='1'){
now+=(1<<x);
dfs2(x+1,n);
now-=(1<<x);
change^=1;
dfs2(x+1,n);
change^=1;
}
else if(s[x]=='?'){
now+=(1<<x);
dfs2(x+1,n);
now-=(1<<x);
}
else{
dfs2(x+1,n);
}
}
void dfs3(int x,int n){
if(x>=n){
if(change) ans-=dp2[now];
else ans+=dp2[now];
return ;
}
if(s[x]=='0'){
change^=1;
now+=(1<<x);
dfs3(x+1,n);
change^=1;
now-=(1<<x);
dfs3(x+1,n);
}
else if(s[x]=='?'){
dfs3(x+1,n);
}
else{
now+=(1<<x);
dfs3(x+1,n);
now-=(1<<x);
}
}
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;
for(char _c:s){
if(_c=='?') a++;
else if(_c=='1') b++;
else c++;
}
ans=change=now=0;
if(a<=min(b,c)){
dfs(0,n);
}
else if(b<=min(a,c)){
dfs2(0,n);
}
else{
dfs3(0,n);
}
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;
| ^~~~
snake_escaping.cpp: In function 'int main()':
snake_escaping.cpp:118:11: warning: 'c' may be used uninitialized in this function [-Wmaybe-uninitialized]
118 | int a,b,c;
| ^
snake_escaping.cpp:118:9: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
118 | int a,b,c;
| ^
snake_escaping.cpp:118:7: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]
118 | int a,b,c;
| ^
# | 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... |