sachin-yadav

Untitled

Jun 5th, 2019
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. //using segmented sieve of eranthroses
  4. typedef long long ll;
  5. //range of int -2*10^9 to 2*10^9
  6. //range of ll -9*10^18 to 9*10^18
  7. int main()
  8. {   ios::sync_with_stdio(0);
  9.     cin.tie(0);
  10.     int rootN=sqrt(1000000000)+5;
  11.     bool prime[rootN];
  12.     memset(prime,1,sizeof(prime));
  13.     int i=2;
  14.     while(i<rootN)
  15.     {
  16.         if(prime[i])
  17.         {
  18.             int x=2*i;
  19.             while(x<rootN)
  20.             {
  21.                 prime[x]=0;
  22.                 x+=i;
  23.             }
  24.         }
  25.         i++;
  26.     }
  27.     vector<int> primes;
  28.     for(int i=2;i<rootN;i++)
  29.     if(prime[i]) primes.emplace_back(i);
  30.     // for(int x:primes) cout<<x<<" ";
  31.     int t;
  32.     cin>>t;
  33.     while(t--)
  34.     {
  35.         ll m,n;
  36.         cin>>m>>n;
  37.         bool result[n-m+1];
  38.         memset(result,1,sizeof(result));
  39.         ll sqroot=sqrt(n);
  40.         for(int i=0;primes[i]<=sqroot;i++)
  41.         {
  42.             ll x=max((ll)(2*primes[i]),primes[i]*(m/primes[i]));
  43.             while(x<=n)
  44.             {
  45.                 if(x>=m)
  46.                 result[x-m]=0;
  47.                 x+=primes[i];  
  48.             }        
  49.         }
  50.         int i=0;
  51.         if(m==1)i++;
  52.         for( ;i<n-m+1;i++)
  53.         if(result[i])cout<<i+m<<"\n";
  54.         cout<<"\n";
  55.     }
  56.    
  57.     return 0;
  58. }
Add Comment
Please, Sign In to add comment