#include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef DEBUG #include "debug.h" #else #include "dzilib.h" #define debug(...) 42 #endif #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) #define each(a,x) for(auto& a : (x)) #define sz(x) int((x).size()) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } typedef unsigned long long ull; ull modmul(ull a, ull b, ull M) { ll ret = a * b - M * ull(1.L / M * a * b); return ret + M * (ret < 0) - M * (ret >= (ll)M); } ull modpow(ull b, ull e, ull mod) { ull ans = 1; for (; e; b = modmul(b, b, mod), e /= 2) if (e & 1) ans = modmul(ans, b, mod); return ans; } bool isPrime(ull n) { if (n < 2 || n % 6 % 4 != 1) return (n | 1) == 3; ull A[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}, s = __builtin_ctzll(n-1), d = n >> s; for (ull a : A) { // ^ count t r a i l i n g zeroes ull p = modpow(a%n, d, n), i = s; while (p != 1 && p != n - 1 && a % n && i--) p = modmul(p, p, n); if (p != n-1 && i != s) return 0; } return 1; } ull pollard(ull n) { auto f = [n](ull x) { return modmul(x, x, n) + 1; }; ull x = 0, y = 0, t = 30, prd = 2, i = 1, q; while (t++ % 40 || __gcd(prd, n) == 1) { if (x == y) x = ++i, y = f(x); if ((q = modmul(prd, max(x,y) - min(x,y), n))) prd = q; x = f(x), y = f(f(y)); } return __gcd(prd, n); } vector<ull> factor(ull n) { if (n == 1) return {}; if (isPrime(n)) return {n}; ull x = pollard(n); auto l = factor(x), r = factor(n / x); l.insert(l.end(), all(r)); return l; } int tau(ll x) { // debug(x); int r = 1; auto process = [&](ull p) { int v = 0; while (x % p == 0) { x /= p; ++v; } r *= v + 1; }; process(2); process(3); for (ull p : factor(x)) process(p); return r; } #ifdef DEBUG namespace hidden { int T, Q, count = 0; ll N, C; vll xs = []() -> vll { cin >> T >> N >> Q >> C; vll xlist(T); for (auto &a : xlist) cin >> a; return {rall(xlist)}; }(); } // Spytaj o T - liczbę przypadków testowych. int GetT() { return hidden::T; } // Spytaj o N - ograniczenie na x. long long GetN() { return hidden::N; } // Spytaj o Q - limit na liczbę zadanych zapytań. int GetQ() { return hidden::Q; } // Spytaj o C - ograniczenie na y. long long GetC() { return hidden::C; } // Spytaj o liczbę dzielników liczby x+y. long long Ask(long long y) { assert(++hidden::count <= hidden::Q && y <= hidden::C); return tau(hidden::xs.back() + y); } // Udziel odpowiedzi. void Answer(long long z) { assert(z == hidden::xs.back()); hidden::xs.pop_back(); } #endif map<ll, int> asked; int ask(ll x) { if (!asked.count(x)) asked[x] = Ask(x); return asked[x]; } bool verify(ll x) { for (auto [q, ans] : asked) if (tau(q + x) != ans) return false; return true; } const ll INF = 1e18; mt19937_64 rnd(263448562); array<array<ll, 60>, 60> powers; array<int, 240> v2s, v3s; void solvetc(ll N, int Q, ll C) { assert(C && Q); ll add = rnd() % N + N / 3; int v2 = 0, v3 = 0; array<array<int, 3>, 2> chineese{}; rep(i, 6) chineese[i % 2][i % 3] = i; while (powers[v2][v3] * 5000 < N) { vi taus; auto psh = [&]() { taus.push_back(ask(add + powers[v2][v3] * sz(taus))); }; psh(); psh(); psh(); array<int, 6> can{}; while (true) { fill(all(can), 0); rep(m2, sz(taus)) rep(m3, sz(taus)) { bool ok = false; repp(nv2, v2 + 1, sz(powers)) repp(nv3, v3 + 1, sz(powers[0])) { if (powers[nv2][nv3] == INF) break; if (2 * powers[nv2 - v2][0] <= sz(taus)) continue; if (3 * powers[0][nv3 - v3] <= sz(taus)) continue; // debug(nv2, nv3, m2, m3); if (m2 == m3) { if (taus[m2] % ((nv2 + 1) * (nv3 + 1))) continue; } else { if (taus[m2] % ((nv2 + 1) * (v3s[abs(m2 - m3)] + 1 + v3))) continue; if (taus[m3] % ((nv3 + 1) * (v2s[abs(m2 - m3)] + 1 + v2))) continue; } bool skip = false; rep(i, sz(taus)) if (i != m2 && i != m3) if (taus[i] % ((v2s[abs(i - m2)] + 1 + v2) * (v3s[abs(i - m3)] + 1 + v3))) skip = true; if (!skip) ok = true; } if (ok) can[chineese[m2 % 2][m3 % 3]] = true; } // debug(taus, can); int acc = accumulate(all(can), 0); assert(acc >= 1); if (acc == 1) break; psh(); } // debug(taus, can); int id = 0; while (!can[id]) ++id; add += id * powers[v2][v3]; ++v2; ++v3; } // debug(sz(asked), powers[v2][v3]); add = ((-add) % powers[v2][v3] + powers[v2][v3]) % powers[v2][v3]; // debug(add, powers[v2][v3]); while (!verify(add)) add += powers[v2][v3]; // debug(add); Answer(add); } void solve() { rep(i, sz(powers)) rep(j, sz(powers[0])) { powers[i][j] = 1; if (i) powers[i][j] = 2 * powers[i-1][j]; if (j) powers[i][j] = 3 * powers[i][j-1]; mini(powers[i][j], INF); } repp(i, 1, sz(v2s)) if (i % 2 == 0) v2s[i] = 1 + v2s[i / 2]; repp(i, 1, sz(v3s)) if (i % 3 == 0) v3s[i] = 1 + v3s[i / 3]; int T = GetT(), Q = GetQ(); ll N = GetN(), C = GetC(); while (T--) { asked.clear(); solvetc(N, Q, C); } debug(hidden::count); } int main() { #ifdef DEBUG const int MEMSIZE = 1024 * 1024 * 1024; static_assert(MEMSIZE % 16 == 0); static char stack[MEMSIZE]; asm volatile ( "mov %[newstack], %%rsp\n" "call *%[funcptr]" :: [funcptr] "r" (solve), [newstack] "r" (stack + MEMSIZE) ); exit(0); #else ios_base::sync_with_stdio(false); cin.tie(nullptr); solve(); #endif }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | #include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef DEBUG #include "debug.h" #else #include "dzilib.h" #define debug(...) 42 #endif #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) #define each(a,x) for(auto& a : (x)) #define sz(x) int((x).size()) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } typedef unsigned long long ull; ull modmul(ull a, ull b, ull M) { ll ret = a * b - M * ull(1.L / M * a * b); return ret + M * (ret < 0) - M * (ret >= (ll)M); } ull modpow(ull b, ull e, ull mod) { ull ans = 1; for (; e; b = modmul(b, b, mod), e /= 2) if (e & 1) ans = modmul(ans, b, mod); return ans; } bool isPrime(ull n) { if (n < 2 || n % 6 % 4 != 1) return (n | 1) == 3; ull A[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}, s = __builtin_ctzll(n-1), d = n >> s; for (ull a : A) { // ^ count t r a i l i n g zeroes ull p = modpow(a%n, d, n), i = s; while (p != 1 && p != n - 1 && a % n && i--) p = modmul(p, p, n); if (p != n-1 && i != s) return 0; } return 1; } ull pollard(ull n) { auto f = [n](ull x) { return modmul(x, x, n) + 1; }; ull x = 0, y = 0, t = 30, prd = 2, i = 1, q; while (t++ % 40 || __gcd(prd, n) == 1) { if (x == y) x = ++i, y = f(x); if ((q = modmul(prd, max(x,y) - min(x,y), n))) prd = q; x = f(x), y = f(f(y)); } return __gcd(prd, n); } vector<ull> factor(ull n) { if (n == 1) return {}; if (isPrime(n)) return {n}; ull x = pollard(n); auto l = factor(x), r = factor(n / x); l.insert(l.end(), all(r)); return l; } int tau(ll x) { // debug(x); int r = 1; auto process = [&](ull p) { int v = 0; while (x % p == 0) { x /= p; ++v; } r *= v + 1; }; process(2); process(3); for (ull p : factor(x)) process(p); return r; } #ifdef DEBUG namespace hidden { int T, Q, count = 0; ll N, C; vll xs = []() -> vll { cin >> T >> N >> Q >> C; vll xlist(T); for (auto &a : xlist) cin >> a; return {rall(xlist)}; }(); } // Spytaj o T - liczbę przypadków testowych. int GetT() { return hidden::T; } // Spytaj o N - ograniczenie na x. long long GetN() { return hidden::N; } // Spytaj o Q - limit na liczbę zadanych zapytań. int GetQ() { return hidden::Q; } // Spytaj o C - ograniczenie na y. long long GetC() { return hidden::C; } // Spytaj o liczbę dzielników liczby x+y. long long Ask(long long y) { assert(++hidden::count <= hidden::Q && y <= hidden::C); return tau(hidden::xs.back() + y); } // Udziel odpowiedzi. void Answer(long long z) { assert(z == hidden::xs.back()); hidden::xs.pop_back(); } #endif map<ll, int> asked; int ask(ll x) { if (!asked.count(x)) asked[x] = Ask(x); return asked[x]; } bool verify(ll x) { for (auto [q, ans] : asked) if (tau(q + x) != ans) return false; return true; } const ll INF = 1e18; mt19937_64 rnd(263448562); array<array<ll, 60>, 60> powers; array<int, 240> v2s, v3s; void solvetc(ll N, int Q, ll C) { assert(C && Q); ll add = rnd() % N + N / 3; int v2 = 0, v3 = 0; array<array<int, 3>, 2> chineese{}; rep(i, 6) chineese[i % 2][i % 3] = i; while (powers[v2][v3] * 5000 < N) { vi taus; auto psh = [&]() { taus.push_back(ask(add + powers[v2][v3] * sz(taus))); }; psh(); psh(); psh(); array<int, 6> can{}; while (true) { fill(all(can), 0); rep(m2, sz(taus)) rep(m3, sz(taus)) { bool ok = false; repp(nv2, v2 + 1, sz(powers)) repp(nv3, v3 + 1, sz(powers[0])) { if (powers[nv2][nv3] == INF) break; if (2 * powers[nv2 - v2][0] <= sz(taus)) continue; if (3 * powers[0][nv3 - v3] <= sz(taus)) continue; // debug(nv2, nv3, m2, m3); if (m2 == m3) { if (taus[m2] % ((nv2 + 1) * (nv3 + 1))) continue; } else { if (taus[m2] % ((nv2 + 1) * (v3s[abs(m2 - m3)] + 1 + v3))) continue; if (taus[m3] % ((nv3 + 1) * (v2s[abs(m2 - m3)] + 1 + v2))) continue; } bool skip = false; rep(i, sz(taus)) if (i != m2 && i != m3) if (taus[i] % ((v2s[abs(i - m2)] + 1 + v2) * (v3s[abs(i - m3)] + 1 + v3))) skip = true; if (!skip) ok = true; } if (ok) can[chineese[m2 % 2][m3 % 3]] = true; } // debug(taus, can); int acc = accumulate(all(can), 0); assert(acc >= 1); if (acc == 1) break; psh(); } // debug(taus, can); int id = 0; while (!can[id]) ++id; add += id * powers[v2][v3]; ++v2; ++v3; } // debug(sz(asked), powers[v2][v3]); add = ((-add) % powers[v2][v3] + powers[v2][v3]) % powers[v2][v3]; // debug(add, powers[v2][v3]); while (!verify(add)) add += powers[v2][v3]; // debug(add); Answer(add); } void solve() { rep(i, sz(powers)) rep(j, sz(powers[0])) { powers[i][j] = 1; if (i) powers[i][j] = 2 * powers[i-1][j]; if (j) powers[i][j] = 3 * powers[i][j-1]; mini(powers[i][j], INF); } repp(i, 1, sz(v2s)) if (i % 2 == 0) v2s[i] = 1 + v2s[i / 2]; repp(i, 1, sz(v3s)) if (i % 3 == 0) v3s[i] = 1 + v3s[i / 3]; int T = GetT(), Q = GetQ(); ll N = GetN(), C = GetC(); while (T--) { asked.clear(); solvetc(N, Q, C); } debug(hidden::count); } int main() { #ifdef DEBUG const int MEMSIZE = 1024 * 1024 * 1024; static_assert(MEMSIZE % 16 == 0); static char stack[MEMSIZE]; asm volatile ( "mov %[newstack], %%rsp\n" "call *%[funcptr]" :: [funcptr] "r" (solve), [newstack] "r" (stack + MEMSIZE) ); exit(0); #else ios_base::sync_with_stdio(false); cin.tie(nullptr); solve(); #endif } |