poj 2689 Prime Distance 区间素数筛+大数 模板

Prime Distance

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22355   Accepted: 5936

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.

题意:输入区间[l,r],1<=r-l<=1000000。求出[l,r]中,相邻素数差的最大和最小的素数对。存在多个时,输出较小的。

做法:

        l,r范围太大,不能纯用素数筛暴力,但区间间隔比较小。对于int范围内的合数来说,最小质因子必定小于2^16。所以可用起点比较高的素数筛筛掉合数,从而得到素数,然后即可暴力枚举。

        因合数的最小质因子小于2^16(50000)。可先求出小于50000的所有素数(代码中我稍微扩大了范围)。则区间中的合数,必定可以表示为小于50000的素数的倍数。由于l,r过大,不能直接用数组。因为r-l<=1000000所以把所有的数都减去l,则数组大小变为[0,r-l]即可。

        敲重点!!!1要被特殊处理。因其不是小于50000的素数的倍数,所以在与合数相斥中,会被当成素数。


代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1000005;
const int N=100005;
ll prime[N],numprime,ans[maxn],numans,l,r;
bool vis[N],temp[maxn];
void init(){
    vis[1]=1;
    for(int i=2;i<=N;i++){
        if(!vis[i]){
            prime[numprime++]=i;
            for(int j=2*i;j<=N;j+=i)
                vis[j]=1;
        }
    }
}
void get_prime(){
    for(ll i=0;i<numprime;i++){
        ll b=l/prime[i];
        while(b<=1) b++;
        for(ll j=b*prime[i];j<=r;j+=prime[i])
            if(l<=j)
                temp[j-l]=1;
    }
    if(l==1) temp[0]=1;
}
int main(){
    init();
    while(~scanf("%lld%lld",&l,&r)){
        memset(temp,0,sizeof(temp));
        memset(ans,0,sizeof(ans));
        numans=0;
        get_prime();
        for(int i=0;i<=r-l;i++)
            if(!temp[i])
                ans[numans++]=l+i;
        if(numans<=1){
            printf("There are no adjacent primes.\n");
            continue;
        }
        ll mincha=1e9,maxcha=-1e9,minf,mins,maxf,maxs;
        for(int i=1;i<numans;i++){
            if((ans[i]-ans[i-1])>maxcha){
                maxcha=ans[i]-ans[i-1];
                maxf=ans[i],maxs=ans[i-1];
            }
            if(ans[i]-ans[i-1]<mincha){
                mincha=ans[i]-ans[i-1];
                minf=ans[i],mins=ans[i-1];
            }
        }
        printf("%lld,%lld are closest, %lld,%lld are most distant.\n",mins,minf,maxs,maxf);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41955236/article/details/81603700
今日推荐