#include <bits/stdc++.h>
#define int long long
#define ll long long
#define pb push_back
#define fi first
#define se second
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<char,char> pcc;
typedef pair<pii,int> ipii;
typedef pair<pii,pii> ipiii;
const int MAXN = (1<<20)+10;
const int SQRT = 300;
const int MAXA = 50;
const int LOG = 20;
const int INF = 1e18+10;
const ld EPS = 1e-12;
const int MOD = 998244353;
int sum(int a, int b){ return (a+b)%MOD; }
void chsum(int &a, int b){ a = (a+b)%MOD; }
void chsub(int &a, int b){ a = (a+MOD-b)%MOD; }
int mul(int a, int b){ return (a*b)%MOD; }
void chmul(int &a, int b){ a = (a*b)%MOD; }
void chmn(int &a, int b){ a = min(a, b); }
void chmx(int &a, int b){ a = max(a, b); }
int dp[MAXN], dp2[MAXN], v[MAXN], n, q;
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>q;
for(int i=0; i<(1<<n); i++){
char x; cin>>x;
v[i] = (int)(x-'0');
dp[i] = v[i];
}
for(int j=0; j<LOG; j++){
for(int i=0; i<(1<<LOG); i++)
if((i>>j) & 1)
dp2[i ^ (1<<j)] += dp2[i],
dp[i] += dp[i ^ (1<<j)];
}
while(q--){
string p; cin>>p;
reverse(p.begin(), p.end());
vector <int> z, o, qu;
for(int i=0; i<p.size(); i++){
char in = p[i];
if(in=='0') z.pb(i);
else if(in=='1') o.pb(i);
else qu.pb(i);
}
if(qu.size() <= 6){
int mask = 0;
for(int i=0; i<p.size(); i++){
char in = p[i];
if(in=='1') mask += (1<<i);
}
int siz = qu.size(), ans = 0;
for(int x=0; x<(1<<siz); x++){
int nw = mask;
for(int i=0; i<siz; i++)
if((x>>i) & 1) nw |= (1<<(qu[i]));
ans += v[nw];
}
cout << ans << '\n';
}else if(o.size() <= 6){
// super
int mask = 0;
for(int i=0; i<p.size(); i++){
char in = p[i];
if(in=='1') mask += (1<<i);
}
int siz = o.size(), ans = 0;
for(int x=0; x<(1<<siz); x++){
int nw = mask;
for(int i=0; i<siz; i++)
if((x>>i) & 1) nw |= (1<<(o[i]));
if(x%2 == siz%2) ans += dp2[nw];
else ans -= dp2[nw];
}
cout << ans << '\n';
} else {
// sub
int mask = 0;
for(int i=0; i<p.size(); i++){
char in = p[i];
if(in=='0') mask += (1<<i);
}
int siz = z.size(), ans = 0;
for(int x=0; x<(1<<siz); x++){
int nw = mask;
for(int i=0; i<siz; i++)
if( !((x>>i) & 1)) nw |= (1<<(z[i]));
if(x%2 == siz%2) ans += dp[nw];
else ans -= dp[nw];
}
cout << ans << '\n';
}
}
}
# | 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... |