fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28. int L;
  29. int R;
  30.  
  31. vector<bool> isPrime;
  32.  
  33. void seive(){
  34. L = 1e12;
  35. R = 1e12+1e6;
  36.  
  37. int rootR = sqrt(R);
  38.  
  39. vector<bool> basePrime(rootR+1, 1);
  40. basePrime[0] = basePrime[1] = 0;
  41.  
  42. vector<int> primes;
  43.  
  44.  
  45. for(int i=2; i<=rootR; i++){
  46. if(basePrime[i] == 0) continue;
  47.  
  48. primes.push_back(i);
  49. for(int j=i*i; j<=rootR; j+=i){
  50. basePrime[j] = 0;
  51. }
  52. }
  53.  
  54.  
  55. isPrime.assign(R-L+1, 1);
  56.  
  57. for(auto& p : primes){
  58.  
  59. int firstMultiple = ( (L+p-1)/p ) * p;
  60.  
  61. int start = max(firstMultiple , p*p);
  62.  
  63. for(int j = start; j<=R; j+=p){
  64. isPrime[j - L] = 0;
  65. }
  66.  
  67. }
  68.  
  69. if(L == 0) {
  70. if(R >= 0) isPrime[0] = false;
  71. if(R >= 1) isPrime[1] = false;
  72. }
  73. if(L == 1) isPrime[0] = false;
  74.  
  75. }
  76.  
  77. void consistency(int l, int r){
  78.  
  79. if(l < L || r > R){
  80. cout << "Query outside precomputed range\n";
  81. return;
  82. }
  83.  
  84. for(int i=l; i<=r ; i++){
  85. if(isPrime[i-L]) cout << i << " ";
  86. }cout << endl;
  87.  
  88. }
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. void practice(int l, int r){
  105.  
  106.  
  107. }
  108.  
  109.  
  110.  
  111.  
  112.  
  113. void solve() {
  114. static int _ = (seive(), 0);
  115.  
  116. int l, r;
  117. cin>> l >> r;
  118.  
  119. consistency(l, r);
  120.  
  121.  
  122. }
  123.  
  124.  
  125.  
  126.  
  127.  
  128. int32_t main() {
  129. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  130.  
  131. int t = 1;
  132. cin >> t;
  133. while (t--) {
  134. solve();
  135. }
  136.  
  137. return 0;
  138. }
Success #stdin #stdout 0.02s 5324KB
stdin
2
1000000000039 1000000000099
1000000000030 1000000000150  
stdout
1000000000039 1000000000061 1000000000063 1000000000091 
1000000000039 1000000000061 1000000000063 1000000000091 1000000000121