제출 #1129873

#제출 시각아이디문제언어결과실행 시간메모리
1129873am_aadvikSnake Escaping (JOI18_snake_escaping)C++20
75 / 100
2096 ms27408 KiB
#include <iostream> #include <vector> #include <queue> #include <math.h> #include <algorithm> #include <map> #include <set> #include <cstring> #include <string> const int mod = 1e9 + 7; const int maxn = 2e5 + 5; const int maxn2 = 1e5 + 5; const int maxs = 1e3 + 5; const int maxl = 2e3 + 5; using namespace std; void problemA() { int n, q; string s; cin >> n >> q >> s; vector<int> arr(1 << n), sub(1 << n), sup(1 << n); for (int i = 0; i < (1 << n); ++i) arr[i] = (s[i] - '0'); vector<int> cur(1 << n), prv = arr; for (int x = 1; x <= n; ++x) { for (int m = 0; m < (1 << n); ++m) { cur[m] = prv[m]; if (m & (1 << (x - 1))) cur[m] += prv[m - (1 << (x - 1))]; } prv = cur; } sub = prv, prv = arr; for (int x = 1; x <= n; ++x) { for (int m = (1 << n) - 1; m >= 0; --m) { cur[m] = prv[m]; if (!(m & (1 << (x - 1)))) cur[m] += prv[m + (1 << (x - 1))]; } prv = cur; } sup = prv; while (q--) { cin >> s; vector<int> a, b, c; for (int i = 0; i < n / 2; ++i) swap(s[i], s[n - i - 1]); for (int i = 0; i < n; ++i) if (s[i] == '?') c.push_back(i); else if (s[i] == '1') b.push_back(i); else a.push_back(i); if (c.size() <= 6) { int ans = 0, o = 0; for (auto x : b) o += (1 << x); for (int i = 0; i < (1 << c.size()); ++i) { int v = 0; for (int j = 0; j < c.size(); ++j) if (i & (1 << j)) v += (1 << c[j]); ans += arr[o + v]; } cout << ans << endl; } else if (a.size() <= 6) { int ans = 0, o = 0; for (auto x : b) o += (1 << x); for (int i = 0; i < (1 << a.size()); ++i) { int v = 0, sz = 0; for (int j = 0; j < a.size(); ++j) if (i & (1 << j)) v += (1 << a[j]), sz++; ans += (sup[o | v] * ((sz % 2) ? -1 : 1)); } cout << ans << endl; } else { int ans = 0, o = 0; for (auto x : c) o += (1 << x); for (int i = 0; i < (1 << b.size()); ++i) { int v = 0, sz = 0; for (int j = 0; j < b.size(); ++j) if (i & (1 << j)) v += (1 << b[j]), sz++; ans += (sub[o | v] * (((b.size() - sz) % 2) ? -1 : 1)); } cout << ans << endl; } } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); int t = 1; while (t--) { problemA(); } }
#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...