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.

题意:求一个长度不超过1e6的区间[L,R](L和R很大)中,距离最近和最远的素数对。

思路:求出1到sqrt(R)间的素数,然后利用这些素数筛去[L,R]中的合数。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int p[maxn+],vis[maxn+],cnt,L,R;
int wl,wr,vl,vr,tag[],times;
int main()
{
for(int i=;i<=maxn;i++){
if(!vis[i]) p[++cnt]=i;
for(int j=i+i;j<=maxn;j+=i) vis[j]=;
}
while(cin>>L>>R){
if(L==) L++;
times++;
for(int i=;i<=cnt;i++){
int a=(L-)/p[i]+;
int b=R/p[i];
for(int j=a;j<=b;j++)
if(j>) tag[j*p[i]-L]=times;
}
int pre=-; wl=wr=vl=vr=;
for(int i=;i<=R-L;i++){
if(tag[i]!=times) {
if(pre!=-) {
if(wl==) wl=vl=pre+L,wr=vr=i+L;
else {
if(i-pre>wr-wl) wl=pre+L,wr=i+L;
if(i-pre<vr-vl) vl=pre+L,vr=i+L;
}
}
pre=i;
}
}
if(wl==) printf("There are no adjacent primes.\n");
else printf("%d,%d are closest, %d,%d are most distant.\n",vl,vr,wl,wr);
}
return ;
}

POJ2689:Prime Distance(大数区间素数筛)的更多相关文章

  1. 题解报告:poj 2689 Prime Distance(区间素数筛)

    Description The branch of mathematics called number theory is about properties of numbers. One of th ...

  2. POJ-2689 Prime Distance (两重筛素数,区间平移)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13961   Accepted: 3725 D ...

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

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

  4. LightOj 1197 Help Hanzo 区间素数筛

    题意: 给定一个区间a,b,a-b>=100000,1<=a<=b<=231,求出给定a,b区间内的素数的个数 区间素数筛 (a+i-1)/ ii向上取整,当a为 i 的整数倍 ...

  5. POJ-2689 Prime Distance,区间素数筛法

                                                    Prime Distance 只会埃氏筛法的弱鸡今天读了读挑战程序设计120页,明白了求小区间内素数的方 ...

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

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

  7. POJ2689 Prime Distance(数论:素数筛选模板)

    题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...

  8. 解题报告:poj2689 Prime Distance

    2017-10-03 11:29:20 writer:pprp 来源:kuangbin模板 从已经筛选好的素数中筛选出规定区间的素数 /* *prime DIstance *给出一个区间[L,U],找 ...

  9. poj2689 Prime Distance题解报告

    题目戳这里 [题目大意] 给定一个区间[L,R],求区间内的质数相邻两个距离最大和最小的. [思路分析] 其实很简单呀,很明显可以看出来是数论题,有关于质数的知识. 要注意一下的就是L和R的数据范围都 ...

随机推荐

  1. Idea基本讲解、常用配置以及快捷键设置

    IDEA官网学习文档:https://www.jetbrains.com/idea/documentation/ IDEA官网入门教程:https://www.jetbrains.com/help/i ...

  2. java 使用POI读取excel数据

    原文:http://doc.okbase.net/0201zcr/archive/161440.html 一.定义 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Ja ...

  3. 【mac】mac上使用brew 安装速度慢/每次使用brew 都会卡在updating homebrew不动/更换homebrew的镜像源

    有没有出现一下这样的情况: 如果有,请继续往下走 1.打开mac的命令窗口,键入如下命令 cd /usr/local/Homebrew 2.更换homebrew的默认源[更换为中科大的镜像源] git ...

  4. 【docker】启动docker连接数据库 出现FATAL: password authentucation failed for user "homestatead"问题

    docker可以成功启动,启动命令如下: docker run -d -p : -v `pwd`/pgdata:/var/lib/postgresql/data -e POSTGRES_USER=ho ...

  5. Base64的空格 + 问题...

    BASE64  通过url传递到后台 加号变空格的处理方法 解决方法: 前台处理:str.replace("+", "%2B"); (错误) <scrip ...

  6. 【转】 使用 Python 获取 Linux 系统信息

    在本文中,我们将会探索使用Python编程语言工具来检索Linux系统各种信息.走你. 哪个Python版本? 当我提及Python,所指的就是CPython 2(准确的是2.7).我会显式提醒那些相 ...

  7. DOS环境进入及基本命令

    DOS:磁盘操作系统(Disk Operating System) Window环境下如何进入DOS: 1. 以win10为例,按ctrl+R打开运行窗口,在输入框输入"CMD"并 ...

  8. docker save docker load

    docker save && docker load docker save 镜像1 镜像2 | gzip > images.tar.gz 打包镜像为压缩文件 docker sa ...

  9. java中等待所有线程都执行结束(转)

    转自:http://blog.csdn.net/liweisnake/article/details/12966761 今天看到一篇文章,是关于java中如何等待所有线程都执行结束,文章总结得很好,原 ...

  10. 手把手编写自己的PHPMVC框架

    1 什么是MVC MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controller ...