题目描述:

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

同时,你还需要找到距离最远的两个相邻质数D1和D2(即D1-D2是最大的),如果存在相同距离的其他相邻质数对,则输出第一对。

输入格式:

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

输出格式:

对于每个L和U ,输出一个结果,结果占一行。

结果包括距离最近的相邻质数对和距离最远的相邻质数对。(具体格式参照样例)

如果L和U之间不存在质数对,则输出“There are no adjacent primes.”。

数据范围:

1≤L<U≤231-1,R-L≤106

输入样例:

2 17

14 17

输出样例:

2,3 are closest, 7,11 are most distant.
There are no adjacent primes.

思路:

①用筛法求出2~sqrt(R)之间的所有质数。

②标记i*p(ceil(L/p)≤i≤floor(R/p))。

③所有未标记的数就是[L,R]中的质数。对相邻的质数两两比较,找出差最大的。

时间复杂度:O(∑质数p≤sqrt(R)(R-L)/p)=O(sqrt(R)log log sqrt(R)+(R-L)log log R)。

代码如下:

#include<bits/stdc++.h>
#define N 1000000+10
#define M 100000+10
using namespace std;

int l,r,m;
int v[M],prime[M];
bool st[N];

void primes(int n){//线性筛质数
    memset(v,,sizeof(v));
    m=;
    ;i<=n;i++){
        ){
            v[i]=i;
            prime[++m]=i;
        }

        ;j<=m;j++){
            if(prime[j]>v[i]||prime[j]>n/i)break;
            v[i*prime[j]]=prime[j];
        }
    }
}

int main(){
    primes();//step 1 

    while(cin>>l>>r){
        memset(st,true,sizeof(st));
        )st[]=false;

        ;prime[i]<=sqrt(r);i++){
            int p=prime[i];

            int l1,r1;
            )l1=l/p;
            ;
            r1=floor(r/p);

            ,l1);j<=r1;j++)
                st[j*p-l]=false;//step 2
        }

        ,min2=-,max1=-,max2=-;
        ;
        int before;
        bool t=false;

        ;i<=r-l;i++){
            if(!st[i])continue;

            if(!t){
                before=i;t=true;
            }

            else{
                if(i-before>maxx){
                    max1=before;max2=i;
                    maxx=i-before;
                }

                if(i-before<minn){
                    min1=before;min2=i;
                    minn=i-before;
                }

                before=i;
            }
        }//step 3

        )cout<<"There are no adjacent primes."<<endl;
        else{
            cout<<min1+l<<","<<min2+l<<" are closest, "
                <<max1+l<<","<<max2+l<<" are most distant."<<endl;
        }
    }

    ;
} 

0x31 prime distance(质数)的更多相关文章

  1. POJ2689 Prime Distance 质数筛选

    题目大意 求区间[L, R]中距离最大和最小的两对相邻质数.R<2^31, R-L<1e6. 总体思路 本题数据很大.求sqrt(R)的所有质数,用这些质数乘以j, j+1, j+2... ...

  2. 一本通1619【例 1】Prime Distance

    1619: [例 1]Prime Distance 题目描述 原题来自:Waterloo local,题面详见 POJ 2689 给定两个整数 L,R,求闭区间 [L,R] 中相邻两个质数差值最小的数 ...

  3. UVA10140 Prime Distance

    UVA10140 Prime Distance 给定两个整数L,R(1<=L<=R<=2^{31},R-L<=10^6)L,R(1<=L<=R<=231,R− ...

  4. ZOJ 1842 Prime Distance(素数筛选法2次使用)

    Prime Distance Time Limit: 2 Seconds      Memory Limit: 65536 KB The branch of mathematics called nu ...

  5. 【题解】UVA10140 [Prime Distance]

    [题解]UVA10140 Prime Distance 哈哈哈哈\(miller-rabbin\)水过去了哈哈哈 还能怎么办呢?\(miller-rabbin\)直接搞.枚举即可,还跑得飞快. 当然此 ...

  6. 数论 - 素数的运用 --- poj 2689 : Prime Distance

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12512   Accepted: 3340 D ...

  7. UVA 10140 - Prime Distance(数论)

    10140 - Prime Distance 题目链接 题意:求[l,r]区间内近期和最远的素数对. 思路:素数打表,打到sqrt(Max)就可以,然后利用大的表去筛素数.因为[l, r]最多100W ...

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

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

  9. [POJ268] Prime Distance(素数筛)

    /* * 二次筛素数 * POJ268----Prime Distance(数论,素数筛) */ #include<cstdio> #include<vector> using ...

随机推荐

  1. 第一个kotlin程序

    class ccc { companion object { @JvmStatic fun main(args: Array<String>) { println("hello! ...

  2. 数据库的事务日志已满,起因为"LOG_BACKUP"。

    问题描述: 数据库的事务日志已满,起因为"LOG_BACKUP". 问题截图: 解决方法: 1).选择数据库–属性—选项—恢复模式–选择简单.2).收缩数据库后,再调回完整. US ...

  3. 零元学Expression Blend 4 - Chapter 37 看如何使用Clip修出想要的完美曲线(上)

    原文:零元学Expression Blend 4 - Chapter 37 看如何使用Clip修出想要的完美曲线(上) 几何外部的 UIElement 会在呈现的配置中以视觉化方式裁剪. 几何不一定要 ...

  4. WebP 极限压缩及ios实现

      WebP 极限压缩及ios实现 Levi.duan 会议提纲 概念以及定义 什么是Webp ? WebP (发音 weppy ),是一种同时提供了有损压缩与无损压缩的图片文件格式,是Google新 ...

  5. 【操作系统】关于Linux桌面操作系统

    以前是Win+Ubuntu+黑苹果,周末想体验一下deepin,于是简单安装了一下,安装过程很简单,这里不再描述.安装之后,第一次打开系统,确实很惊艳,赏心悦目的操作系统. 之前用Ubuntu时候,C ...

  6. URL重写 httpModules IIS7

    <system.web> <httpModules> <!--URL重写:IIS 及以下用次处配置--> <!--add name="MyHttpM ...

  7. 由TreeView 如何触发OnSelectedNodeChanged事件想到的与做到的

    前提是每层的结点上设置了NavigateUrl OnSelectedNodeChanged事件好像失去了作用. 另外TreeView是没有AutoPostBack的!!!! 那么要如何触发OnSele ...

  8. HTML连载8-video标签

    ​一.video标签 1.作用:播放视频 2.格式:<video src="视频地址“></video> 3.可添加的属性: (1)autoplay="au ...

  9. ZooKeeper学习第五期--ZooKeeper管理分布式环境中的数据(转)

    转载来源:https://www.cnblogs.com/sunddenly/p/4092654.html 引言 本节本来是要介绍ZooKeeper的实现原理,但是ZooKeeper的原理比较复杂,它 ...

  10. pytorch实现yolov3(2) 配置文件解析及各layer生成

    配置文件 配置文件yolov3.cfg定义了网络的结构 .... [convolutional] batch_normalize=1 filters=64 size=3 stride=2 pad=1 ...