首页 > 代码库 > POJ 2689 二次筛选(映射)

POJ 2689 二次筛选(映射)

Prime Distance
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12303 Accepted: 3296

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 1714 17

Sample Output

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




 
题意:
在区间[l,r](1<=l<r<=2,147,483,647)内找最近素数对和最远素数对。(r-l<=1,000,000)
 
 
思路:
由于l,r可能很大,没法用数组存储,所以普通的筛选素数算法已经没法直接把[l,r]中的素数筛选出来了,但是我们发现r-l才不过100万,所以咱们会想到若把[l,r]映射到[0,r-l]上不就行了么,由于int范围内的合数最小因数是50000以下,那么咱们可以把[2,50000]中的素数筛选出来放到数组prime[]中,然后设a=l/prime[i], b=r/prime[i], 那么for(j=a;j<=b)   就可以把j*prime[i]筛选出来即把[l,r]中的合数筛选出来了,那么答案也就出来了。
 
 
代码:
 1 #include <cstdio> 2 #include <algorithm> 3 #include <iostream> 4 #include <cstring> 5 using namespace std; 6  7 int p[500005]; 8 int vis[500005]; 9 int d[1000005];10 int b[1000005];11 int l, r, tot, ans, min1, min2, max1, max2;12 13 void get_prime(){                    //第一次筛选,把[2,50000]中的素数筛选出来,nlogn筛选法 14     int i, j;15     memset(vis,0,sizeof(vis));16     tot=0;17     for(i=2;i<50005;i++){18         if(!vis[i]) p[tot++]=i;19         for(j=0;j<tot&&p[j]*i<50005;j++){20             vis[p[j]*i]=1;21             if(i%p[j]==0) break;22         }23     }24 }25 26 void solve(){              //第二次筛选,把[l,r]区间映射到[0,l-r] 27     int i, j, k;28     memset(d,0,sizeof(d));29     for(i=0;i<tot;i++){30         if(l==1) l++;31         int x=l/p[i];32         if(x==1) x++;33         if(x==0) x+=2;34         j=x;35         for(;j<=r/p[i];j++){36             d[j*p[i]-l]=1;37         }38     }39     int flag=0;k=0;40     for(i=0;i<=r-l;i++){41         if(!d[i]) {42             b[k++]=i+l;43             ans++;44         }45     }46     sort(b,b+k);47     48     int MIN=1999999999, MAX=-1999999999;49     for(i=1;i<k;i++){50         if(b[i]-b[i-1]<MIN){51             min1=b[i-1];min2=b[i];MIN=b[i]-b[i-1];52         }53         if(b[i]-b[i-1]>MAX){54             max1=b[i-1];max2=b[i];MAX=b[i]-b[i-1];55         }56     }57 }58 main()59 {60     get_prime();61     while(scanf("%d %d",&l,&r)==2){62         ans=0;63         64         solve();65        if(ans<=1){66            printf("There are no adjacent primes.\n");continue;67        }68         printf("%d,%d are closest, %d,%d are most distant.\n",min1,min2,max1,max2);69     }70 }