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<<20)];
int dp0[(1<<20)];
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;
for(int i=0;i<n;i++){
if(t[i]=='0')c0++;
else if(t[i]=='1')c1++;
else c++;
}
if(c<=min(c1,c0)){
cout<<stupid(t)<<"\n";
}
else if(c0<=c1){
int ans=0;
for(int mask=0;mask<(1<<c0);mask++){
int f1=0;
int cur=0;
int c=0;
for(int j=0;j<n;j++){
if(t[j]=='1'){
f1=f1*2+1;
}
else if(t[j]=='?'){
f1=f1*2;
}
else{
f1=f1*2;
if(!bit(mask,cur++)){
f1++;
c++;
}
}
}
// cout<<f1<<" "<<f<<" "<<dp1[f1]<<" "<<dp1[f]<<"\n";
int res=dp1[f1];
if(c%2==1)ans-=res;
else ans+=res;
}
cout<<ans<<"\n";
}
else{
int ans=0;
for(int mask=0;mask<(1<<c1);mask++){
int f1=0;
int cur=0;
int c=0;
for(int j=0;j<n;j++){
if(t[j]=='0'){
f1=f1*2;
}
else if(t[j]=='?'){
f1=f1*2+1;
}
else{
f1=f1*2;
if(!bit(mask,cur++)){
f1++;
c++;
}
}
}
// cout<<f<<" "<<f1<<"\n";
int res=dp0[f1];
if(c%2==1)ans-=res;
else ans+=res;
}
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... |