Description

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.
解题思路:题目的意思就是给出区间[L,U],要求分别找出最近距离、最远距离的一对相邻素数,其中区间[L,U]长度不超过1e6,且L,U∈[1,2147483647]。由于L,U允许有最大值,而在堆区数组也开不了这么大的内存,所以应该考虑区间筛法。由埃氏筛的定义可知:通过枚举不大于sqrt(n)内的所有素数,将素数的倍数一一筛掉,最后剩下的都是素数。利用这个特点,我们可以先做好[2,sqrt(U)]的素数表,然后从[2,sqrt(U)]的表中筛得素数的同时,也将其倍数从[L,U]的表中划去(这里由于开不了很大的数组,所以区间[L,R]中每个值即下标整体向左偏移了L个单位,这样就够标记区间长达1e6个元素了,详解看代码),那么最后剩下的就是区间[L,U]内的素数了。注意:必须特判L,因为题目中要求输出相邻一对素数之间的距离,前提必须是素数,而1不是素数,所以区间左端点值最小为2。
AC代码:
 #include<iostream>
#include<string.h>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long LL;
const int maxn=1e6+;
LL L,U,prime[maxn];bool isp1[maxn],isp2[maxn];
int segment_sieve(LL L,LL R){//区间筛法
memset(isp1,true,sizeof(isp1));//isp1标记[2,sqrt(R)]内的素数
memset(isp2,true,sizeof(isp2));//isp2标记区间[L,R]中的素数,其中isp2[j-L]=true <=> j是素数
if(L<)L=;//注意左端点必须是从2开始,必须特判这种情况
for(LL i=;i*i<=R;++i){//枚举不大于sqrt(R)即可(埃氏筛)
if(isp1[i]){
for(LL j=i*i;j*j<=R;j+=i)isp1[j]=false;//筛[2,sqrt(R)],选出这个区间的所有素数,然后去筛掉区间[L,R]的所有合数
for(LL j=max(2LL,(L+i-)/i)*i;j<=R;j+=i)isp2[j-L]=false;
//(L+i-1)/i得到最接近L的i的倍数,最小是i的2倍,然后筛掉区间[L,R]中素数i的倍数(合数)
}
}
int cnt=;//记录区间[L,R]中素数的个数
for(LL i=;i<=R-L;++i)//0~R-L
if(isp2[i])prime[cnt++]=i+L;//保存区间[L,R]中的所有素数
return cnt;
}
int main(){
while(~scanf("%lld%lld",&L,&U)){
int num=segment_sieve(L,U);
if(num<)printf("There are no adjacent primes.\n");//如果个数小于2,说明没有相邻的素数,直接输出
else{//依次去枚举区间[L,U]中所有相邻素数的距离
LL mlt=,mrt=,nlt=,nrt=,f1=,f2=maxn;
for(int i=;i<num;++i){
if(prime[i]-prime[i-]>f1)//最远距离的一对相邻素数
f1=prime[i]-prime[i-],mlt=prime[i-],mrt=prime[i];
if(prime[i]-prime[i-]<f2)//最近距离的一对相邻素数
f2=prime[i]-prime[i-],nlt=prime[i-],nrt=prime[i];
}
printf("%lld,%lld are closest, %lld,%lld are most distant.\n",nlt,nrt,mlt,mrt);
}
}
return ;
}

题解报告:poj 2689 Prime Distance(区间素数筛)的更多相关文章

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

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

  2. POJ 2689 Prime Distance (素数筛选法,大区间筛选)

    题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...

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

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9944   Accepted: 2677 De ...

  4. POJ - 2689 Prime Distance (区间筛)

    题意:求[L,R]中差值最小和最大的相邻素数(区间长度不超过1e6). 由于非素数$n$必然能被一个不超过$\sqrt n$的素数筛掉,因此首先筛出$[1,\sqrt R]$中的全部素数,然后用这些素 ...

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

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

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

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

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

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

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

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

  9. [ACM] POJ 2689 Prime Distance (筛选范围大素数)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12811   Accepted: 3420 D ...

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

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

随机推荐

  1. 【Todo】C++和Java里面的浮点数及各种数字表示

    今天看了这篇文章,是讲C++中的浮点类型的:<浮点数的二进制表示> 再复习一下Java里面的Float和Double. 首先,直接数字赋值给Float变量是不行的,数字后要加上F,这样写: ...

  2. sudo 用户添加

    sudo 用户添加 /etc/sudoers 在 ## Allow root to run any commands anywhere root    ALL=(ALL)   ALL 下面加上 xxx ...

  3. Office WORD如何为每一页设置不同的页眉页脚

    如下图所示,我想要为封面和目录,摘要等等设置不同的页眉页脚(一般封面和目录不需要页脚)   而从正文开始,套用相同的页眉和以页数作为页脚(注意"第一章 绪论"不是这个文档的第一页) ...

  4. Qt学习--初学注意事项

    过程.心得: 1)Qt Creator与相关的安装包的安装 我在选择去学习Qt之后,第一件事就是Qt SDK下载安装与配置.最初,在网上发现Qt使用的IDE环境        在Windows上可以选 ...

  5. 生成器 减少代码行数 map reduce

    map reduce l = [[2, 3], [2, 3, 4]] ll = [2, 3, 4] l = [int(i) for i in set(','.join([','.join([str(i ...

  6. Webservice(CXF) 、 POI(excel)操作部署到weblogic上冲突解决

    这几日把webservice和POI 操作部署到WebLogic上,问题重重,有各种冲突. 部署到tomcat上没有问题 版本: jdk:6 tomcat:6 weblogic:10.3.3 cxf: ...

  7. Strus2中关于ValueStack详解

    什么是ValueStack 它是一个接口com.opensymphony.xwork2.util.ValueStack.我们使用它是将其做为一个容器,用于携带action数据到页面.在页面上通过ogn ...

  8. YTU 2412: 帮警长数一数【循环、分支简单综合】

    2412: 帮警长数一数[循环.分支简单综合] 时间限制: 1 Sec  内存限制: 64 MB 提交: 323  解决: 169 题目描述 黑猫警长在犯罪现场发现了一些血迹,现已经委托检验机构确定了 ...

  9. RMQ(Range Minimum Query)问题(转)

    问题描述 RMQ问题是求给定区间中的最值问题.对于长度为n的数列A,回答若干查询RMQ(A, i, j).返回数组A中下标在[i,j]里的最小值的下标. 比如数列 5,8,1,3,6,4,9,5,7 ...

  10. go语言godep使用命令

    godep 看见他的star比govendor,所以我使用它.官方地址 https://github.com/tools/godep install   1 go get github.com/too ...