1. 程式人生 > >POJ 2689 Prime Distance

POJ 2689 Prime Distance

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,統計這個區間的素數就行,先找到這個區間所有的數的最小素因子,即用尤拉篩法篩到 就行了,再用得到的素數加上埃氏篩法篩出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; // 標記該區間不是素數的數,壓縮空間   
    }
    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