Submission #827863

#TimeUsernameProblemLanguageResultExecution timeMemory
827863ikaurovAncient Machine 2 (JOI23_ancient2)C++17
0 / 100
17 ms336 KiB
#include "ancient2.h" #include <bits/stdc++.h> using namespace std; #define all(arr) (arr).begin(), (arr).end() #define ll long long #define ld long double #define pb push_back #define sz(x) (int)(x).size() #define fi first #define se second #define endl '\n' vector<int> nums = {4, 5, 7, 9}; bool first1; int query(std::vector<int> a, std::vector<int> b){ if (first1) swap(a, b); return Query(sz(a), a, b); } int ask(int idx, vector<vector<int>> a, bool type){ if (a[0].empty()){ a[0] = a[1] = {1}; } if (idx == -1){ int to_add = sz(nums); for (int i = 0; i < to_add; i++){ a[type].pb(sz(a[type]) + 1); a[type ^ 1].pb(sz(a[type ^ 1]) + 1 + to_add); } for (int i = 0; i < to_add + 1; i++) a[0].pb(sz(a[0])), a[1].pb(sz(a[1])); int node = query(a[0], a[1]); int midnode = sz(a[0]) - to_add - 1; if (node == midnode) return 0; else if (node < midnode) return node - (midnode - to_add) + 1; else return node - midnode; } int to_add = nums[idx]; for (int i = 0; i < to_add; i++) a[type].pb(sz(a[type]) + 1), a[type ^ 1].pb(sz(a[type ^ 1]) + to_add); a[type].back() = sz(a[type]) - to_add; for (int i = 0; i < to_add; i++) a[0].pb(sz(a[0])), a[1].pb(sz(a[1])); int node = query(a[0], a[1]); return (node - (sz(a[0]) - to_add * 2) + 1) % to_add; } int reconstruct(vector<int> mods){ for (int i = 0;; i++){ bool good = 1; for (int j = 0; j < sz(nums); j++){ good &= (i % nums[j] == mods[j]); } if (good) return i; } } vector<vector<int>> compress(vector<int>& dp, vector<pair<int, int>>& par, vector<int> blocks){ if (blocks.empty()) return vector<vector<int>>(2); blocks.pb(1); vector<bool> s; for (int i = 0; i < sz(blocks); i++){ while (blocks[i]--){ s.pb(i & 1); } } int n = sz(s); auto get = [&] (int idx){ return s[idx - 1]; }; for (int i = sz(dp); i <= n; i++){ int cur = get(i); dp.pb(dp.back() + 1); par.pb({1, 1}); for (int c = 2; c <= i; c++){ if (get(i - c + 1) == cur) break; int curdp = dp[i - c] + 1; if (curdp < dp[i]) dp[i] = curdp, par[i] = {1, c}; } for (int l = 2; l <= i; l++){ for (int c = 1; c * l <= i; c++){ if (get(i - c * l + 1) == cur) break; int curdp = dp[i - c * l] + l; if (curdp < dp[i]) dp[i] = curdp, par[i] = {l, c}; } } } vector<pair<int, bool>> cycles; for (int i = n; i > 0;){ cycles.pb({par[i].fi, get(i)}); i -= par[i].fi * par[i].se; } reverse(all(cycles)); vector<vector<int>> a(2); for (auto [l, type] : cycles){ a[type].pb(sz(a[type]) + l); a[type ^ 1].pb(sz(a[type ^ 1]) + (l > 1)); if (l == 1) continue; for (int i = 1; i < l; i++) a[0].pb(sz(a[0]) + 1), a[1].pb(sz(a[1]) + 1); a[0].back() -= l, a[1].back() -= l; } return a; } string Solve(int n){ first1 = (Query(3, {1, 1, 2}, {2, 1, 2}) == 2); vector<int> blocks, dp{0}; vector<pair<int, int>> par{pair{0, 0}}; while (accumulate(all(blocks), 0) < n - 1){ auto a = compress(dp, par, blocks); vector<int> mods(sz(nums)); int val = ask(-1, a, sz(blocks) & 1); if (val){ blocks.pb(val); continue; } for (int i = 0; i < sz(mods); i++) mods[i] = ask(i, a, sz(blocks) & 1); blocks.pb(reconstruct(mods)); } if (accumulate(all(blocks), 0) == n - 1) blocks.pb(1); string ans; for (int i = 0; i < sz(blocks); i++){ ans += string(blocks[i], '0' + ((i & 1) ^ first1)); } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...