#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("Os")
#pragma unroll
#include <vector>
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
int t = 1;
while (t--) {
int n, q; string s;
cin >> n >> q >> s;
vector<int> arr(1 << n), sub(1 << n), sup(1 << n);
for (int i = 0; i < (1 << n); ++i)
arr[i] = (s[i] - '0');
vector<int> cur(1 << n), prv = arr;
for (int x = 1; x <= n; ++x) {
for (int m = 0; m < (1 << n); ++m) {
cur[m] = prv[m];
if (m & (1 << (x - 1))) cur[m] += prv[m - (1 << (x - 1))];
}
prv = cur;
}
sub = prv, prv = arr;
for (int x = 1; x <= n; ++x) {
for (int m = (1 << n) - 1; m >= 0; --m) {
cur[m] = prv[m];
if (!(m & (1 << (x - 1)))) cur[m] += prv[m + (1 << (x - 1))];
}
prv = cur;
}
sup = prv;
vector<int> a, b, c;
while (q--) {
cin >> s;
for (int i = 0; i < n / 2; ++i) swap(s[i], s[n - i - 1]);
for (int i = 0; i < n; ++i)
if (s[i] == '?') c.push_back(i);
else if (s[i] == '1') b.push_back(i);
else a.push_back(i);
if (c.size() <= 6) {
int ans = 0, o = 0;
for (auto x : b) o += (1 << x);
for (int i = 0; i < (1 << c.size()); ++i) {
int v = 0;
for (int j = 0; j < c.size(); ++j)
if (i & (1 << j))
v += (1 << c[j]);
ans += arr[o + v];
}
cout << ans << "\n";
}
else if (a.size() <= 6) {
int ans = 0, o = 0;
for (auto x : b) o += (1 << x);
for (int i = 0; i < (1 << a.size()); ++i) {
int v = 0, sz = 0;
for (int j = 0; j < a.size(); ++j)
if (i & (1 << j))
v += (1 << a[j]), sz++;
ans += (sup[o | v] * ((sz & 1) ? -1 : 1));
}
cout << ans << "\n";
}
else {
int ans = 0, o = 0;
for (auto x : c) o += (1 << x);
for (int i = 0; i < (1 << b.size()); ++i) {
int v = 0, sz = 0;
for (int j = 0; j < b.size(); ++j)
if (i & (1 << j))
v += (1 << b[j]), sz++;
ans += (sub[o | v] * (((b.size() - sz) & 1) ? -1 : 1));
}
cout << ans << "\n";
}
a.clear(), b.clear(), c.clear();
}
}
}
# | 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... |