POJ 2689 Prime Distance

Prime Distance
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18943 Accepted: 5072

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


题意:给一个区间[m,n],数据范围很大(n-m<=1e6),求区间相邻素数最大差值与最小差值
与之前写的一题类似,这里再写一遍,加深一下理解
题目数据范围小于2^31,素数打表到1e6就行,再枚举区间内的素数即可

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<cmath>
#include<vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;

#define pi acos(-1.0)
#define eps 1e-10
#define pf printf
#define sf scanf
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
#define e tree[rt]
#define _s second
#define _f first
#define all(x) (x).begin,(x).end
#define mem(i,a) memset(i,a,sizeof i)
#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)
#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)
#define mi ((l+r)>>1)
#define sqr(x) ((x)*(x))

const int inf=1e7;
const int Max=1e6+1;
int p[Max/5],pos[Max+1],a;
bool vis[Max+1],pr[Max+1];
ll m,n;

void prime()
{
    mem(vis,0);
    p[0]=0;
    for(int i=2;i<=Max;i++)
    {
        if(!vis[i])p[++p[0]]=i;
        for(int j=1;j<=p[0]&&(ll)i*p[j]<=Max;j++)
        {
            vis[i*p[j]]=1;
            if(!(i%p[j]))break;
        }
    }
}

void solve()
{
    a=0;
    for(ll i=1;i<=p[0]&&p[i]<=n;i++)
    {
        ll u=m/p[i];
        if(m>p[i]*u)u++;//找到第一个大于等于m并能整除p[i]的数x,x+p[i]仍然不是素数
        if(u==1)u++;//枚举时如果u为1,p[i]*1仍是素数,所以应该u++
        for(ll j=u*p[i];j<=n;j+=p[i])
        {
            pr[j-m]=1;
        }
    }
    for(ll i=0;i<=n-m;i++)
        if(!pr[i])pos[++a]=i;//预处理一下,将素数区间整理一下
}

int main()
{
    prime();
    ll x,y,x1,y1;
    while(~sf("%lld%lld",&m,&n))
    {
        if(m==1)m=2;
        mem(pr,0);
        solve();
        int q=inf,o=0;
        for(ll i=1;i<a;i++)//直接枚举最大最小长度
        {
            int z=pos[i+1]-pos[i];
            if(z>o)
                x=pos[i],y=pos[i+1],o=z;
            if(z<q)
                x1=pos[i],y1=pos[i+1],q=z;
        }
        if(!o)puts("There are no adjacent primes.");
        else pf("%lld,%lld are closest, %lld,%lld are most distant.\n",x1+m,y1+m,x+m,y+m);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值