Prime Distance


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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.

题目大意

求两个数之间相邻素数值相差最小和最大的数

分析:这道题是学习素数筛法的经典,应用到了区间筛素数。具体思路是先筛出1到sqrt(2147483647)之间的所有素数,然后再通过已经晒好素数筛出给定区间的素数。

题目中的U,L最大值可为整型上限,用纯粹的暴力筛法肯定要超时?怎么办,用二次筛法。U和L之间的合数,质因子不超过O(L^0.5),于是用筛法选出50000内的素数即可,因为50000的平方大于整形上线了。再用这些素数去筛出U-L之间的合数,剩下的就是U-L之间的素数了,边筛边计算两个素数之间的差,就OK了。另外,尤其小心的是U为1的情况,做素数题往往1是个坑爹的东西。。。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int N=50000;//为什么用到5W?U和L之间的合数,
//质因子不超过O(L^0.5),于是用筛法选出50000内的素数即可,因为50000的平方大于整形上线了
//比如说你只求到1W,但是你有个质数是42347(乱说一个的);那么42347*42347这个数你就不能使他为合数了
//因为你利用的是质数的倍数为合数的筛选法,你没筛选到50000这里
int r[1000000],a[N+100],b[N+100],z; int main()
{
int a0,b0,i,j;
z=0;
memset(a,0,sizeof(a));
for(i=2;i<=N;i++) //1st prime sieve第一次筛选得到5W内的所有质数
if(a[i]==0){
b[++z]=i;//b[z]用来得到a[]中所有的素数z:1->z
for(j=2;i*j<=N;j++) a[i*j]=1;//i的倍数都为1
}
while(~scanf("%d%d",&a0,&b0)){
memset(r,0,sizeof(r));
int t=0,dis,max=-1,min=9999999,m1,m2; for(i=1;i<=z;i++){ //prime sieve again(b[1]到b[z]的素数)
if(b[i]>b0)break;//对于一些小数据来说可以直接就中断掉了
int s,t;
s=(a0-1)/b[i]+1;//if(a0%b[i]==0)a0/b[i]==(a0-1)/b[i]+1;往上取整
t=b0/b[i];//往下取整 这样取整的目的是为了让0<=j*b[i]-a0<=b0-a0 不然其他取整会超出范围
for(j=s;j<=t;j++)//j的取值一定是>=1 如果是==1的情况 说明(a0-1)<b[i]即a0<=b[i]
if(j>1) r[j*b[i]-a0]=1;//r[]==1表示为合数(为了限定范围在0<->(b0-a0),其实意思是j*b[i]是一个合数)
}//j!=1 举 1,20 b[i]=7 这时s=1,t=2 if(j==1)可以 则[1*7-1]=1 正如上面所说后面还要a0=1 难道说7是合数?
//总的来说,j==1时 b[i]是质数 j*b[i]-a0+a0肯定也是质数 int k=-1;
for(i=0;i<=b0-a0;i++)
if(!r[i]){//如果r[i]==0 (不是合数的情况)
if(k!=-1){//第一个是不能计的至少要两个
dis=i-k;//与前一次的差
if(dis>max){
max=dis;
m1=i+a0;//m1表示的是最大值
}
if(dis<min){
min=dis;
m2=i+a0;//m2表示的是第二小
} }
if(i+a0!=1) k=i;//第一次是这个用来下一次的使用
// k=i;//这样是错的 注意当a0=1的时候r[0]是一定为0的但是[举[1,2]]
//如果这边没注意让 k=0的话,max=i-0,mi=i+a0(1);显然当元素中只有一个素数的时候是不能输出结果 但是这样却有结果
} //if(max<0)说明没有进行过任何比较
if(max<0) printf("There are no adjacent primes.\n");
else printf("%d,%d are closest, %d,%d are most distant.\n",m2-min,m2,m1-max,m1);
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

ZOJ 1842 Prime Distance(素数筛选法2次使用)的更多相关文章

  1. POJ2689 - Prime Distance(素数筛选)

    题目大意 给定两个数L和U,要求你求出在区间[L, U] 内所有素数中,相邻两个素数差值最小的两个素数C1和C2以及相邻两个素数差值最大的两个素数D1和D2,并且L-U<1,000,000 题解 ...

  2. zoj 1842 Prime Distance

    // 数论题,增强的筛法,回想素数筛法 // 只要筛到最大数的开方,剩下的就是素数 // 于是这里,开一个 sqrt(2^31) 大约 65536 的素数表,然后 // 对于每个 L~U 的区间,筛掉 ...

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

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

  4. PAT甲题题解-1059. Prime Factors (25)-素数筛选法

    用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...

  5. 素数筛选法(prime seive)

    素数筛选法比较有名的,较常用的是Sieve of Eratosthenes,为古希腊数学家埃拉托色尼(Eratosthenes 274B.C.-194B.C.)提出的一种筛选法.详细步骤及图示讲解,还 ...

  6. POJ 3978 Primes(素数筛选法)

    题目 简单的计算A,B之间有多少个素数 只是测试数据有是负的 //AC //A和B之间有多少个素数 //数据可能有负的!!! #include<string.h> #include< ...

  7. LightOJ 1259 Goldbach`s Conjecture (哥德巴赫猜想 + 素数筛选法)

    http://lightoj.com/volume_showproblem.php?problem=1259 题目大意:给你一个数n,这个数能分成两个素数a.b,n = a + b且a<=b,问 ...

  8. poj 2262 Goldbach's Conjecture(素数筛选法)

    http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total ...

  9. HDU_2136——最大质因数,素数筛选法

    Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...

随机推荐

  1. vs code 写C#心得

    !!!官方指南请访问: https://docs.microsoft.com/zh-cn/dotnet/core/index code runner 插件 ---------------------- ...

  2. PCB布线设计(1)

    在PCB设计的时候,初学的时候对布线设计一无所知,那个时候老师布置  AT91SAM7X-开发板 作为学习例板  ,最终采用自动布线的结果如下 也并非全为自动布线,自动布线对于这种元器件稍多的很难全部 ...

  3. Python enumerate()方法

    for循环中如果要获取当前元素的索引值,一个方法是定义一个计数器,每次取值的时候将这个值加一,如果是列表的话可以用index()函数,而python中有一个比较简洁的方法而已直接获得索引值,并可以方便 ...

  4. python 验证码识别初探

    使用 pytesser 与 pytesseract 识别验证码 前置 :  首先需要安装  tesserract tesserract windows 安装包及中文 https://pan.baidu ...

  5. 对Linux命令od -tc -tx1的C语言程序实现myod-优化版

    导语 自编od C语言实现版名为myod 上个星期有一个初代版,链接- myod原版 这星期的课上要求实现myod-系统调用版本,要求如下 1 参考教材第十章内容 2 用Linux IO相关系统调用编 ...

  6. SRM First Problem && SRM 638 250pts NamingConvention

    NamingConvention 题意: 给一个字符串,删掉所有的'_',然后将‘_'后的第一个字符改成大写. 代码: #include<bits/stdc++.h> using name ...

  7. LVS入门篇(五)之LVS+Keepalived实战

    一.实验架构和环境说明 (1)本次基于VMware Workstation搭建一个四台Linux(CentOS 7.4)系统所构成的一个服务器集群,其中两台负载均衡服务器(一台为主机,另一台为备机), ...

  8. Tp框架之命名空间

    命名空间,相当于虚拟目录 实现自动加载类的机制 初始命名空间:Library文件夹 初始命名空间下面有很多根命名空间: 1.Library里面的文件夹 2.APP的模块文件夹 在tp框架中,只有这两个 ...

  9. 一个web应用的诞生(2)--使用模板

    经过了第一章的内容,已经可以做出一些简单的页面,首先用这种方式做一个登录页面,首先要创建一个login的路由方法: @app.route("/login",methods=[&qu ...

  10. Keil出错解决方法

    1.安装KEIL5后创建工程后出现这个报错 解决方法:打开下图目录的文件. Keil.STM32F1xx_DFP.pdsc文件是只读文件,必须将只读属性取消. 如下图所示,注释掉红色圆圈的哪一行,保存 ...