Submission #965920

#TimeUsernameProblemLanguageResultExecution timeMemory
965920four_specksSnake Escaping (JOI18_snake_escaping)C++17
100 / 100
604 ms43428 KiB
#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<int> b(n); for (uint32_t x = 0; x < n; x++) { b[x] = a[x] - '0'; } vector dp_sub = b, dp_super = b; for (int s = 0; s < h; s++) { for (uint32_t x = n - 1; x + 1 > 0; x--) { if (x >> s & 1) { dp_sub[x] += dp_sub[x ^ (1u << s)]; } } for (uint32_t x = 0; x < n; x++) { if (!(x >> s & 1)) { dp_super[x] += dp_super[x | (1u << 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 += b[v[1] | x]; } else { int term = p == 1 ? dp_sub[(v[1] ^ x) | v[2]] : dp_super[v[1] | x]; 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...