#include <iostream>
#include <vector>
#include <queue>
#include <math.h>
#include <algorithm>
#include <map>
#include <set>
#include <cstring>
#include <string>
//#define int long long
//const int inf = 1e17;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 5;
const int maxn2 = 1e5 + 5;
const int maxs = 1e3 + 5;
const int maxl = 2e3 + 5;
using namespace std;
vector<int> arr;
vector<vector<int>> sub, sup;
void problemA() {
int n, q; string s;
cin >> n >> q >> s;
arr.resize(1 << n);
sub.resize(n + 1, vector<int>(1 << n));
sup.resize(n + 1, vector<int>(1 << n));
for (int i = 0; i < (1 << n); ++i)
arr[i] = sub[n][i] = sup[n][i] = (s[i] - '0');
for (int m = 0; m < (1 << n); ++m) {
sub[0][m] = arr[m];
for (int x = 1; x <= n; ++x) {
sub[x][m] = sub[x - 1][m];
if (m & (1 << (x - 1))) sub[x][m] += sub[x - 1][m - (1 << (x - 1))];
}
}
for (int m = (1 << n) - 1; m >= 0; --m) {
sup[0][m] = arr[m];
for (int x = 1; x <= n; ++x) {
sup[x][m] = sup[x - 1][m];
if (!(m & (1 << (x - 1)))) sup[x][m] += sup[x - 1][m + (1 << (x - 1))];
}
}
while (q--) {
cin >> s;
vector<int> a, b, c;
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 << endl;
}
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[n][o | v] * ((sz % 2) ? -1 : 1));
}
cout << ans << endl;
}
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[n][o | v] * (((b.size() - sz) % 2) ? -1 : 1));
}
cout << ans << endl;
}
}
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
while (t--) {
problemA();
}
}
# | 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... |