#line 1 "Main.cpp" #include #include #include #include #include #line 2 "nachia\\math\\prime-sieve-explicit.hpp" #line 5 "nachia\\math\\prime-sieve-explicit.hpp" #include #line 7 "nachia\\math\\prime-sieve-explicit.hpp" namespace nachia{ namespace prime_sieve_explicit_internal{ std::vector isprime = { false }; // a[x] := isprime(2x+1) void CalcIsPrime(int z){ if((int)isprime.size() *2+1 < z+1){ int new_z = isprime.size(); while(new_z*2+1 < z+1) new_z *= 2; z = new_z-1; isprime.resize(z+1, true); for(int i=1; i*(i+1)*2<=z; i++) if(isprime[i]){ for(int j=i*(i+1)*2; j<=z; j+=i*2+1) isprime[j] = false; } } } std::vector prime_list = {2}; int prime_list_max = 0; void CalcPrimeList(int z){ while((int)prime_list.size() < z){ if((int)isprime.size() <= prime_list_max + 1) CalcIsPrime(prime_list_max + 1); for(int p=prime_list_max+1; p<(int)isprime.size(); p++){ if(isprime[p]) prime_list.push_back(p*2+1); } prime_list_max = isprime.size() - 1; } } void CalcPrimeListUntil(int z){ if(prime_list_max < z){ CalcIsPrime(z); for(int p=prime_list_max+1; p<(int)isprime.size(); p++){ if(isprime[p]) prime_list.push_back(p*2+1); } prime_list_max = isprime.size() - 1; } } } bool IsprimeExplicit(int n){ using namespace prime_sieve_explicit_internal; if(n == 2) return true; if(n % 2 == 0) return false; CalcIsPrime(n); return isprime[(n-1)/2]; } int NthPrimeExplicit(int n){ using namespace prime_sieve_explicit_internal; CalcPrimeList(n); return prime_list[n]; } int PrimeCountingExplicit(int n){ using namespace prime_sieve_explicit_internal; if(n < 2) return 0; CalcPrimeListUntil(n); auto res = std::upper_bound(prime_list.begin(), prime_list.end(), n) - prime_list.begin(); return (int)res; } // [l, r) std::vector SegmentedSieveExplicit(long long l, long long r){ assert(0 <= l); assert(l <= r); long long d = r - l; if(d == 0) return {}; std::vector res(d, true); for(long long p=2; p*p<=r; p++) if(IsprimeExplicit(p)){ long long il = (l+p-1)/p, ir = (r+p-1)/p; if(il <= p) il = p; for(long long i=il; i; int main(){ int k; cin >> k; vector P(k), E(k); rep(i,k) cin >> P[i] >> E[i]; int Pmax = P.back(); int PCnt = nachia::PrimeCountingExplicit(Pmax); vector Pex(PCnt); rep(i,k) Pex[nachia::PrimeCountingExplicit(P[i])-1] = E[i]; vector> nx(Pmax+1); rep(i,PCnt){ int p = nachia::NthPrimeExplicit(i); for(int q=1; q*p<=Pmax; q++) nx[p*q] = {i,q}; } vector Pnow(PCnt); int MatchCount = 0; rep(i,PCnt) if(Pnow[i] == Pex[i]) MatchCount++; for(int r=1; r<=Pmax; r++){ for(int x=Pmax-r+1; x>1; x=nx[x].second){ int p = nx[x].first; if(Pnow[p] == Pex[p]) MatchCount--; Pnow[p]++; if(Pnow[p] == Pex[p]) MatchCount++; } for(int x=r; x>1; x=nx[x].second){ int p = nx[x].first; if(Pnow[p] == Pex[p]) MatchCount--; Pnow[p]--; if(Pnow[p] == Pex[p]) MatchCount++; } if(MatchCount == PCnt){ cout << Pmax << ' ' << r << endl; return 0; } } cout << "-1 -1\n"; return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;