제출 #900613

#제출 시각아이디문제언어결과실행 시간메모리
900613oviyan_gandhiSnake Escaping (JOI18_snake_escaping)C++17
75 / 100
2045 ms40720 KiB
#include <bits/stdc++.h> using namespace std; #define MAXN 20 int n, q; string s; int prv[1 << MAXN], dp[1 << MAXN], sub1[1 << MAXN]; int flip(int x){ return ~x ^ ~((1 << n) - 1); } int curr = 0; int brute(string t, int pos = 0){ if (pos == 0) curr = 0; if (pos == n) return s[curr] - '0'; int ret = 0; if (t[pos] != '1') ret += brute(t, pos+1); if (t[pos] != '0'){ curr |= (1 << pos); ret += brute(t, pos+1); curr ^= (1 << pos); } return ret; } void sos(bool one){ memset(dp, 0, sizeof(dp)); for (int mask = 0; mask < (1 << n); mask++){ dp[mask] = s[one ? mask : flip(mask)]-'0'; if (mask & 1) dp[mask] += s[one ? (mask ^ 1) : flip(mask ^ 1)]-'0'; prv[mask] = dp[mask]; } for (int i = 1; i <= n; i++){ for (int mask = 0; mask < (1 << n); mask++){ dp[mask] = prv[mask]; if (mask & (1 << i)) dp[mask] += prv[mask ^ (1 << i)]; } for (int mask = 0; mask < (1 << n); mask++) prv[mask] = dp[mask]; } if (one) for (int mask = 0; mask < (1 << n); mask++) sub1[mask] = dp[mask]; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> q >> s; sos(1), sos(0); while (q--){ string t; cin >> t; reverse(t.begin(), t.end()); int cnt[]{0, 0, 0}; for (char c : t){ if (c == '?') cnt[0]++; else cnt[1+c-'0']++; } int least = min_element(cnt, cnt+3) - cnt - 1; if (least == -1){ cout << brute(t) << '\n'; continue; } int m = cnt[least+1], omask = 0; vector<int> pos; for (int i = 0; i < n; i++){ if (t[i] == '?' || t[i] == ('0' + least)) omask |= (1 << i); if (t[i] == ('0' + least)) pos.push_back(i); } int *sub = (least ? sub1 : dp); int ans = 0; for (int i = 0; i < (1 << m); i++){ int mask = omask; for (int j = 0; j < m; j++) if (i & (1 << j)) mask ^= (1 << pos[j]); if (__builtin_popcount(i) % 2) ans -= sub[mask]; else ans += sub[mask]; } cout << ans << '\n'; } 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...