-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsegmentedsieve.cpp
More file actions
57 lines (53 loc) · 1.23 KB
/
segmentedsieve.cpp
File metadata and controls
57 lines (53 loc) · 1.23 KB
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
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define PI acos(-1)
#define pb push_back
#define int long long int
#define pi pair<int, int>
#define pii pair<int, pi>
#define fir first
#define sec second
#define DEBUG 0
#define MAXN 1000003
#define mod 1000000007
vector<int> prime;
void segmentedsieve(int l, int r)
{
int lim = sqrt(r);
vector<bool> mark(lim + 1, false);
vector<int> primes;
for (int i = 2; i <= lim; ++i)
{
if (!mark[i])
{
primes.pb(i);
for (int j = i * i; j <= lim; j += i)
mark[j] = true;
}
}
vector<bool> isprime(r - l + 1, true);
for (int i : primes)
for (int j = max(i * i, (l + i - 1) / i * i); j <= r; j += i)
isprime[j - l] = false;
if (l == 1)
isprime[0] = false;
for (int i = 0; i < isprime.size(); i++)
if (isprime[i])
prime.pb(i + l);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int l, r;
cin >> l >> r;
segmentedsieve(l, r);
for (auto const &i : prime)
cout << i << " ";
return 0;
}