题解
区间筛质数模板题。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
using namespace std;
int read(){
int f = 1, x = 0;
char c = getchar();
while(c < '0' || c > '9'){if(c == '-') f = -f; c = getchar(); }
while(c >= '0' && c <= '9')x = x * 10 + c - '0', c = getchar();
return f * x;
}
unsigned int L, R, prime[35005], cnt = 0;
bool vis1[70005] = {0}, vis2[1000005] = {0};
void getP(){
vis1[1] = true;
for(int i = 2; i <= 65536; ++i)
if(!vis1[i]){
for(int j = i + i; j <= 65536; j += i)
vis1[j] = true;
prime[++cnt] = i;
}
}
void init(){
if(L <= 1) L = 2;
for(int i = 1; i <= cnt; ++i){
unsigned int p = prime[i];
if(R < p) break;
for(unsigned int j = max((L - 1 + p) / p, 2u); j <= R / p; ++j)
vis2[j * p - L] = 1;
}
}
void solve(){
unsigned int lst = 0, mind = 1000001, maxd = 0;
pair<int, int> ans1, ans2;
for(unsigned int i = L; i <= (unsigned int)R; ++i){
if(!vis2[i - L]){
if(lst > 0){
if(i - lst < mind){
mind = i - lst;
ans1.first = lst, ans1.second = i;
}
if(i - lst > maxd){
maxd = i - lst;
ans2.first = lst, ans2.second = i;
}
}
lst = i;
}
}
if(!maxd) printf("There are no adjacent primes.\n");
else printf("%d,%d are closest, %d,%d are most distant.\n", ans1.first, ans1.second, ans2.first, ans2.second);
memset(vis2, 0, sizeof(vis2));
}
int main(){
getP();
while(scanf("%d%d", &L, &R) == 2){
init();
solve();
}
return 0;
}