Submission #681606

#TimeUsernameProblemLanguageResultExecution timeMemory
681606peijarSnake Escaping (JOI18_snake_escaping)C++17
100 / 100
1144 ms47348 KiB
#include <bits/stdc++.h> #define int long long using namespace std; namespace std { template <typename T> ostream &operator<<(ostream &out, const vector<T> &vec) { out << "["; for (int i = 0; i < (int)vec.size(); ++i) { out << vec[i]; if (i + 1 < (int)vec.size()) out << ", "; } return out << "]"; } } // namespace std void dbg_out() { cout << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); } #ifdef DEBUG #define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif signed main(void) { ios_base::sync_with_stdio(false); cin.tie(0); int L, Q; cin >> L >> Q; string T; cin >> T; vector<int> dpSub(1 << L), dpOver(1 << L); int sommeTox = 0; for (char c : T) sommeTox += c - '0'; for (int i = 0; i < (1 << L); ++i) dpSub[i] = dpOver[i] = T[i] - '0'; for (int bit = 0; bit < L; ++bit) for (int msk = 0; msk < (1 << L); ++msk) { if (msk & (1 << bit)) dpSub[msk] += dpSub[msk ^ (1 << bit)]; else dpOver[msk] += dpOver[msk ^ (1 << bit)]; } auto brute0 = [&](int msk0, int msk1, int mskFree) { int sol = 0; for (int m = msk0;; m = (m - 1) & msk0) { int sign = __builtin_popcount(m) % 2 ? -1 : 1; sol += sign * dpOver[m | msk1]; if (!m) break; } return sol; }; auto brute1 = [&](int msk0, int msk1, int mskFree) { int sol = 0; for (int m = msk1;; m = (m - 1) & msk1) { int sign = __builtin_popcount(m ^ msk1) % 2 ? -1 : 1; sol += sign * dpSub[m | mskFree]; if (!m) break; } return sol; }; auto bruteFree = [&](int msk0, int msk1, int mskFree) { int sol = 0; for (int m = mskFree;; m = (m - 1) & mskFree) { sol += T[msk1 | m] - '0'; if (!m) break; } return sol; }; for (int iRequete = 0; iRequete < Q; ++iRequete) { string s; cin >> s; reverse(s.begin(), s.end()); int msk0 = 0, msk1 = 0, mskFree = 0; for (int i = 0; i < L; ++i) { if (s[i] == '0') msk0 |= 1 << i; else if (s[i] == '1') msk1 |= 1 << i; else mskFree |= 1 << i; } if (__builtin_popcount(msk0) * 3 <= L) { cout << brute0(msk0, msk1, mskFree) << '\n'; } else if (__builtin_popcount(msk1) * 3 <= L) { cout << brute1(msk0, msk1, mskFree) << '\n'; } else { cout << bruteFree(msk0, msk1, mskFree) << '\n'; } } }
#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...