-
Notifications
You must be signed in to change notification settings - Fork 16
/
PRIME1.cpp
64 lines (53 loc) · 788 Bytes
/
PRIME1.cpp
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
#include<bits/stdc++.h>
#define lli long long int
#define vi vector<int>
//segmented sieve
using namespace std;
vi primes;
int prime[100001];
void sieve(int maxN)
{
vi ar(maxN + 1 , 0);
ar[1] = 1;
for(int i=2;i<=maxN;i++)
if(ar[i] == 0)
{
for(int j=2*i;j<=maxN;j+=i)
ar[j] = 1;
}
for(int i =1 ; i<= maxN ; i++)
if(ar[i] == 0)
primes.push_back(i);
}
void init(int L , int R)
{
if(L == 1) L++;
int maxN = R - L + 1;
vi ar(maxN , 0);
for(lli p : primes)
if(p*p <= R)
{
int i = (L / p) * p;
if(i < L) i += p;
for(;i<=R;i+=p)
{
if(i != p)
ar[i-L] = 1;
}
}
for(int i=0;i<maxN;i++)
if(ar[i] == 0)
cout<<L + i<<endl;
}
int main()
{
sieve(100000);
int t , L , R;
cin>>t;
while(t--)
{
cin>>L>>R;
init(L , R);
cout<<endl;
}
}