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. SpringBoot初始教程之项目结构(一)

    SpringBoot初始教程之项目结构 1 简介 spring Boot makes it easy to create stand-alone, production-grade Spring ba ...

  2. Android网络通信之Socket

    在移动APP开发中.网络通信数据传输是必定存在的.移动APP离开了网络通信数据传输的功能方式,就好比一潭死水,永远都 是原来的样子. 提到网络通信传输数据.首先出如今程序猿脑海中的是HTTP协议传输, ...

  3. iOS iOS8中 问题&quot;registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later&quot; 解决方式

    问题重述: iOS 8中改变了通知注冊的方式,假设App须要同一时候支持iOS 7 和 8 的话,须要首先检查selector. 解决方式:在Xcode 6中 - (BOOL)application: ...

  4. Android自带的分享功能案例

    MainActivity的代码 package com.hpsvse.weiboshare; import java.io.File; import android.net.Uri; import a ...

  5. MySQL的引入,绿色包下载和应用

    一.下载MySQL绿色版 1.下载地址: 以下是MySQL最新绿色版链接(都是来源于oracle官网),点击以下链接直接下载. 1.1.官网链接:https://www.oracle.com/inde ...

  6. 堆排序C++实现

    //heap sort //堆排序能够分为两个过程.其一是建堆.其二是出堆 //堆是一种全然二叉树,所以它能够用数组进行存储. //堆可分为最大堆和最小堆.最大堆指任一节点的值都大于其左右孩子节点的值 ...

  7. Buildroot 龙芯1C支持指南

    本文转载自:https://github.com/pengphei/smartloong-sphinx/blob/master/source/cn/loongson1c_buildroot_guide ...

  8. mysql字符集设置utf-8

    mysql字符集设置utf-8 mysql修改环境的默认字符集为utf-8(当然你也可以设置成别的,国际点还是utf-8好) 如果不把mysql字符集统一下,后面还是有点麻烦的 首先得在服务里关掉my ...

  9. 配置JDK环境变量配置及path和classpath的作用

    1.环境变量配置 用鼠标右击“我的电脑”->属性->高级->环境变量 JAVA_HOME :D:\Program Files\Java\jdk1.6.0_12(JDK安装路径) Pa ...

  10. 苹果Instruments/Shark性能调试工具概述

    在Mac OS X上你可以使用Gprof这样的UNIX工具用于测试程序性能.当然,Apple也有自己的Profiling Tools,用得比较多的是Shark.10.5里还引入了一个基于DTrace的 ...