Submission #900555

#TimeUsernameProblemLanguageResultExecution timeMemory
900555oviyan_gandhiSnake Escaping (JOI18_snake_escaping)C++17
0 / 100
16 ms65536 KiB
#include <bits/stdc++.h> using namespace std; #define int long long // MAIN CODE #define MAXL 21 #define MAXN 1048576 int n, q; string s; int dp[MAXN][MAXL], sub0[MAXN], sub1[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][0] = s[one ? mask : flip(mask)]-'0'; if (mask & 1) dp[mask][0] += s[one ? (mask ^ 1) : flip(mask ^ 1)]-'0'; for (int i = 1; i <= n; i++){ dp[mask][i] = dp[mask][i-1]; if (mask & (1 << i)) dp[mask][i] += dp[mask ^ (1 << i)][i-1]; } (one ? sub1 : sub0)[mask] = dp[mask][n]; } } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> q >> s; sos(q), 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 : sub0); 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...