forked from ajahuang/CodeChef
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrime_Generator.cpp
More file actions
63 lines (54 loc) · 1.4 KB
/
Prime_Generator.cpp
File metadata and controls
63 lines (54 loc) · 1.4 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
61
62
63
#include <iostream>
#include <vector>
#include <bitset>
using namespace std;
typedef long long ll;
const int IS_PRIME_SIZE = 31650;
bitset<IS_PRIME_SIZE> isPrime;
int primes[5000];
bitset<100001> interval;
int main()
{
// Prepare isPrime and primes.
isPrime.set();
isPrime.reset(1);
int primesIndex = 0;
for (int n = 2; n < IS_PRIME_SIZE; ++n)
{
if (isPrime.test(n))
{
primes[primesIndex++] = n;
for (int m = n * n; m < IS_PRIME_SIZE; m += n)
isPrime.reset(m);
}
}
interval.set();
int T;
scanf("%d", &T);
while ( T-- )
{
ll m, n;
scanf("%lld %lld", &m, &n);
if (m == 1)
++m;
for (int i = 0; i < 5000 && primes[i] * primes[i] <= n; ++i)
{
ll start = m % primes[i] == 0? m : m + (primes[i] - m % primes[i]);
if (start == primes[i])
start += primes[i];
for (ll idx = start - m; idx <= n - m; idx += primes[i])
interval.reset(idx);
}
for (ll x = m; x <= n; ++x)
{
if (interval[x - m] == 1)
printf("%lld\n", x);
else
interval.set(x - m);
}
// Separate the answers for each test case by an empty line.
if (T)
printf("\n");
}
return 0;
}