#include <bits/stdc++.h>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#define sui cout.tie(NULL); cin.tie(NULL); ios_base::sync_with_stdio(false)
const int MAX_N = 1e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;
const int LOG = 10;
int sum[(1 << LOG)];
void solve() {
int n, q;
cin >> n >> q;
string s;
cin >> s;
int dp[(1 << n)];
for (int i = 0; i < (1 << n); i++) dp[i] = s[i] - '0';
for (int i = 0; i < n; i++) for (int j = (1 << n) - 1; j >= 0; j--) if (j & (1 << i)) dp[j] += dp[j ^ (1 << i)];
while (q--)
{
string t;
cin >> t;
reverse(all(t));
vector<int> al;
vector<int> al1;
int sum1 = 0;
for (int i = 0; i < n; i++)
{
if (t[i] == '?') al.push_back(i);
else if (t[i] == '1') al1.push_back(i), sum1 += (1 << i);
}
if (al.size() <= LOG)
{
int ans = s[sum1] - '0';
sum[0] = 0;
for (int j = 1; j < (1 << al.size()); j++)
{
int ind = 31 - __builtin_clz(j);
sum[j] = sum[j - (1 << ind)] + (1 << al[ind]);
ans += s[sum[j] + sum1] - '0';
}
cout << ans << "\n";
} else
{
int num1 = 0;
for (int x : al) num1 += (1 << x);
int ans = 0;
int num2 = sum1;
while (num2)
{
ans += (__builtin_popcount(num2 ^ sum1) % 2 ? -1 : 1) * dp[num1 + num2];
num2 = (num2 - 1) & sum1;
}
ans += (__builtin_popcount(num2 ^ sum1) % 2 ? -1 : 1) * dp[num1 + num2];
cout << ans << "\n";
}
}
}
int main() {
sui;
int tc = 1;
//cin >> tc;
for (int t = 1; t <= tc; t++) {
solve();
}
}
| # | 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... |