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("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define int long long
int DP1[1048576];
int DP2[1048576];
int arr[1048576];
int idx[6];
inline int preprocess(const string &s,char c){
int ans = 0;
int cnt = 0;
for(int bit=0;bit<s.size();bit++){
if(s[bit]==c){
idx[cnt++]=bit;
} else if(s[bit]=='?'){
if(c=='1')ans|=(1<<bit);
} else {
if(s[bit]=='1')ans|=(1<<bit);
}
}
return ans;
}
inline int applymask(int qu,int mask,int cnt){
for(int bit=0;bit<cnt;bit++){
if(mask&(1<<bit))qu|=(1<<idx[bit]);
}
return qu;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int l,q;
cin >> l >> q;
for(int mask=0;mask<(1<<l);mask++){
char a;cin>>a;a-='0';
DP1[mask]=DP2[mask]=arr[mask]=a;
}
for(int bit=0;bit<20;bit++){
for(int mask=0;mask<(1<<20);mask++){
if(mask&(1<<bit)){
DP2[mask^(1<<bit)]+=DP2[mask];
} else {
DP1[mask|(1<<bit)]+=DP1[mask];
}
}
}
for(int i=1;i<=q;i++){
string s;cin>>s;
reverse(s.begin(),s.end());
int c1=0,c0=0,cq=0;
for(char&j:s) {
if(j=='1')c1++;
else if(j=='0')c0++;
else if(j=='?')cq++;
}
if(cq<=6){
int qu = preprocess(s,'?');
int ans = 0;
for(int mask=0;mask<(1<<cq);mask++){
ans+=arr[applymask(qu,mask,cq)];
}
cout<<ans<<'\n';
continue;
}
if(c1<=6){
int qu = preprocess(s,'1');
int ans = 0;
for(int mask=0;mask<(1<<c1);mask++){
if(__builtin_parity(mask)==(c1&1)){
ans+=DP1[applymask(qu,mask,c1)];
} else {
ans-=DP1[applymask(qu,mask,c1)];
}
}
cout<<ans<<'\n';
continue;
}
if(c0<=6){
int qu = preprocess(s,'0');
int ans = 0;
for(int mask=0;mask<(1<<c0);mask++){
if(__builtin_parity(mask)){
ans-=DP2[applymask(qu,mask,c0)];
} else {
ans+=DP2[applymask(qu,mask,c0)];
}
}
cout<<ans<<'\n';
continue;
}
}
}
Compilation message (stderr)
snake_escaping.cpp: In function 'long long int preprocess(const string&, char)':
snake_escaping.cpp:16:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for(int bit=0;bit<s.size();bit++){
| ~~~^~~~~~~~~
# | 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... |