-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprime1.cpp
More file actions
60 lines (51 loc) · 1.38 KB
/
prime1.cpp
File metadata and controls
60 lines (51 loc) · 1.38 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
58
59
60
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
void simpleSieve(int limit, vector<int> &prime) {
bool mark[limit+1];
memset(mark, false, sizeof(mark));
for (long long int i=2; i*i<limit; i++)
if (mark[i] == false)
for (long long int i=i*2; i<limit; i+=i)
mark[i] = true;
for (int i=2; i<limit; i++)
if (mark[i] == false)
prime.push_back(i);
}
void segmentedSieve(long long int n, long long int l) {
int limit = floor(sqrt(n))+1;
vector<int> prime;
simpleSieve(limit, prime);
bool mark[n-l+1];
memset(mark, false, sizeof(mark));
int i = 0;
while (i < prime.size() && prime[i] <= limit) {
long long int beg;
if (prime[i] >= l)
beg = prime[i] * 2;
else
beg = l + ((prime[i] - l % prime[i]) % prime[i]);
for (long long int j = beg; j <= n; j += prime[i]) {
mark[j - l] = true;
}
i++;
}
for (int i = 0; i < n-l; i++)
if (mark[i] == false)
cout << mark[i]+l << endl;
}
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int test_cases;
long long int lb, ub;
cin >> test_cases;
while (test_cases--) {
cin >> lb >> ub;
segmentedSieve(ub, lb);
cout << endl;
}
return 0;
}