1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| #include<bits/stdc++.h> using namespace std;
void read(int &x) { x=0;int f=1;char ch=getchar(); for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f; for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f; }
void print(int x) { if(x<0) putchar('-'),x=-x; if(!x) return ;print(x/10),putchar(x%10+48); } void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}
#define lf double #define ll long long
#define pii pair<int,int > #define vec vector<int >
#define pb push_back #define mp make_pair #define fr first #define sc second
#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)
const int maxn = 1.1e6+10; const int inf = 1e9; const lf eps = 1e-8; const int mod = 1e9+7;
char s[maxn],t[100]; int a[maxn],b[maxn],n,q,cnt[maxn];
void fwt(int *r,int op) { for(int i=1;i<1<<n;i<<=1) for(int j=0;j<1<<n;j+=i<<1) for(int k=0;k<i;k++) if(op) r[i+j+k]+=r[j+k];else r[j+k]+=r[i+j+k]; }
int main() { read(n),read(q);scanf("%s",s); for(int i=0;i<1<<n;i++) a[i]=b[i]=(s[i]-='0'),cnt[i]=cnt[i>>1]+(i&1); fwt(a,1),fwt(b,0); for(int i=1;i<=q;i++) { scanf("%s",t);int ca=0,c0=0,c1=0,sa=0,s0=0,s1=0; for(int j=0,x=n-1;j<n;j++,x--) if(t[j]=='0') c0++,s0|=1<<x; else if(t[j]=='1') c1++,s1|=1<<x; else ca++,sa|=1<<x; int mn=min(min(c0,c1),ca),ans=0; if(mn==c0) { for(int r=s0;~r;r=r?(r-1)&s0:-1) if(cnt[r]&1) ans-=b[r|s1]; else ans+=b[r|s1]; } else if(mn==c1) { for(int r=s1;~r;r=r?(r-1)&s1:-1) if(cnt[s1-r]&1) ans-=a[r|sa]; else ans+=a[r|sa]; } else { for(int r=sa;~r;r=r?(r-1)&sa:-1) ans+=s[r|s1]; }write(ans); } return 0; }
|