-
Notifications
You must be signed in to change notification settings - Fork 0
/
segmented_sieve.cpp
59 lines (48 loc) · 1.03 KB
/
segmented_sieve.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
//segmented sieve algorithm
#include<bits/stdc++.h>
#define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
using namespace std;
vector<ll> v;
void sieve(ll n){
ll prime[n]= {};
for(ll i=3; i*i<=n; i+=2){
if(prime[i]==0){
for(ll j=i*i; j<=n; j+=i+i){
prime[j] = 1;
}
}
}
v.push_back(2);
for(ll i=3; i<=n; i+=2){
if(prime[i]==0) v.push_back(i);
}
}
void segmented_sieve(ll l,ll r){
if(l==1) l++;
ll mx = r-l+1;
ll a[mx+1] = {};
for(int p:v){
if(p*p<=r){
int i = (l/p)*p;
if(i<l) i+=p;
for(; i<=r; i+=p){
if(i!=p){
a[i-l] = 1;
}
}
}
}
for(int i=0; i<mx; i++){
if(a[i]==0) cout<< i+l <<" ";
} cout<< '\n';
}
int main()
{
sync;
sieve(100000);
ll l,r;
cin>>l>>r;
segmented_sieve(l,r);
return 0;
}