题意很简单就是让你求给定区间的素数,然后用一个循环求出相距最远的相邻素数数和最近的素数以及相距最近的相邻素数
难点在与数据很大,所以不可能直接对区间的每一个数进行素数判断。但是,每个合数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. Mac下Homebrew更新国内源brew update卡死(完美解决,网上都不完整)

    https://blog.csdn.net/zz00008888/article/details/113880633

  2. Git Commit Rule

    ## git commit tagfeat: 新功能fix: 修复问题docs: 修改文档style: 修改代码格式,不影响代码逻辑refactor: 重构代码,理论上不影响现有功能perf: 提升性 ...

  3. SAP S/4HANA Cloud的功能亮点以及大中型企业为何更倾向选择它

    SAP-System Applications and Products,是一家来自德国的大型跨国软件公司,成立于1972年.作为全球企业管理和协同化商务解决方案供应商,世界第三大独立软件供应商和全球 ...

  4. ChainofResponsibility Pattern

    责任链模式: 参考:https://www.runoob.com/design-pattern/chain-of-responsibility-pattern.html 避免请求发送者与接收者耦合在一 ...

  5. etcd 入门

    服务注册与服务发现 三部分的作用: 注册中心:记录服务和服务地址的映射关系 服务提供者:将服务注册到服务中心 服务发现者:对服务中心的服务进行调用 简单易懂的 raft 算法 Raft算法是一个一致性 ...

  6. java向上转型知识点收录

    package tex2polymorphism; /*总结如下: * 对于多态,可以总结它为: 一.使用父类类型的引用指向子类的对象: 二.该引用只能调用父类中定义的方法和变量: 三.如果子类中重写 ...

  7. ts(typescript)讲解for , for...in..., for...of..., while, every, some, map, filter

    for  一般用于已知循环次数 var num:number = 5; var i:number; var factorial = 1; for(i = num;i>=1;i--) { fact ...

  8. django解决网站CORS前后端跨域问题

    1.安装cors-headers⼯具   pip install django-cors-headers 2.安装cors-headers应⽤ # 注册应用 INSTALLED_APPS = [ 'd ...

  9. vue使用websoket

    参考链接:https://www.cnblogs.com/qisi007/p/10213886.html export default { name: "realdetail", ...

  10. nohup文件的压缩分割

    编写sh脚本 先拷贝,之后,清空. 待完成,压缩功能 #!/bin/sh #description split logs time1=$(date -d 'yesterday' "+%Y%m ...