题意很简单就是让你求给定区间的素数,然后用一个循环求出相距最远的相邻素数数和最近的素数以及相距最近的相邻素数
难点在与数据很大,所以不可能直接对区间的每一个数进行素数判断。但是,每个合数n都至少有一个因数在2到根号n(以此来筛去该合数),同时这其中U的最大值为2的31次方,对其开方得46000+,这个数据的大小在可接受范围内,所以可以用2到根号U之间的素数来筛去L到U区间的合数。
#include<iostream>
#include<cstring>
#include<utility>
#include<cmath>
#define N 1000000
#define M 50000
using namespace std;
int primes[M];
bool judge[M];
bool p[N];
int b[N];
int cnt;
void getPrimes(int n);
int main()
{
    int L,U;
    while(cin>>L>>U)
    {
        getPrimes(sqrt(U));
        memset(p,true,sizeof p);
        if(L==1)
            p[0]=false;
        for(int i=0;i<cnt;i++)
            for(int j=ceil(L/primes[i]);j<=floor(U/primes[i]);j++)
                if(j!=1)
                    p[primes[i]*j-L]=false;
        pair<int,int> p1,p2;
        int cnt2=0;
        for(int i=0;i<U-L+1;i++)
            if(p[i])
            b[cnt2++]=(L+i);
        if(cnt2<2)
            cout<<"There are no adjacent primes."<<endl;
        else
        {
          int max_nu=0,min_nu=10000;
          for(int i=0;i<cnt2-1;i++)
          {
              if(b[i+1]-b[i]>max_nu)
                {
                    max_nu=b[i+1]-b[i];
                    p1.first=b[i];
                    p1.second=b[i+1];
                }
                if(b[i+1]-b[i]<min_nu)
                {
                    min_nu=b[i+1]-b[i];
                    p2.first=b[i];
                    p2.second=b[i+1];
                }
          }
               cout<<p2.first<<","<<p2.second<<" are closest, ";
               cout<< p1.first<<","<<p1.second<<" are most distant."<<endl;
        }
    }
    return 0;
}
void getPrimes(int n)//用欧拉筛法筛出2到根号n里面的素数
{
    memset(judge,false,sizeof judge);
    cnt=0;
    for(int i=2;i<=n;i++)
    {
       if(!judge[i])
        primes[cnt++]=i;
        for(int j=0;j<cnt&&i*primes[j]<=n;j++)
        {
            judge[i*primes[j]]=true;
            if(i%primes[j]==0)
                break;
        }
    }
}

POJ--2689-C++的更多相关文章

  1. poj 2689 Prime Distance(大区间素数)

    题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...

  2. 大区间素数筛选(POJ 2689)

    /* *POJ 2689 Prime Distance *给出一个区间[L,U],找出区间内容.相邻的距离最近的两个素数和距离最远的两个素数 *1<=L<U<=2147483647 ...

  3. POJ 2689 Prime Distance (素数+两次筛选)

    题目地址:http://poj.org/problem?id=2689 题意:给你一个不超过1000000的区间L-R,要你求出区间内相邻素数差的最大最小值,输出相邻素数. AC代码: #includ ...

  4. POJ 2689 Prime Distance(素数筛选)

    题目链接:http://poj.org/problem?id=2689 题意:给出一个区间[L, R],找出区间内相连的,距离最近和距离最远的两个素数对.其中(1<=L<R<=2,1 ...

  5. POJ 2689 - Prime Distance - [埃筛]

    题目链接:http://poj.org/problem?id=2689 Time Limit: 1000MS Memory Limit: 65536K Description The branch o ...

  6. 【POJ 2689】 Prime Distance

    [题目链接] http://poj.org/problem?id=2689 [算法] 我们知道,一个在区间[l,r]中的合数的最小质因子必然不超过sqrt(r) 那么,先暴力筛出1-50000中的质数 ...

  7. poj 2689 Prime Distance(大区间筛素数)

    http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...

  8. 数学#素数筛法 HDU 4548&POJ 2689

    找素数本来是很简单的问题,但当数据变大时,用朴素思想来找素数想必是会超时的,所以用素数筛法. 素数筛法 打表伪代码(用prime数组保存区间内的所有素数): void isPrime() vis[]数 ...

  9. poj 2689 Prime Distance (素数二次筛法)

    2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...

  10. 素数筛 poj 2689

    素数筛 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; ...

随机推荐

  1. jquery 去除兄弟节点的class

    function chooseCategory(dicCode,obj){ $(obj).siblings('a').removeClass("active"); $(obj).a ...

  2. k8s Service yaml文件编写

    apiVersion: app/v1 #API的版本号,版本号可以用 kubectl api-versions 查询到 kind: Service #表明资源对象,例如Pod.RC.Service.N ...

  3. ref(代替id)

    App.vue <template> <div> <Student ref="str"/> <h3 v-text="age&qu ...

  4. vuex中的state、mutations 、actions 、getters四大属性如何使用

    一.state (提供唯一的公共数据源) 方式1 在div中,$store.state.count 方式2 import {mapState} from 'vuex' computed:{ -mapS ...

  5. idea插件Tranlation配置有道搜索引擎

    idea配置有道翻译引擎 一.更换翻译引擎原因 由于Google在2022年9月末宣布关闭GoogleTranslate在中国的服务,原本在chrome浏览器和idea上使用的google翻译引擎也不 ...

  6. CSP-S T3函数调用

    函数是各种编程语言中一项重要的概念,借助函数,我们总可以将复杂的任务分解成一个个相对简单的子任务,直到细化为十分简单的基础操作,从而使代码的组织更加严密.更加有条理.然而,过多的函数调用也会导致额外的 ...

  7. Java调用Kettle

    Java 调用 kettle,难的不是怎么调用,而是解决 maven 依赖冲突问题,直接将 kettle 依赖,添加到我们的 maven 工程,可能会导致代码大范围报错:解决方案也很简单,就是直接从 ...

  8. 【Anaconda】为右键菜单添加“当前位置开启Anaconda Prompt”

    Stack Overflow 上查找到该解决方法:『Adding "Open Anaconda Prompt here" to context menu (Windows) - S ...

  9. CANas分析软件,DBC文件解析,CAN报文分析,仿CANoe曲线显示

    2023.01.01:增加对Kvaser的支持参考了CANoe写了下面的软件,主要用途是对报文的回放及曲线的分析. 1.CAN连接,支持周立功CAN.CANFD及PCAN 2.DBC解析与生成文件 打 ...

  10. 在windows如何下载android源码

    如链接https://blog.csdn.net/freekiteyu/article/details/70939672