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>
using namespace std;
#define int long long
int DP1[1048576];
int DP2[1048576];
int arr[1048576];
int applymaskques(const string &s,int mask){
int ans = 0;
for(int bit=0;bit<s.size();bit++){
if(s[bit]=='?'){
if(mask&1)ans|=(1<<bit);
mask>>=1;
} else {
if(s[bit]=='1')ans|=(1<<bit);
}
}
return ans;
}
int applymask(const string &s,int mask,char to){
int ans = 0;
for(int bit=0;bit<s.size();bit++){
if(s[bit]=='?'){
if(to=='1')ans|=(1<<bit);
} else if(s[bit]==to){
if(mask&1)ans|=(1<<bit);
mask>>=1;
} else {
if(to=='0')ans|=(1<<bit);
}
}
return ans;
}
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++;
for(char&j:s)if(j=='0')c0++;
for(char&j:s)if(j=='?')cq++;
if(c0<=6){
int ans = 0;
for(int mask=0;mask<(1<<c0);mask++){
if(__builtin_parity(mask)){
ans-=DP2[applymask(s,mask,'0')];
} else {
ans+=DP2[applymask(s,mask,'0')];
}
}
cout<<ans<<'\n';
continue;
}
if(c1<=6){
int ans = 0;
for(int mask=0;mask<(1<<c1);mask++){
if(__builtin_parity(mask)==(c1&1)){
ans+=DP1[applymask(s,mask,'1')];
} else {
ans-=DP1[applymask(s,mask,'1')];
}
}
cout<<ans<<'\n';
continue;
}
if(cq<=6){
int ans = 0;
for(int mask=0;mask<(1<<cq);mask++){
ans+=arr[applymaskques(s,mask)];
}
cout<<ans<<'\n';
continue;
}
}
}
Compilation message (stderr)
snake_escaping.cpp: In function 'long long int applymaskques(const string&, long long int)':
snake_escaping.cpp:12: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]
12 | for(int bit=0;bit<s.size();bit++){
| ~~~^~~~~~~~~
snake_escaping.cpp: In function 'long long int applymask(const string&, long long int, char)':
snake_escaping.cpp:25: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]
25 | 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... |