Poj2689 Prime Distance

Poj2689 Prime Distance

Description

The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no proper factors (it is only evenly divisible by 1 and itself). The first prime numbers are 2,3,5,7 but they quickly become less frequent. One of the interesting questions is how dense they are in various ranges. Adjacent primes are two numbers that are both primes, but there are no other prime numbers between the adjacent primes. For example, 2,3 are the only adjacent primes that are also adjacent numbers.
Your program is given 2 numbers: L and U (1<=L< U<=2,147,483,647), and you are to find the two adjacent primes C1 and C2 (L<=C1< C2<=U) that are closest (i.e. C2-C1 is the minimum). If there are other pairs that are the same distance apart, use the first pair. You are also to find the two adjacent primes D1 and D2 (L<=D1< D2<=U) where D1 and D2 are as distant from each other as possible (again choosing the first pair if there is a tie).

Input

Each line of input will contain two positive integers, L and U, with L < U. The difference between L and U will not exceed 1,000,000.

Output

For each L and U, the output will either be the statement that there are no adjacent primes (because there are less than two primes between the two given numbers) or a line giving the two pairs of adjacent primes.

Sample Input

2 17
14 17

Sample Output

2,3 are closest, 7,11 are most distant.
There are no adjacent primes.

sol:

       由于数据范围很大,无法生成$[1,N]$的所有素数

       使用筛法求出$[2,\sqrt{N}]$之间的所有素数,对于每个素数$p$,把$[L,R]$中能被$p$整除的数标记,即标记$i \times p(\lceil \frac{L}{p} \rceil \leq i \leq \lfloor \frac{R}{p} \rfloor)$为合数。

       将筛出的素数进行两两比较,找出差最大的即可。

code:

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const ll N=1e6+5;
 5 ll l,r,p[N],tot,f[N],v[N];
 6 void prim() {
 7     for(ll i=2; i<N; i++) {
 8         if(!f[i]) p[++tot]=i;
 9         for(ll j=1; j<=tot&&i*p[j]<N; j++) {
10             f[i*p[j]]=1;
11             if(i%p[j]==0) break;
12         }
13     }
14 }
15 void mn(ll &x,ll l,ll r,ll &p,ll &q) {
16     if(r-l<x)
17         p=l,q=r,x=r-l;
18 }
19 void mx(ll &x,ll l,ll r,ll &p,ll &q) {
20     if(r-l>x)
21         p=l,q=r,x=r-l;
22 }
23 int main() {
24     prim();
25     while(~scanf("%lld%lld",&l,&r)) {
26         memset(v,1,sizeof(v));
27         if(l==1) v[0]=0;
28         for(ll i=1; i<=tot; i++) {
29             for(ll j=l/p[i]; j*p[i]<=r; j++) {
30                 ll x=p[i]*j;
31                 if(j>1&&x>=l) v[x-l]=0;
32             }
33         }
34         ll t=0,a,b,c,d,minn=1LL<<60,maxx=0;
35         for(ll i=l; i<=r; i++) {
36             if(!v[i-l]) continue;
37             if(t) mn(minn,t,i,a,b),mx(maxx,t,i,c,d);
38             t=i;
39         }
40         if(!maxx)
41             puts("There are no adjacent primes.");
42         else
43             printf("%lld,%lld are closest, %lld,%lld are most distant.\n",a,b,c,d);
44     }
45     return 0;
46 }

 

posted @ 2021-02-06 13:34  VanderKingStudy  阅读(66)  评论(0编辑  收藏  举报