#include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return {i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (c it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(x...) " [" #x ": " << (x) << "] " using ll = long long; namespace Rho { vector<ll> Rozklad(ll n); vector<ll> witness = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}; ll mul(ll a, ll b, ll mod) { return (__int128) a * b % mod; } ll my_pow(ll a, ll b, ll mod) { ll res = 1; while(b) { if(b % 2) res = mul(res, a, mod); a = mul(a, a, mod); b /= 2; } return res; } bool test(ll n) { if(n == 2) return true; if(n < 2 || n % 2 == 0) return false; ll d = n - 1, s = 0; while(d % 2 == 0) { d /= 2; ++s; } for(auto i : witness) if(i % n) { ll x = my_pow(i, d, n); if(x == 1) continue; bool zlozona = true; for(int j = 0; j < s; ++j) { if(x == n - 1) { zlozona = false; break; } x = mul(x, x, n); } if(zlozona) return false; } return true; } ll f(ll x, ll mod, ll c) { ll y = mul(x, x, mod) + c; if(y > mod) y -= mod; return y; } void rho(ll n, vector<ll> & w) { if(n <= 1) return; if(test(n)) { w.push_back(n); return; } for(ll c = 1; true; ++c) { ll x = 2, y = 2, d = 1; while(d == 1) { x = f(x, n, c); y = f( f(y,n,c), n, c); d = __gcd(abs(x - y), n); } if(d < n) { rho(d, w); rho(n / d, w); return; } } } vector<ll> Rozklad(ll n) { vector<ll> w; for(int i = 2; i <= 100; ++i) while(n % i == 0) { n /= i; w.push_back(i); } rho(n, w); sort(w.begin(), w.end()); return w; } } // namespace Rho template <typename T> T Maxi(T& a, T b) { return a = max(a, b); } template <typename T> T Mini(T& a, T b) { return a = min(a, b); } #include "dzilib.h" /* int GetT(); long long GetN(); int GetQ(); long long GetC(); long long Ask(long long y); void Answer(long long z); */ ll Dzielniki(ll n) { ll last_p = -1; ll last_a = 0; ll wynik = 1; for (const ll p : Rho::Rozklad(n)) { if (p == last_p) { last_a++; } else { if (last_p != -1) { wynik *= (last_a + 1); } last_p = p; last_a = 1; } } if (last_p != -1) { wynik *= (last_a + 1); } return wynik; } std::mt19937 rnd(0x8923921); struct Test { ll Run(ll n) { const ll y236 = Znajdz236(uniform_int_distribution<ll>(1, n)(rnd)); assert(y236 != -1); debug() << imie(y236); vector<ll> kandydaci; for (ll i = (y236 >> 36) + 1; (i << 36) <= n + y236; i++) { const ll kandydat = (i << 36) - y236; assert(kandydat >= 1); if (OkKandydat(kandydat)) { kandydaci.push_back(kandydat); } } assert(!kandydaci.empty()); while ((int) kandydaci.size() > 1) { debug() << imie(kandydaci.size()) imie(zapytania.size()); std::uniform_int_distribution<ll> dist(0, GetC()); const ll y = dist(rnd); const ll d = Zapytanie(y); for (int i = 0; i < (int) kandydaci.size(); i++) { if (Dzielniki(kandydaci[i] + y) != d) { swap(kandydaci[i], kandydaci.back()); kandydaci.pop_back(); } } } debug() << imie(kandydaci); assert((int) kandydaci.size() == 1); return kandydaci[0]; } ll Znajdz236(ll start_y) { const ll y = Baktracz(0, start_y); if (y != -1) return y; return Baktracz(0, start_y + 1ll); } // 2^pot | (x + y) and 2^(pot+1) not| (x + y) // Returns y such that 2^36 | (x + y) or -1 if contradiction. ll Baktracz(int pot, ll y) { if (Zapytanie(y) % (pot + 1) != 0) { return -1; } if (pot >= 36) { return y; } ll y1 = y + (1ll << pot); ll y2 = y + (1ll << pot) + (1ll << (pot + 1)); if (uniform_int_distribution<int>(0, 1)(rnd) == 1) { swap(y1, y2); } const ll res = Baktracz(pot + 1, y1); if (res != -1) return res; return Baktracz(pot + 1, y2); } ll Zapytanie(ll y) { auto it = zapytania.find(y); if (it != zapytania.end()) { return it->second; } return zapytania[y] = Ask(y); } bool OkKandydat(ll x) { for (const auto& [y, d] : zapytania) { if (Dzielniki(x + y) != d) { return false; } } return true; } unordered_map<ll, ll> zapytania; }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); rnd.seed(time(NULL)); const int t = GetT(); for (int i = 0; i < t; i++) { Answer(Test().Run(GetN())); } 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 | #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return {i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (c it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(x...) " [" #x ": " << (x) << "] " using ll = long long; namespace Rho { vector<ll> Rozklad(ll n); vector<ll> witness = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}; ll mul(ll a, ll b, ll mod) { return (__int128) a * b % mod; } ll my_pow(ll a, ll b, ll mod) { ll res = 1; while(b) { if(b % 2) res = mul(res, a, mod); a = mul(a, a, mod); b /= 2; } return res; } bool test(ll n) { if(n == 2) return true; if(n < 2 || n % 2 == 0) return false; ll d = n - 1, s = 0; while(d % 2 == 0) { d /= 2; ++s; } for(auto i : witness) if(i % n) { ll x = my_pow(i, d, n); if(x == 1) continue; bool zlozona = true; for(int j = 0; j < s; ++j) { if(x == n - 1) { zlozona = false; break; } x = mul(x, x, n); } if(zlozona) return false; } return true; } ll f(ll x, ll mod, ll c) { ll y = mul(x, x, mod) + c; if(y > mod) y -= mod; return y; } void rho(ll n, vector<ll> & w) { if(n <= 1) return; if(test(n)) { w.push_back(n); return; } for(ll c = 1; true; ++c) { ll x = 2, y = 2, d = 1; while(d == 1) { x = f(x, n, c); y = f( f(y,n,c), n, c); d = __gcd(abs(x - y), n); } if(d < n) { rho(d, w); rho(n / d, w); return; } } } vector<ll> Rozklad(ll n) { vector<ll> w; for(int i = 2; i <= 100; ++i) while(n % i == 0) { n /= i; w.push_back(i); } rho(n, w); sort(w.begin(), w.end()); return w; } } // namespace Rho template <typename T> T Maxi(T& a, T b) { return a = max(a, b); } template <typename T> T Mini(T& a, T b) { return a = min(a, b); } #include "dzilib.h" /* int GetT(); long long GetN(); int GetQ(); long long GetC(); long long Ask(long long y); void Answer(long long z); */ ll Dzielniki(ll n) { ll last_p = -1; ll last_a = 0; ll wynik = 1; for (const ll p : Rho::Rozklad(n)) { if (p == last_p) { last_a++; } else { if (last_p != -1) { wynik *= (last_a + 1); } last_p = p; last_a = 1; } } if (last_p != -1) { wynik *= (last_a + 1); } return wynik; } std::mt19937 rnd(0x8923921); struct Test { ll Run(ll n) { const ll y236 = Znajdz236(uniform_int_distribution<ll>(1, n)(rnd)); assert(y236 != -1); debug() << imie(y236); vector<ll> kandydaci; for (ll i = (y236 >> 36) + 1; (i << 36) <= n + y236; i++) { const ll kandydat = (i << 36) - y236; assert(kandydat >= 1); if (OkKandydat(kandydat)) { kandydaci.push_back(kandydat); } } assert(!kandydaci.empty()); while ((int) kandydaci.size() > 1) { debug() << imie(kandydaci.size()) imie(zapytania.size()); std::uniform_int_distribution<ll> dist(0, GetC()); const ll y = dist(rnd); const ll d = Zapytanie(y); for (int i = 0; i < (int) kandydaci.size(); i++) { if (Dzielniki(kandydaci[i] + y) != d) { swap(kandydaci[i], kandydaci.back()); kandydaci.pop_back(); } } } debug() << imie(kandydaci); assert((int) kandydaci.size() == 1); return kandydaci[0]; } ll Znajdz236(ll start_y) { const ll y = Baktracz(0, start_y); if (y != -1) return y; return Baktracz(0, start_y + 1ll); } // 2^pot | (x + y) and 2^(pot+1) not| (x + y) // Returns y such that 2^36 | (x + y) or -1 if contradiction. ll Baktracz(int pot, ll y) { if (Zapytanie(y) % (pot + 1) != 0) { return -1; } if (pot >= 36) { return y; } ll y1 = y + (1ll << pot); ll y2 = y + (1ll << pot) + (1ll << (pot + 1)); if (uniform_int_distribution<int>(0, 1)(rnd) == 1) { swap(y1, y2); } const ll res = Baktracz(pot + 1, y1); if (res != -1) return res; return Baktracz(pot + 1, y2); } ll Zapytanie(ll y) { auto it = zapytania.find(y); if (it != zapytania.end()) { return it->second; } return zapytania[y] = Ask(y); } bool OkKandydat(ll x) { for (const auto& [y, d] : zapytania) { if (Dzielniki(x + y) != d) { return false; } } return true; } unordered_map<ll, ll> zapytania; }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); rnd.seed(time(NULL)); const int t = GetT(); for (int i = 0; i < t; i++) { Answer(Test().Run(GetN())); } return 0; } |