POJ 2689 Prime Distance

  • Post author:
  • Post category:其他


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.

题意

在L~R(含端点)的区间内找出距离最近的一对素数和距离最远的一对素数

思路

数据量比较大,常规筛法解决不了。优化一下。 题目保证R-L<=1000000,统计这个区间的素数就行,先找到这个区间所有的数的最小素因子,即用欧拉筛法筛到sqrt(2^31)就行了,再用得到的素数加上埃氏筛法筛出L~R区间内的素数。之后对该区间的素数扫描一遍,记录距离最近的一对素数和距离最远的一对素数。

#include<cstdio>
#include<cstring>
#define N 50000
#define DIS 1000005
#define INF 0x3f3f3f3f
using namespace std;
typedef long long int LL;
int prime[N];
int prime2[DIS];
int isnotprime[DIS];
void EulerSeiv()   //欧拉筛法
{
    for(int i=2; i<N; i++)   
    {
        if(!prime[i])
            prime[++prime[0]]=i;   //prime[0]记录该数组有多少个数
        for(int j=1; j<=prime[0]&&i*prime[j]<N; j++)
        {
            prime[i*prime[j]]=true;   
            if(i%prime[j]==0)
                break;
        }
    }
}
void GetPrime(int L,int R)
{
    memset(isnotprime,0,sizeof(isnotprime));  // 标记数组
    if(L<2)
        L=2;
    for(int i=1; i<prime[0]&&(LL)prime[i]*prime[i]<=R; i++) // 加long long 防溢出
    {
        int s=L/prime[i]+(L%prime[i]!=0);      //向上取整
        if(s==1)     
            s=2;                               // 防止把prime数组中的数筛掉
        for(int j=s; (LL)j*prime[i]<=R; j++)
            if((LL)j*prime[i]>=L)              // 防止下标越界
                isnotprime[j*prime[i]-L]=true; // 标记该区间不是素数的数,压缩空间,或者理解为偏移处理,偏移量为a   
    }
    prime2[0]=0;                               
    for(int j=0; j<=R-L; j++)                  // 统计该区间的素数
        if(!isnotprime[j])
            prime2[++prime2[0]]=j+L;           
}

int main()
{
    int L,R;
    EulerSeiv();
    while(scanf("%d%d",&L,&R)!=EOF)
    {
        GetPrime(L,R);
        int Lmax=0,Rmax=0,Lmin=0,Rmin=INF,dis;
        if(prime2[0]<2)
            puts("There are no adjacent primes.");
        else
        {
            for(int i=2; i<=prime2[0]; i++)
            {
                dis=prime2[i]-prime2[i-1];
                if(Rmin-Lmin>dis)
                {
                    Rmin=prime2[i];
                    Lmin=prime2[i-1];
                }
                if(Rmax-Lmax<dis)
                {
                    Rmax=prime2[i];
                    Lmax=prime2[i-1];
                }
            }
            printf("%d,%d are closest, %d,%d are most distant.\n",Lmin,Rmin,Lmax,Rmax);
        }
    }
    return 0;
}

摘抄于该大佬的博客:

here



版权声明:本文为codertcm原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。