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 rep(i,a,b) for(int i=(a); i<(b); ++i)
#define all(x) x.begin(),x.end()
#define sz(x) int(x.size())
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
const int L = 20;
int SOS[3][1<<L];
int main(){
cin.tie(NULL),cin.sync_with_stdio(false);
int l,q; cin >> l >> q;
int msk_all = (1<<l)-1;
string s; cin >> s;
rep(i,0,1<<l){
// ones
SOS[1][i] = SOS[2][i] = int(s[i]-'0');
SOS[0][msk_all^i] = int(s[i]-'0');
}
rep(X,0,2) rep(i,0,l) rep(mask,0,1<<l) if (mask&(1<<i)) SOS[X][mask] += SOS[X][mask^(1<<i)];
while (q--){
string s; cin >> s;
reverse(all(s));
if (count(all(s),'?') < 7){
int qmask = 0, omask = 0;
rep(i,0,l) {
if (s[i] == '?') qmask |= 1<<i;
else if (s[i] == '1') omask |= 1<<i;
}
int ans = 0, msk = qmask;
while (msk){
ans += SOS[2][msk|omask];
msk = (msk-1)&qmask;
} ans += SOS[2][omask];
cout << ans << '\n';
}else if (count(all(s),'1') < 7){
int ans = 0, qmask = 0, omask = 0;
rep(i,0,l) {
if (s[i] == '?') qmask |= 1<<i;
else if (s[i] == '1') omask |= 1<<i;
}
int msk = omask;
while (msk){
ans += (1-2*(__builtin_popcount(msk)&1))*SOS[1][msk|qmask];
msk = (msk-1)&omask;
} ans += SOS[1][qmask];
cout << abs(ans) << '\n';
}else{
int ans = 0, qmask = 0, omask = 0;
rep(i,0,l) {
if (s[i] == '?') qmask |= 1<<i;
else if (s[i] == '0') omask |= 1<<i;
}
int msk = omask;
while(msk){
ans += (1-2*(__builtin_popcount(msk)&1))*SOS[0][msk|qmask];
msk = (msk-1)&omask;
} ans += SOS[0][qmask];
cout << abs(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... |