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. (入门SpringBoot)SpringBoot项目数据源以及整合mybatis(二)

    1.配置tomcat数据源: #   数据源基本配置spring.datasource.url=jdbc:mysql://localhost:3306/shoptest?useUnicode=true ...

  2. activiti实现的请假流程

    直接上图,还是有点复杂的

  3. 邁向IT專家成功之路的三十則鐵律 鐵律一:IT人生存之道-柔

    老子在道德經裡頭曾提到:「天下之至柔,馳聘天下之至堅」,又說:「堅強者死之徒,柔弱者生之徒」.其實人在面對世間的萬事萬物都是一樣的,只是當我們學習將這個至理套用在IT的工作職場時,將可以讓我們在這條崎 ...

  4. ViewPager 无限循环遇到的坑 viewpager.setOffscreenPageLimit(2);

    viewpager.setOffscreenPageLimit(limit);这个方法,是表示viewpage除了当前显示的页面外,左右个预加载的页面个数,也就是 为limit=2时表示当前一共加载了 ...

  5. discuz搬家

    1.需要重命名forumdata下面的cache和templates文件夹 2.如果数据库账户名和密码有改动过,需要修改config.inc.php 3.保证uc_server的链接正确!

  6. centos7 网络不能重启问题 解决办法

    cnetos7 网络不可重启 突然解决办法 参考他人处理 之前部署hadoop环境,在自己机器上安装了一台centos虚拟机,然后图省事,就克隆出三台,一台为master,另两台来作为 slave. ...

  7. 释放SQL Server占用的内存 .Net 读取xml UrlReWriter 在web.config中简单的配置

    释放SQL Server占用的内存   由于Sql Server对于系统内存的管理策略是有多少占多少,除非系统内存不够用了(大约到剩余内存为4M左右),Sql Server才会释放一点点内存.所以很多 ...

  8. 细说linux IPC(三):mmap系统调用共享内存

    [版权声明:尊重原创,转载请保留出处:blog.csdn.net/shallnet 或 .../gentleliu,文章仅供学习交流,请勿用于商业用途]         前面讲到socket的进程间通 ...

  9. OpenCV中使用SVM简介

    下面这是opencv官方文档中的代码,我加了一部分注释: #include "stdafx.h" #include "opencv2/core/core.hpp" ...

  10. Nova虚拟机迁移

    这里根据我的配置环境只讲述冷迁移(Migrate Instance)需要进行的计算节点配置而不包含热迁移(Live Migrate Instance),后者需要共享存储及Hypervisor的支持. ...