POJ 2689 Prime Distance 解题报告(素数筛)

来源:互联网 发布:备案域名30出售 编辑:程序博客网 时间:2024/05/04 23:39
Prime Distance
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11435 Accepted: 3064

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间最近的素数对,最远的素数对。U-L<=1000000。
    仍然可以用筛素数的办法。首先,如果L之U间有一个数十约数,那么必有一个[2,sqrt(2147483647)]间的素因子。
    枚举所有素因子,筛出约数,再求出最近和最远的素数对即可。
    有几个注意的地方:1不是素数,不要超过int的范围。
    代码如下:
#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>using namespace std;typedef long long LL;const int maxn = 1000100;bool vis[maxn];bool h[50000];int prime[50000];int primeNum;void calPrime(){    primeNum=0;    for(int i=2;i<=46340;i++) if(!h[i])    {        prime[primeNum++] = i;        for(int j=i+i;j<=46340;j+=i)            h[j]=true;    }}void work(int l, int r){    memset(vis, 0, sizeof(vis));    if(l==1) l++;    for(int i=0;i<primeNum;i++)    {        int pri = prime[i];        if(pri>=r) break;        LL sta=l;        if(sta%pri!=0)            sta+=pri-sta%pri;        if(sta==pri) sta+=pri;        for(;sta<=r;sta+=pri)            vis[sta-l]=true;    }    int mi=maxn, ma=0;    int mista, miend;    int masta, maend;    int last=-1;    for(LL i=l;i<=r;i++) if(vis[i-l]==false)    {        if(last!=-1)        {            int dis=i-last;            if(mi>dis)            {                mi=dis;                mista=last;                miend=i;            }            if(ma<dis)            {                ma=dis;                masta=last;                maend=i;            }        }        last=i;    }    if(ma==0)    {        puts("There are no adjacent primes.");    }    else    {        printf("%d,%d are closest, %d,%d are most distant.\n", mista, miend, masta, maend);    }}int main(){    calPrime();    int l, r;    while(~scanf("%d%d", &l, &r))        work(l, r);}


0 0