-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimeDistance_UVA10140.cpp
109 lines (94 loc) · 2.53 KB
/
primeDistance_UVA10140.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
ifstream fin("primeDistance_UVA10140.in");
ofstream fout("primeDistance_UVA10140.out");
const int N = 46341;
const int INTMAX = 2147483647;
int main()
{
vector<bool> isPrime(1000000, true);
isPrime[0] = false, isPrime[1] = false;
vector<long long> primes;
for (long long i = 2; i <= N; ++i)
{
if (isPrime[i] == true)
{
primes.push_back(i);
for (long long j = i * i; j <= N; j += i)
{
isPrime[j] = false;
}
}
}
int size0 = primes.size();
while (true)
{
long long l = 0, u = 0;
fin >> l >> u;
if (l == 0) break;
vector<bool> isPrimeNow(u - l + 1, true);
for (int i = 0; i <= size0 - 1; ++i)
{
int b = l / primes[i];
while (b <= 1 || primes[i] * b < l)
{
++b;
}
for (long long j = primes[i] * b; j <= u; j += primes[i])
{
if (j >= l)
{
isPrimeNow[j - l] = false;
}
}
}
if (l == 1)
{
isPrimeNow[0] = false;
}
long long lastPrime = -1;
long long _min = INTMAX, minL, minR;
long long _max = -1 * INTMAX, maxL, maxR;
for (long long i = l; i <= u; ++i)
{
if (isPrimeNow[i - l] == true)
{
if (lastPrime != -1)
{
if (i - lastPrime < _min)
{
_min = i - lastPrime;
minL = lastPrime;
minR = i;
}
if (i - lastPrime > _max)
{
_max = i - lastPrime;
maxL = lastPrime;
maxR = i;
}
}
lastPrime = i;
}
}
if (_min == INTMAX)
{
fout << "There are no adjacent primes.\n";
}
else
{
fout << minL << ',' << minR << " are closest, " << maxL << ',' << maxR << " are most distant.\n";
}
}
return 0;
}