POJ 2689:Prime Distance

来源:互联网 发布:淘宝网姓名贴 编辑:程序博客网 时间:2024/06/13 13:26

Prime Distance
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 19346 Accepted: 5197

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,U虽然很大,但L-U<=1e6,这就是在提示我们。该如何求出这个区间的素数呢?可以想到这个区间里的非素数的素数因子最大不会超过1e6,不然就会1e6*1e6超过U的上限,那么那些非素数的数的因子都小于等于1e6。那么先求出1-1e6之间的素数,然后用这些素数作为因子去删掉[L,U]之间的合数。剩下来的就是素数。

#include<iostream>#include<algorithm>#include<vector>#include<cstring>#include<cstdio>using namespace std;const long long MAX_N = 2e6;long long all=0;long long pr[MAX_N/10+100];        //素数表bool isp[MAX_N+10];void init(){all = 0;memset(isp,0,sizeof isp);for(long long i=2;i<=MAX_N;i++){if(!isp[i])pr[all++] = i;for(long long j=0;j<all;j++){         long long t = 1LL*pr[j]*i ;                if(t<=MAX_N)                {                isp[t] = true;                if(i%pr[j]==0)break;                }                else break;        }    }return;}long long a[MAX_N];int main(){    init();    long long l,r;    while(cin>>l>>r)    {        memset(a,1,sizeof a);         //用a[i]标记i是否为素数        for(long long i=0;i<all;i++)  //用素数来删[l,r]之间的合数        {            if(pr[i]>r)break;            long long st;            //挑选出第一个能整除pr[i]的合数            if(l>=pr[i])            {                if(l%pr[i]==0)                {                    if(l>pr[i])st=l;                    else st=2*l;                }                else st=l+pr[i]-l%pr[i];            }            else st=pr[i]*2;            for(;st<=r;st+=pr[i])a[st-l]=0;        }        vector<long long>ans;       //储存素数        for(long long i=0;i<=r-l;i++)if(a[i]&&l+i!=1)ans.push_back(l+i);   //这里忘加了一个l+i!=1,结果把1也放进去,WA了好几发。。        sort(ans.begin(),ans.end());        if(ans.size()<2){puts("There are no adjacent primes.");continue;}        long long dis=1e15;        long long x,y;        for(long long i=1;i<ans.size();i++)    //求最小距离的素数对        {            if(dis>ans[i]-ans[i-1])            {                dis=ans[i]-ans[i-1];                x=ans[i-1],y=ans[i];            }        }        printf("%lld,%lld are closest, ",x,y);        dis=0;        for(long long i=1;i<ans.size();i++)   //求最大距离的素数对        {            if(dis<ans[i]-ans[i-1])            {                dis=ans[i]-ans[i-1];                x=ans[i-1],y=ans[i];            }        }        printf("%lld,%lld are most distant.\n",x,y);    }    return 0;}




原创粉丝点击