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 ll long long
#define ar array
int n, q, sub[1<<20], super[1<<20];
string s;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q >> s;
for (int i=0; i<1<<n; ++i)
sub[i]=super[i]=s[i]-'0';
for (int j=0; j<n; ++j)
for (int i=0; i<1<<n; ++i) {
if (i&1<<j)
sub[i]+=sub[i^(1<<j)];
else
super[i]+=super[i^(1<<j)];
}
while(q--) {
string t;
cin >> t;
reverse(t.begin(), t.end());
if (count(t.begin(), t.end(), '?')<=6) {
int a=0, b=0;
for (int i=0; i<n; ++i) {
if (t[i]=='?')
b|=1<<i;
else if (t[i]=='1')
a|=1<<i;
}
int ans=s[a]-'0';
for (int c=b; c; c=b&(c-1))
ans+=s[a|c]-'0';
cout << ans << "\n";
} else if (count(t.begin(), t.end(), '1')<=6) {
int a=0, b=0;
for (int i=0; i<n; ++i) {
if (t[i]=='?')
a|=1<<i;
else if (t[i]=='1')
b|=1<<i;
}
int ans=sub[a|b];
for (int c=b; c; c=b&(c-1))
ans+=(__builtin_popcount(c)%2?-1:1)*sub[(a|b)^c];
cout << ans << "\n";
} else {
int a=0, b=0;
for (int i=0; i<n; ++i) {
if (t[i]=='1')
a|=1<<i;
else if (t[i]=='0')
b|=1<<i;
}
int ans=super[a];
for (int c=b; c; c=b&(c-1))
ans+=(__builtin_popcount(c)%2?-1:1)*super[a|c];
cout << ans << "\n";
}
}
return 0;
}
# | 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... |