-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSegmented_Sieve.cpp
59 lines (56 loc) · 1.81 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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void solve() {
ll N = sqrt(1e10);
bool prime[N];
memset(prime, true, sizeof(prime));
for (int i = 2; i * i <= N; i++) {
if (prime[i]) {
for (int j = i * i; j <= N; j += i) {
prime[j] = false;
}
}
}
vector<ll>primes;
primes.push_back(2);
for (int i = 3; i <= N; i += 2) {
if (prime[i]) {
primes.push_back(i);
}
}
ll t;
cin >> t;
while (t--) {
ll l , r;
cin >> l >> r;
bool isPrime[r - l + 1];
memset(isPrime, true, sizeof(isPrime));
for (int i = 0; primes[i]*primes[i] <= r; i++) {
ll currentPrime = primes[i];
ll base = (l / currentPrime) * currentPrime;
if (base < l) {
base += currentPrime;
}
for (int j = base; j <= r ; j += currentPrime) {
isPrime[j - l] = false;
if (base == currentPrime) {
isPrime[base - l] = true;
}
}
}
for (int i = 0; i < r - l + 1; i++) {
if (isPrime[i]) {
if (l + i != 1) {
cout << l + i << endl;
}
}
}
cout << endl;
}
}
int main()
{
solve();
return 0;
}