# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
965392 | four_specks | Snake Escaping (JOI18_snake_escaping) | C++17 | 0 ms | 0 KiB |
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;
ranges::reverse(t);
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;
{
vector<int> cnt;
ranges::transform(v, back_inserter(cnt), popcount<uint32_t>);
int p = ranges::distance(cnt.begin(), ranges::min_element(cnt));
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 (popcount(x) % 2 == 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;
}