#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
#pragma GCC target("avx2")
#ifdef LOCAL
#include "algo/debug.h"
#else
template <typename... Args>
void dummy(Args&&... args){}
#define ps dummy
#endif
#define f first
#define s second
template<class T> using V = vector<T>;
using vi = V<int>;
using vb = V<bool>;
using vs = V<string>;
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define len(x) (int)((x).size())
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define ai2 array<int,2>
#define ai3 array<int,3>
#define ai4 array<int,4>
#define ai5 array<int,5>
template<class T> int lwb(const V<T>& a, const T& b) { return lb(all(a),b)-begin(a); }
template<class T> int upb(const V<T>& a, const T& b) { return ub(all(a),b)-begin(a); }
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a=b, true : false; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a=b, true : false; }
#define pct __builtin_popcountll
#define ctz __builtin_ctzll
#define clz __builtin_clzll
constexpr int p2(int x) { return (int)1 << x; }
constexpr int bits(int x) { return x == 0 ? 0 : 63-clz(x); } // floor(log2(x))
template<class T>void UNIQUE(V<T>& v) { sort(all(v)); v.erase(unique(all(v)),end(v)); }
template<class T,class U>void erase(T& t, const U& u) { auto it = t.find(u); assert(it != end(t)); t.erase(it); }
template<class F> struct y_combinator_result {
F f;
template<class T> explicit y_combinator_result(T &&f): f(std::forward<T>(f)) {}
template<class ...Args> decltype(auto) operator()(Args &&...args) { return f(std::ref(*this), std::forward<Args>(args)...); }
};
template<class Fun> decltype(auto) yy(Fun &&fun) { return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun)); }
void solve() {
int L, q; cin >> L >> q;
vi cost(p2(L));
for (int i = 0; i < p2(L); i++) {
char c; cin >> c;
cost[i] = c - '0';
}
auto sosup = cost, sosub = cost;
for (int i = 0; i < L; i++) {
for (int msk = p2(L) - 1; msk >= 0; msk--) if (msk & p2(i)) {
sosub[msk] += sosub[msk ^ p2(i)];
}
}
for (int i = 0; i < L; i++) {
for (int msk = 0; msk < p2(L); msk++) if (!(msk & p2(i))) {
sosup[msk] += sosup[msk | p2(i)];
}
}
for (int i = 0; i < q; i++) {
string pat; cin >> pat;
reverse(all(pat));
int ca = 0, cb = 0, cc = 0, A = 0, B = 0, C = 0;
for (int i = 0; i < L; i++) {
if (pat[i] == '0') ca++, A |= p2(i);
if (pat[i] == '1') cb++, B |= p2(i);
if (pat[i] == '?') cc++, C |= p2(i);
}
int ans = 0;
if (min({ ca, cb, cc }) == cc) {
for (int c = C; ; c = (c - 1) & C) {
ans += cost[B | c];
if (!c) break;
}
} else if (min({ ca, cb, cc }) == ca) {
for (int a = A; ; a = (a - 1) & A) {
int coef = (pct(a ^ A) & 1) ? -1 : 1;
ans += coef * sosup[A ^ a | B];
if (!a) break;
}
} else if (min({ ca, cb, cc }) == cb) {
for (int b = B; ; b = (b - 1) & B) {
int coef = (pct(b ^ B) & 1) ? -1 : 1;
ans += coef * sosub[b | C];
if (!b) break;
}
}
cout << ans << '\n';
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
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... |