POJ 2689(区间素数筛)

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.

思路

题目给定一个区间(长度1e6) 大小为int类型数字

让你计算出这个区间内相邻质数差最大的和最小的是哪两对

由于是int (2^31-1)

处理出1~ 216 2 16 的质数用于筛给定区间是够用的

复杂度是 216/ln(216)log() 区 间 长 2 16 / l n ( 2 16 ) ∗ l o g ( 区 间 长 )

这里给一些数据

1 1

There are no adjacent primes.

1 2

There are no adjacent primes.

2141342543 2141999999

2141342867,2141342869 are closest, 2141982517,2141982683 are most distant.




代码示例

#include<iostream>
#include<vector>
using namespace std;

const int maxn=(1<<16)+10;
bool valid[maxn];
int ans[maxn];

int tot;

void get_prime(int n)
{
    tot=0;
    for(int i=2;i<=n;++i) valid[i]=true;

    for(int i=2;i<=n;++i){
        if(valid[i]){
        ans[++tot]=i;
        }
        for(int j=1;j<=tot &&  ans[j]*i<=n;++j){
            valid[ans[j]*i]=false;
            if(i%ans[j]==0) break;
        }
    }
}

bool vis[maxn<<4];//用于被筛

int main()
{
    ios::sync_with_stdio(false);
    get_prime(1<<16);
    int L,R;
    while(cin>>L>>R)
    {
        for(int i=1;i<=(R-L+1);++i){//-L+1编号
            vis[i]=true;
        }
        if(L==1) vis[1]=false;//1不是素数
        //下面用素数筛


        for(int i=1;i<=tot;i++)
            for(int j=max(ans[i],(L-1)/ans[i]+1);j<=R/ans[i];j++)//当前素数的j倍
                vis[ans[i]*j-L+1]=false;//ans[i]*j是合数  

//(L-1)/ans[i]+1)找的是从L开始第一个ans[i]的倍数是其多少倍

//也可用下面这种写法 但要unsigned int    慢多少?
//        for(int i=1;i<=tot;++i){
//            for(unsigned int j=(L%ans[i]==0?L:L+ans[i]-L%ans[i]);j<=R;j+=ans[i]){
//                if(j==ans[i]) continue;//自己是质数 
//                vis[j-L+1]=false;//j是合数
//            }
//        }

//考虑这里为什么当L较小时 从ans[i]倍ans[i]开始筛 (当L较大时自然从(L-1)/ans[i]+1倍开始)
//能从ans[i]*ans[i]开始的条件是 ans[i]的ans[i-1]倍、ans[i-2]倍...已经筛过了(如果在L,R区间里)
//那当i=i-1、i-2...时不就是吗 证毕
//也许会问 
//1.如果ans[k],k<i 小于 (L-1)/ans[k]+1呢? 辣就从  (L-1)/ans[k]+1倍开始 这样才在L,R区间内
//2.如果ans[k],k<i 大于 (L-1)/ans[k]+1呢? 辣就从  ans[k]开始,上面讨论的ans[i](i>k)的ans[k]倍就是在这筛去的,即ans[k]的ans[i]倍  

        int num=0;
        for(int i=1;i<=(R-L+1);++i){
            if(vis[i]) num++;
        }
        //cout<<num<<endl;
        if(num>=2){
            vector<int> tep;
            for(int i=1;i<=(R-L+1);++i){
                if(vis[i]) tep.push_back(L+i-1);
            }
            int ans1=0,ans2=0;
            int maxx=tep[1]-tep[0],minn=tep[1]-tep[0];
            for(int i=2;i<tep.size();++i){
                if(tep[i]-tep[i-1]>maxx) ans2=i-1,maxx=tep[i]-tep[i-1];
                if(tep[i]-tep[i-1]<minn) ans1=i-1,minn=tep[i]-tep[i-1];
            }
            cout<<tep[ans1]<<','<<tep[ans1+1]<<" are closest, "<<tep[ans2]<<','<<tep[ans2+1]<<" are most distant."<<endl;
        }
        else{
            cout<<"There are no adjacent primes."<<endl;
        }
    }
    return 0;
}
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值