This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
namespace {}
void solve() {
int h, q;
cin >> h >> q;
uint32_t n = 1 << h;
string a;
cin >> a;
vector dp_sub(n, vector<int>(h + 1)), dp_super(n, vector<int>(h + 1));
for (uint32_t x = 0; x < n; x++) {
dp_sub[x][0] = a[x] - '0';
for (int s = 0; s < h; s++) {
dp_sub[x][s + 1] = dp_sub[x][s];
if (x >> s & 1) {
dp_sub[x][s + 1] += dp_sub[x ^ (1u << s)][s];
}
}
}
for (uint32_t x = n - 1; x + 1 > 0; x--) {
dp_super[x][0] = a[x] - '0';
for (int s = 0; s < h; s++) {
dp_super[x][s + 1] = dp_super[x][s];
if (!(x >> s & 1)) {
dp_super[x][s + 1] += dp_super[x | (1u << s)][s];
}
}
}
for (int i = 0; i < q; i++) {
string t;
cin >> t;
reverse(t.begin(), t.end());
array<uint32_t, 3> v;
v.fill(0);
for (int s = 0; s < h; s++) {
if (t[s] == '0') {
v[0] |= 1u << s;
} else if (t[s] == '1') {
v[1] |= 1u << s;
} else {
v[2] |= 1u << s;
}
}
int ans = 0;
{
auto it = min_element(v.begin(), v.end(), [&](uint32_t x, uint32_t y) -> bool {
return __builtin_popcount(x) < __builtin_popcount(y);
});
int p = distance(v.begin(), it);
for (uint32_t x = v[p];; x = (x - 1) & v[p]) {
if (p == 2) {
ans += a[v[1] | x] - '0';
} else {
int term = p == 1 ? dp_sub[(v[1] ^ x) | v[2]][h] : dp_super[v[1] | x][h];
if (__builtin_parity(x) == 0) {
ans += term;
} else {
ans -= term;
}
}
if (x == 0) {
break;
}
}
}
cout << ans << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
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... |