#include <bits/stdc++.h> #include "dzilib.h" using namespace std; namespace factorize { struct Mint { uint64_t n; static uint64_t mod, inv, r2; Mint() : n(0) { } Mint(const uint64_t &x) : n(init(x)) { } static uint64_t init(const uint64_t &w) { return reduce(__uint128_t(w) * r2); } static void set_mod(const uint64_t &m) { mod = inv = m; for(int i = 0; i < 5; i++) inv *= 2 - inv * m; r2 = -__uint128_t(m) % m; } static uint64_t reduce(const __uint128_t &x) { uint64_t y = uint64_t(x >> 64) - uint64_t((__uint128_t(uint64_t(x) * inv) * mod) >> 64); return int64_t(y) < 0 ? y + mod : y; } Mint& operator+= (const Mint &x) { n += x.n - mod; if(int64_t(n) < 0) n += mod; return *this; } Mint& operator+ (const Mint &x) const { return Mint(*this) += x; } Mint& operator*= (const Mint &x) { n = reduce(__uint128_t(n) * x.n); return *this; } Mint& operator* (const Mint &x) const { return Mint(*this) *= x; } uint64_t val() const { return reduce(n); } }; uint64_t Mint::mod, Mint::inv, Mint::r2; bool suspect(const uint64_t &a, const uint64_t &s, uint64_t d, const uint64_t &n) { if(Mint::mod != n) Mint::set_mod(n); Mint x(1), xx(a), o(x), m(n - 1); while(d > 0) { if(d & 1) x *= xx; xx *= xx; d >>= 1; } if(x.n == o.n) return true; for(uint r = 0; r < s; r++) { if(x.n == m.n) return true; x *= x; } return false; } bool is_prime(const uint64_t &n) { if(n <= 1 || (n > 2 && (~n & 1))) return false; uint64_t d = n - 1, s = 0; while(~d & 1) s++, d >>= 1; static const uint64_t a_big[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}; static const uint64_t a_smo[] = {2, 7, 61}; if(n <= 4759123141LL) { for(auto &&p : a_smo) { if(p >= n) break; if(!suspect(p, s, d, n)) return false; } } else { for(auto &&p : a_big) { if(p >= n) break; if(!suspect(p, s, d, n)) return false; } } return true; } uint64_t rho_pollard(const uint64_t &n) { if(~n & 1) return 2u; static random_device rng; uniform_int_distribution<uint64_t> rr(1, n - 1); if(Mint::mod != n) Mint::set_mod(n); for(;;) { uint64_t c_ = rr(rng), g = 1, r = 1, m = 500; Mint y(rr(rng)), xx(0), c(c_), ys(0), q(1); while(g == 1) { xx.n = y.n; for(uint i = 1; i <= r; i++) y *= y, y += c; uint64_t k = 0; g = 1; while(k < r && g == 1) { for(uint i = 1; i <= (m > (r - k) ? (r - k) : m); i++) { ys.n = y.n; y *= y; y += c; uint64_t xxx = xx.val(), yyy = y.val(); q *= Mint(xxx > yyy ? xxx - yyy : yyy - xxx); } g = __gcd<uint64_t>(q.val(), n); k += m; } r *= 2; } if(g == n) g = 1; while(g == 1) { ys *= ys; ys += c; uint64_t xxx = xx.val(), yyy = ys.val(); g = __gcd<uint64_t>(xxx > yyy ? xxx - yyy : yyy - xxx, n); } if(g != n && is_prime(g)) return g; } assert(69 == 420); } template <typename T> vector<T> inter_factor(const T &n) { if(n < 2) return { }; if(is_prime(n)) return {n}; T d = rho_pollard(static_cast<uint64_t>(n)); vector<T> l = inter_factor(d), r = inter_factor(n / d); l.insert(l.end(), r.begin(), r.end()); return l; } template <typename T> vector<T> factor(T n) { vector<T> f1; for(uint i = 2; i <= 67; i += (i & 1) + 1) while(n % i == 0) f1.push_back(i), n /= i; vector<T> f2 = inter_factor(n); sort(f2.begin(), f2.end()); f1.insert(f1.end(), f2.begin(), f2.end()); return f1; } template <typename T> int count_divisors(T n) { assert(n > 0); if (n == 1) return 1; vector<T> f = factor(n); int ans = 1, cur = 1; for (int i = 1; i < (int) f.size(); i++) { if (f[i] != f[i - 1]) { ans *= cur + 1; cur = 0; } cur++; } ans *= cur + 1; return ans; } }; // factorize using factorize::is_prime; using factorize::factor; using factorize::count_divisors; typedef long long LL; mt19937_64 rng(2137); LL randint(LL a, LL b) { return uniform_int_distribution<LL>(a, b)(rng); } const int S = 1000000; bitset<S> sieve; vector<LL> primes; void make_sieve() { sieve.set(); sieve[0] = sieve[1] = false; for (int i = 2; i * i < S; i++) { if (sieve[i]) { for (int j = i * i; j < S; j += i) sieve.reset(j); } } for (int i = 2; i < S; i++) if (sieve[i]) primes.push_back(i); } namespace subtask12 { const int N = 1000000 + 200; int cnt[N]; void solve() { for (int i = 1; i < N; i++) for (int j = i; j < N; j += i) cnt[j]++; map<uint64_t, int> all; for (int i = 1; i + 50 < N; i++) { uint64_t h = 0; for (int j = 0; j < 50; j++) { h *= 32141; h += cnt[i + j]; } if (all.count(h)) { assert(false); } all[h] = i; } int t = GetT(); while (t--) { uint64_t h = 0; for (int j = 0; j < 50; j++) { h *= 32141; h += Ask(j); } Answer(all[h]); } } }; const LL inf = 1e18L; LL mul(LL a, LL b) { if (b == 0LL) return 0LL; if (a > inf / b) return inf; return a * b; } map<LL, LL> cache; LL ask(LL y) { if (cache.count(y)) return cache[y]; return cache[y] = Ask(y); } namespace subtask3 { void solve() { make_sieve(); // const LL TRUE_N = 100'000'000'000'000LL; const LL TRUE_N = 1'000'000'000LL; // const LL C = 100'000'000'000'000'000LL; const LL C = 1'000'000'000'000LL; // const LL N = 100'001'000'000'000LL; const LL N = 2 * 1'000'000'000LL; const int T = 100; const int CHECK = 40; const int BRUTE = 1000000; int t = GetT(); while (t--) { cache.clear(); // number has to have lots of ones in binary LL shift = randint(1LL, 1'000'000'000LL); auto common_divisor = [&](LL z, LL fail_at) { assert(shift + T * z <= C); for (LL d = 1; d <= T; d++) if (ask(shift + d * z) == fail_at) return false; return true; }; int cnt = 0; LL y = 1; for (int k = 1; y <= N; k++) { y *= 2; cnt++; if (common_divisor(y, 2 * k) == false) { shift += y / 2; } } Answer(y - shift); } } } int main() { int q = GetQ(); long long c = GetC(); long long n = GetN(); if (n <= 1000000) { subtask12::solve(); return 0; } if (n <= 1'000'000'000) { subtask3::solve(); return 0; } assert(false); return 0; }
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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | #include <bits/stdc++.h> #include "dzilib.h" using namespace std; namespace factorize { struct Mint { uint64_t n; static uint64_t mod, inv, r2; Mint() : n(0) { } Mint(const uint64_t &x) : n(init(x)) { } static uint64_t init(const uint64_t &w) { return reduce(__uint128_t(w) * r2); } static void set_mod(const uint64_t &m) { mod = inv = m; for(int i = 0; i < 5; i++) inv *= 2 - inv * m; r2 = -__uint128_t(m) % m; } static uint64_t reduce(const __uint128_t &x) { uint64_t y = uint64_t(x >> 64) - uint64_t((__uint128_t(uint64_t(x) * inv) * mod) >> 64); return int64_t(y) < 0 ? y + mod : y; } Mint& operator+= (const Mint &x) { n += x.n - mod; if(int64_t(n) < 0) n += mod; return *this; } Mint& operator+ (const Mint &x) const { return Mint(*this) += x; } Mint& operator*= (const Mint &x) { n = reduce(__uint128_t(n) * x.n); return *this; } Mint& operator* (const Mint &x) const { return Mint(*this) *= x; } uint64_t val() const { return reduce(n); } }; uint64_t Mint::mod, Mint::inv, Mint::r2; bool suspect(const uint64_t &a, const uint64_t &s, uint64_t d, const uint64_t &n) { if(Mint::mod != n) Mint::set_mod(n); Mint x(1), xx(a), o(x), m(n - 1); while(d > 0) { if(d & 1) x *= xx; xx *= xx; d >>= 1; } if(x.n == o.n) return true; for(uint r = 0; r < s; r++) { if(x.n == m.n) return true; x *= x; } return false; } bool is_prime(const uint64_t &n) { if(n <= 1 || (n > 2 && (~n & 1))) return false; uint64_t d = n - 1, s = 0; while(~d & 1) s++, d >>= 1; static const uint64_t a_big[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}; static const uint64_t a_smo[] = {2, 7, 61}; if(n <= 4759123141LL) { for(auto &&p : a_smo) { if(p >= n) break; if(!suspect(p, s, d, n)) return false; } } else { for(auto &&p : a_big) { if(p >= n) break; if(!suspect(p, s, d, n)) return false; } } return true; } uint64_t rho_pollard(const uint64_t &n) { if(~n & 1) return 2u; static random_device rng; uniform_int_distribution<uint64_t> rr(1, n - 1); if(Mint::mod != n) Mint::set_mod(n); for(;;) { uint64_t c_ = rr(rng), g = 1, r = 1, m = 500; Mint y(rr(rng)), xx(0), c(c_), ys(0), q(1); while(g == 1) { xx.n = y.n; for(uint i = 1; i <= r; i++) y *= y, y += c; uint64_t k = 0; g = 1; while(k < r && g == 1) { for(uint i = 1; i <= (m > (r - k) ? (r - k) : m); i++) { ys.n = y.n; y *= y; y += c; uint64_t xxx = xx.val(), yyy = y.val(); q *= Mint(xxx > yyy ? xxx - yyy : yyy - xxx); } g = __gcd<uint64_t>(q.val(), n); k += m; } r *= 2; } if(g == n) g = 1; while(g == 1) { ys *= ys; ys += c; uint64_t xxx = xx.val(), yyy = ys.val(); g = __gcd<uint64_t>(xxx > yyy ? xxx - yyy : yyy - xxx, n); } if(g != n && is_prime(g)) return g; } assert(69 == 420); } template <typename T> vector<T> inter_factor(const T &n) { if(n < 2) return { }; if(is_prime(n)) return {n}; T d = rho_pollard(static_cast<uint64_t>(n)); vector<T> l = inter_factor(d), r = inter_factor(n / d); l.insert(l.end(), r.begin(), r.end()); return l; } template <typename T> vector<T> factor(T n) { vector<T> f1; for(uint i = 2; i <= 67; i += (i & 1) + 1) while(n % i == 0) f1.push_back(i), n /= i; vector<T> f2 = inter_factor(n); sort(f2.begin(), f2.end()); f1.insert(f1.end(), f2.begin(), f2.end()); return f1; } template <typename T> int count_divisors(T n) { assert(n > 0); if (n == 1) return 1; vector<T> f = factor(n); int ans = 1, cur = 1; for (int i = 1; i < (int) f.size(); i++) { if (f[i] != f[i - 1]) { ans *= cur + 1; cur = 0; } cur++; } ans *= cur + 1; return ans; } }; // factorize using factorize::is_prime; using factorize::factor; using factorize::count_divisors; typedef long long LL; mt19937_64 rng(2137); LL randint(LL a, LL b) { return uniform_int_distribution<LL>(a, b)(rng); } const int S = 1000000; bitset<S> sieve; vector<LL> primes; void make_sieve() { sieve.set(); sieve[0] = sieve[1] = false; for (int i = 2; i * i < S; i++) { if (sieve[i]) { for (int j = i * i; j < S; j += i) sieve.reset(j); } } for (int i = 2; i < S; i++) if (sieve[i]) primes.push_back(i); } namespace subtask12 { const int N = 1000000 + 200; int cnt[N]; void solve() { for (int i = 1; i < N; i++) for (int j = i; j < N; j += i) cnt[j]++; map<uint64_t, int> all; for (int i = 1; i + 50 < N; i++) { uint64_t h = 0; for (int j = 0; j < 50; j++) { h *= 32141; h += cnt[i + j]; } if (all.count(h)) { assert(false); } all[h] = i; } int t = GetT(); while (t--) { uint64_t h = 0; for (int j = 0; j < 50; j++) { h *= 32141; h += Ask(j); } Answer(all[h]); } } }; const LL inf = 1e18L; LL mul(LL a, LL b) { if (b == 0LL) return 0LL; if (a > inf / b) return inf; return a * b; } map<LL, LL> cache; LL ask(LL y) { if (cache.count(y)) return cache[y]; return cache[y] = Ask(y); } namespace subtask3 { void solve() { make_sieve(); // const LL TRUE_N = 100'000'000'000'000LL; const LL TRUE_N = 1'000'000'000LL; // const LL C = 100'000'000'000'000'000LL; const LL C = 1'000'000'000'000LL; // const LL N = 100'001'000'000'000LL; const LL N = 2 * 1'000'000'000LL; const int T = 100; const int CHECK = 40; const int BRUTE = 1000000; int t = GetT(); while (t--) { cache.clear(); // number has to have lots of ones in binary LL shift = randint(1LL, 1'000'000'000LL); auto common_divisor = [&](LL z, LL fail_at) { assert(shift + T * z <= C); for (LL d = 1; d <= T; d++) if (ask(shift + d * z) == fail_at) return false; return true; }; int cnt = 0; LL y = 1; for (int k = 1; y <= N; k++) { y *= 2; cnt++; if (common_divisor(y, 2 * k) == false) { shift += y / 2; } } Answer(y - shift); } } } int main() { int q = GetQ(); long long c = GetC(); long long n = GetN(); if (n <= 1000000) { subtask12::solve(); return 0; } if (n <= 1'000'000'000) { subtask3::solve(); return 0; } assert(false); return 0; } |