首页 > 代码库 > POJ-2689 Prime Distance(线性筛法)

POJ-2689 Prime Distance(线性筛法)

Prime Distance
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17021   Accepted: 4536

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.

Source

Waterloo local 1998.10.17
 
个人认为,线性筛法的精髓之处在于:比一个合数数大的质数和该合数的乘积可用一个更大的合数和比其小的质数相乘得到。
技术分享
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #define N 50000
 5 #define len 1000000
 6 #define inf 0x7fffffff
 7 using namespace std;
 8 typedef long long LL;
 9 bool isprime[N+5];
10 LL prime[N],cnt;
11 bool res[len+5];
12 void init(){
13     LL i,j;
14     cnt=0;
15     memset(isprime,true,sizeof(isprime));
16     for (i=2;i<=N;i++){
17         if (isprime[i]) prime[cnt++]=i;
18         for (j=0;j<cnt && i*prime[j]<=N;j++){
19             isprime[i*prime[j]]=false;
20             if (i%prime[j]==0) break;
21         }
22     }
23 }
24 int main(){
25     freopen ("prime.in","r",stdin);
26     freopen ("prime.out","w",stdout);
27     LL L,U,i,j,k,min,max,s,t;
28     init();
29     while(scanf("%lld%lld",&L,&U)!=EOF){
30         memset(res,0,sizeof(res));
31         for(i=0;i<cnt;i++){
32             s=(L-1)/prime[i]+1;
33             t=U/prime[i];
34             for(j=s;j<=t;j++)
35                 if(j>1)
36                     res[j*prime[i]-L]=true;
37         }
38         k=-1,min=inf,max=-1;
39         LL dis,m1,m2;
40         for(i=0;i<=U-L;i++){
41             if(!res[i]){
42                 if(k!=-1){
43                     dis=i-k;
44                     if(dis>max){
45                         max=dis;
46                         m1=i;
47                     }
48                     if(dis<min){
49                         min=dis;
50                         m2=i;
51                     }
52                 }
53                 if(i+L!=1)//注意 1 的时候特殊判断 1 不是素数
54                     k=i;
55             }
56         }
57         if(max==-1)
58             printf("There are no adjacent primes.\n");
59         else{
60             printf("%lld,%lld are closest, %lld,%lld are most distant.\n",m2-
61             min+L,m2+L,m1-max+L,m1+L);
62         }
63     }
64     return 0;
65 }
喵~

 

POJ-2689 Prime Distance(线性筛法)