POJ2689 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.
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 1000005;
bool is_prime[maxn];
bool is_prime_small[maxn];
ll prime[maxn];
ll prime_num=0;

//对区间[a,b)内的整数执行筛法,is_prime[i-a]=true  ---  表示i是素数 注意这里下标偏移了a,所以从0开始。
void segment_sieve(ll a,ll b) {
    for(ll i=0;i*i<=b;++i) is_prime_small[i]=true; //对[2,sqrt(b))的初始化全为质数
    for(ll i=0;i<=b-a;++i) is_prime[i]=true; //对下标偏移后的[a,b)进行初始化

    for(ll i=2;i*i<=b;++i) {
        if(is_prime_small[i]) {
            for(ll j=2*i;j*j<=b;j+=i) is_prime_small[j]=false;  //筛选[2,sqrt(b));
            //(a+i-1)/i得到最接近a的i的倍数,最低是i的2倍,然后筛选
            for(ll j=max(2LL,(a+i-1)/i)*i;j<=b;j+=i) is_prime[j-a]=false;
        }
    }
    for(ll i=0;i<=b-a;++i)  //统计个数
        if(is_prime[i]) prime[prime_num++]=i+a;
}

int main()
{
    ll a,b,ans1,ans2,ans3,ans4,dis1,dis2;
    while(~scanf("%lld%lld",&a,&b))
    {
        if(a==1)
            a++;
        prime_num=0;
        dis1 = 0;
        dis2 =  1000010;
        memset(prime,0,sizeof(prime));
        segment_sieve(a,b);
        for(ll i=1;i<prime_num;++i)
        {
            //printf("%d",prime[i]);
            if(prime[i]-prime[i-1]>dis1)
            {
                dis1 = prime[i]-prime[i-1];
                ans1 = prime[i-1];
                ans2 = prime[i];
            }
            if(prime[i]-prime[i-1]<dis2)
            {
                dis2 = prime[i]-prime[i-1];
                ans3 = prime[i-1];
                ans4 = prime[i];
            }
        }
        //cout<<prime_num<<endl;
        if(dis1==0)
            printf("There are no adjacent primes.\n");
        else
            printf("%lld,%lld are closest, %lld,%lld are most distant.\n",ans3,ans4,ans1,ans2);
        //printf("%lld\n",prime_num);
    }
    return 0;
}

 

posted @ 2018-01-25 12:13  TTTCoder  阅读(231)  评论(0编辑  收藏  举报