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. IntelliJ IDEA删除项目

    删除项目一向比较奇葩,因为当你点击到该项目名称右键时,并没有delete选项,导致我们不知道怎么删除,查找多方文档,得到以下解决: 1.将鼠标移到要删除的项目名称上,单击并按“Delete”按钮删除项 ...

  2. NIO与传统IO的区别(形象比喻)[转]

    传统的socket IO中,需要为每个连接创建一个线程,当并发的连接数量非常巨大时,线程所占用的栈内存和CPU线程切换的开销将非常巨大.使用NIO,不再需要为每个线程创建单独的线程,可以用一个含有限数 ...

  3. dwarf调试信息格式入门

    https://www.prevanders.net/dwarf.html#testingcomment http://www.dwarfstd.org/ http://www.cnblogs.com ...

  4. Scala学习笔记 & 一些不错的学习材料 & 函数编程的历史八卦

    参考这篇文章: http://www.ibm.com/developerworks/cn/java/j-lo-funinscala1/ 这也是一个系列 严格意义上的编程范式分为:命令式编程(Imper ...

  5. 【Todo】C++类 & 通用面试题分析记录 & 最难的bug

    1. the most difficult bug u fixed and how u solved this problem.. 解决过很多疑难bug.最困难的分为两类.一类是并发.多线程类的,因为 ...

  6. keras常见问题解答

    https://keras.io/zh/getting-started/faq/ https://keras.io/zh/ https://github.com/keras-team/keras/tr ...

  7. 天下文章一大抄 mysql远程连接

    使用GRANT命令创建远程连接mysql授权用户特定用户mysql -u root -ppassword   注意:p后面没有空格直接密码.mysql>grant all privileges ...

  8. Hash分析

    分析Hash 列表内容 Hash表中的一些原理/概念,及依据这些原理/概念,自己设计一个用来存放/查找数据的Hash表,而且与JDK中的HashMap类进行比較. 我们分一下七个步骤来进行. Hash ...

  9. (转) SYSTEM_HANDLE_INFORMATION中ObjectTypeIndex的定义

    typedef struct _SYSTEM_HANDLE_TABLE_ENTRY_INFO{ USHORT UniqueProcessId; USHORT CreatorBackTraceIndex ...

  10. Android-studio 连接真机 调试weex项目

    1.选择项目 platforms  /  android 2.创建虚拟机(AVD) (1)点击 AVD Manager (2) 点击  Create Virtual Device 最后发现 CPU 不 ...