#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int k;
int q;
cin >> k >> q;
int N = 1 << k;
int n = N;
vector<long long> freq(N, 0);
string d;
cin >> d;
for (int i = 0; i < n; i++) {
int x = d[i] - '0';
freq[i] += x;
}
vector<long long> H = freq;
for (int bit = 0; bit < k; bit++) {
for (int mask = 0; mask < N; mask++) {
if (mask & (1 << bit)) {
H[mask] += H[mask ^ (1 << bit)];
}
}
}
while (q--) {
string a;
cin >> a;
int m1 = 0, m2 = 0;
int u = 0;
int y = (int) a.size() - 1;
for (int i = 0; i < a.size(); i++) {
if (a[i] == '1') {
m1 += (1 << (y - i));
m2 += (1 << (y - i));
}
if (a[i] == '?') {
u++;
m2 += (1 << (y - i));
}
}
if (u <= 10) {
int x1 = m2 - (m1 & m2);
int Y = x1;
int cnt = 0;
while (true) {
cnt += ((d[Y | m1]) - '0');
if (Y == 0) {
break;
}
Y = (Y - 1) & x1;
}
cout << cnt << '\n';
continue;
}
long long ans = 0;
int X = m1;
while (true) {
int sign = (__builtin_popcount(X) & 1) ? -1 : +1;
ans += sign * H[m2 & ~X];
if (X == 0) break;
X = (X - 1) & m1;
}
cout << 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... |