质数距离(筛素数)

题目描述

给定两个整数 L 和 U,你需要在闭区间 [L,U] 内找到距离最接近的两个相邻质数 C1 和 C2(即 C2−C1 是最小的),如果存在相同距离的其他相邻质数对,则输出第一对。
同时,你还需要找到距离最远的两个相邻质数 D1 和 D2(即 D1−D2 是最大的),如果存在相同距离的其他相邻质数对,则输出第一对。

输入格式

每行输入两个整数 L 和 U,其中 L 和 U 的差值不会超过 106。

输出格式

对于每个 L 和 U,输出一个结果,结果占一行。
结果包括距离最近的相邻质数对和距离最远的相邻质数对。(具体格式参照样例)
如果 L 和 U 之间不存在质数对,则输出 There are no adjacent primes.。

数据范围

1≤L<U≤231−1

输入样例
2 17
14 17
输出样例
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.

题目分析

本题中区间给定的范围是[1,231-1],很明显没法直接用筛法求出答案。因此我们需要去挖掘本题中的某些性质,从而求出答案:

  • 性质1: 若一个数 n n n 是一个合数,必然存在2个因子 d , n / d d,n/d d,n/d,假设 d < = n / d d <= n/d d<=n/d,则 d < = n d <= \sqrt n d<=n ,因此这一对因子中必然存在一个小于等于 n \sqrt n n 的因子
  • 性质2: 若 x ∈ [ L , R ] x∈[L,R] x[L,R],且x是合数,则一定存在 P < = 2 31 − 1 ( < 50000 ) P <= \sqrt {2^{31}−1}(< 50000) P<=2311 (<50000),使得P能整除x,其中P < x.

有了这些性质,我们就可以以此来推出这个题的解法了:

  1. 用筛法求出 [ 1 , 2 31 − 1 ( 50000 ) ] [1,\sqrt {2^{31}−1} (50000)] [1,2311 (50000)]区间内的素数。
  2. 对于 1 − 50000 1-50000 150000内的每一个素数p,将区间 [ L , R ] [L,R] [L,R]内所有p的倍数全部筛掉。经过了这样的筛选之后,区间 [ L , R ] [L,R] [L,R]内剩下的数就都是素数了。
  3. 补充:要筛掉 [ L , R ] [L,R] [L,R]内所有p的倍数,我们只需要找到大于等于L的p的最小倍数( ( L + p − 1 ) / p ∗ p (L+p-1)/p*p (L+p1)/pp ),之后每次+p即可。
代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <algorithm>
#define LL long long
#define ULL unsigned long long
#define PII pair<int,int>
#define x first
#define y second
using namespace std;
const int N=1e6+5,INF=0x3f3f3f3f;
int prime[N],cnt;
bool st[N];
void init(int n)				//线筛求素数
{
    cnt=0;
    memset(st,0,sizeof st);
    for(int i=2;i<=n;i++)
    {
        if(!st[i]) prime[cnt++]=i;
        for(int j=0;prime[j]*i<=n;j++)
        {
            st[prime[j]*i]=true;
            if(i%prime[j]==0) break;
        }
    }
}
int main()
{
    int l,r;
    while(cin>>l>>r)				//有多组测试样例,因此要用while读入
    {
        init(50000);				//初始化
        memset(st,0,sizeof st);		//因为后面我们会复用st[]和prime[]数组,因此这里要重新清空st[]
        for(int i=0;i<cnt;i++)
        {
            LL p=prime[i];					//筛掉[l,r]区间内所有p的倍数(最小要从2开始)
            for(LL j=max(2*p,(l+p-1)/p*p);j<=r;j+=p)
                st[j-l]=true;
        }
        cnt=0;
        for(int i=0;i<=r-l;i++)				//将剩下的没被筛掉的数放入存入prime[]中(注意要把1特判掉)
            if(!st[i]&&i+l>1) prime[cnt++]=i+l;
        
        if(cnt<2) puts("There are no adjacent primes.");
        else {
            int maxp=1,minp=1;
            for(int i=2;i<cnt;i++)				//枚举区间内所有的素数,记录距离的最小值与最大值
            {
                int d=prime[i]-prime[i-1];
                if(prime[maxp]-prime[maxp-1]<d) maxp=i;
                if(prime[minp]-prime[minp-1]>d) minp=i;
            }
            printf("%d,%d are closest, %d,%d are most distant.\n",
                prime[minp-1],prime[minp],prime[maxp-1],prime[maxp]);
        }
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwz_159

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值