POJ 2689-Prime Distance(区间素数)

  • Post author:
  • Post category:其他



Prime Distance


Time Limit:

1000MS

Memory Limit:

65536K

Total Submissions:

15901

Accepted:

4227

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

Sample Output

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

Source


Waterloo local 1998.10.17


题目意思:


求给定两个数区间内的所有素数中,相邻两个相差值最大和最小的数并输出;

如果没有就输出There are no adjacent primes.


解题思路:


打素数表筛选出两个数区间内的所有素数,再两两比较求出最值。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define MAXN 1000005
#define INF 0xfffffff
using namespace std;
typedef long long ll;
bool is_prime[MAXN];
bool is_prime_small[MAXN];
ll a,b;
void segment_sieve()//筛选a~b区间内的素数,is_prime[i-a]=true时,i是素数
{
    for(ll i=0; i*i<=b; ++i)
        is_prime_small[i]=true;
    for(ll i=0; i<=b-a; ++i)
        is_prime[i]=true;
    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)
            for(ll j=max(2LL,(a+i-1)/i)*i; j<=b; j+=i)
                is_prime[j-a]=false;//a~b
        }
    }
}
ll cnt,prime[MAXN];//a~b区间内素数的个数和素数表
void getnum()
{
    for(ll i=a; i<=b; ++i)
        if(is_prime[i-a])
            if(i!=1)
                prime[cnt++]=i;
    //for(ll i=0; i<=cnt; ++i)
        //cout<<prime[i]<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(cin>>a>>b)
    {
        memset(is_prime,false,sizeof(is_prime));
        memset(is_prime_small,false,sizeof(is_prime_small));
        memset(prime,0,sizeof(prime));
        cnt=0;
        segment_sieve();
        getnum();
        ll x=0,xx=0,y=0,yy=0,Max=-1,Min=INF;
        for(ll i=1; i<cnt; ++i)
        {
            if((prime[i]-prime[i-1])<Min)
            {
                x=prime[i-1],xx=prime[i];
                Min=xx-x;
            }
            if((prime[i]-prime[i-1])>Max)
            {
                y=prime[i-1],yy=prime[i];
                Max=yy-y;
            }
        }
        if(cnt==0||cnt==1)
            cout<<"There are no adjacent primes."<<endl;
        else
            cout<<x<<","<<xx<<" are closest, "<<y<<","<<yy<<" are most distant."<<endl;
    }
    return 0;
}
/*
1 5
2 17
14 17
22 37
22801763 22801787
2147483000 2147483647
*/



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