This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma optimize("Ofast")
#pragma target("avx2")
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define mem(a,i) memset(a,i,sizeof(a))
#define sz(s) (int)s.size()
#define y1 yy
#define ppb pop_back
#define lb lower_bound
#define ub upper_bound
#define gcd(a,b) __gcd(a,b)
#define in insert
// #define int ll
const int MAX=1e5+15;
const int B=2e5;
const int N=104;
const int block=450;
const int maxB=MAX/B+10;
const ll inf=1e18;
const int mod=1e9+7;
const int mod1=1e9+9;
const ld eps=1e-9;
int dx[8]={1,0,-1,0,1,-1,-1,1};
int dy[8]={0,1,0,-1,1,-1,1,-1};
int binpow(int a,int n){
if(!n)return 1;
if(n%2==1)return a*binpow(a,n-1);
int k=binpow(a,n/2);
return k*k;
}
int n,q;
string s;
int dp1[(1<<21)];
int dp0[(1<<21)];
int bit(int i,int j){
return (i>>j)&1;
}
int stupid(string t){
int ans=0;
int c=0;
for(int j=0;j<n;j++)if(t[j]=='?')c++;
for(int i=0;i<(1<<c);i++){
int cur=0;
int f=0;
for(int j=0;j<n;j++){
if(t[j]!='?'){
f=f*2+(t[j]-'0');
}
else{
f=f*2+bit(i,cur++);
}
}
ans+=s[f]-'0';
}
return ans;
}
void solve(){
cin>>n>>q;
cin>>s;
for(int mask=0;mask<(1<<n);mask++)dp1[mask]=s[mask]-'0';
for(int i=0;i<=n;i++){
for(int mask=(1<<n)-1;mask>=0;mask--){
if(!((mask>>i)&1)){
dp1[mask]+=dp1[mask+(1<<i)];
}
}
}
for(int mask=0;mask<(1<<n);mask++)dp0[mask]=s[mask]-'0';
for(int i=0;i<n;i++){
for(int mask=0;mask<(1<<n);mask++){
if((mask>>i)&1){
dp0[mask]+=dp0[mask-(1<<i)];
}
}
}
while(q--){
string t;
cin>>t;
int c0=0,c1=0,c=0;
int m0=0,m1=0,m=0;
int msk=0;
for(int i=0;i<n;i++){
m0*=2;
m1*=2;
m*=2;
msk*=2;
if(t[i]=='0')c0++,m0++;
else if(t[i]=='1')c1++,m1++;
else c++,m++;
}
if(c<=min(c1,c0)){
cout<<stupid(t)<<"\n";
}
else if(c0<=c1){
int ans=0;
for(int mask=m0;;mask=((mask-1)&m0)){
int c=__builtin_popcount(mask);
if(c%2==1)ans-=dp1[mask|m1];
else ans+=dp1[mask|m1];
if(mask==0)break;
}
cout<<ans<<"\n";
}
else{
int ans=0;
for(int mask=m1;;mask=((mask-1)&m1)){
int c=__builtin_popcount(mask);
if((c1-c)%2==1)ans-=dp0[mask|m];
else ans+=dp0[mask|m];
if(mask==0)break;
}
cout<<ans<<"\n";
}
}
}
//1010
//0011
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// prec();
int t=1;
// cin>>t;
while(t--)solve();
}
Compilation message (stderr)
snake_escaping.cpp:3: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
3 | #pragma optimize("Ofast")
|
snake_escaping.cpp:4: warning: ignoring '#pragma target ' [-Wunknown-pragmas]
4 | #pragma target("avx2")
|
# | 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... |