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>
#define mp make_pair
#define fi first
#define se second
#define ii pair<int, int>
#define iii pair<ii, int>
#define ll long long
#define pb push_back
#define pli pair<ll, int>
#define all(v) (v).begin(), (v).end()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define cntbit(mask) __builtin_popcount(mask)
#define getbit(x, i) ((x >> i) & 1)
#define MASK(i) (1 << i)
#define pli pair<ll, int>
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return 1;} return 0;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return 1;} return 0;}
const int N = 1e5 + 5;
const int V = 1e6 + 5;
const int MOD = 1e9 + 7;
int n, q, cost[MASK(20) + 5], p[MASK(20) + 5];
string s;
void solve() {
cin >> n >> q;
cin >> s;
for(int i = 0 ; i < (1 << n) ; i++) p[i] = cost[i] = (s[i] - '0');
for(int i = 0 ; i < n ; i++)
for(int mask = 0 ; mask < (1 << n) ; mask++)
if (getbit(mask, i)) cost[mask] += cost[mask ^ MASK(i)];
for(int i = 0 ; i < q ; i++) {
string t; cin >> t;
reverse(all(t));
int mask1, mask2;
mask1 = mask2 = 0;
for(int i = 0 ; i < n ; i++)
if (t[i] == '1') mask1 |= MASK(i);
else if (t[i] == '?') mask2 |= MASK(i);
int ans = 0;
if (cntbit(mask2) <= 10) {
for(int submask = mask2 ; submask >= 0 ; submask = (submask - 1) & mask2) {
ans += p[mask1 + submask];
if (submask == 0) break;
}
} else {
int c = cntbit(mask1);
for(int submask = mask1 ; submask >= 0 ; submask = (submask - 1) & mask1) {
if (cntbit(submask) % 2 == c % 2) ans += cost[submask + mask2];
else ans -= cost[submask + mask2];
if (submask == 0) break;
}
}
cout << ans << '\n';
}
}
int main(void) {
//freopen("task.inp", "r", stdin);
//freopen("task.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1; while(t--) solve();
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... |