博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ2689 Prime Distance题解
阅读量:4975 次
发布时间:2019-06-12

本文共 2930 字,大约阅读时间需要 9 分钟。

题目

题目描述

英文题目

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 \leqslant L< U \leqslant 2,147,483,647\)), and you are to find the two adjacent primes \(C1\) and \(C2\) (\(L \leqslant C1< C2 \leqslant 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 \leqslant D1< D2 \leqslant U\)) where \(D1\) and \(D2\) are as distant from each other as possible (again choosing the first pair if there is a tie).

中文题意

给定两个整数\(L,U\)(\(1 \leqslant L< U \leqslant 2,147,483,647\)),求闭区间 \(\left[L,U\right]\)中相邻两个质数的差的最小值和最大值是多少,分别输出这两对质数。

输入输出格式

输入格式

每行两个整数\(L,R\)(\(1 \leqslant L< R \leqslant 2,147,483,647,R-L \leqslant 10^6\))。

输出格式

对于每个\(L,R\),输出最小值和最大值,格式参照样例。若区间内无质数,输出"There are no adjacent primes."。

输入输出样例

输入样例

2 17

14 17

输出样例

2,3 are closest, 7,11 are most distant.

There are no adjacent primes.

题解

由于\(L,R\)的范围很大,所以埃氏筛法和欧拉筛法都无法生成\(\left[1,R\right]\)的所有质数。但是\(R-L\)的范围很小且任何一个合数\(n\)一定包含一个不超过\(\sqrt{n}\)的质因子,所以我们只需要用筛法求出\(2,3,\cdots,\sqrt{n}\)的所有质数。而对于每一个质数\(p\),标记\(i \times p \left(\left\lceil\frac{L}{p}\right\rceil \leqslant i \leqslant \left\lceil\frac{R}{p}\right\rceil\right)\)为合数。标记完后,剩下的所有数就是\(\left[L,R\right]\)中的质数了。再两两比较,找出差最大和最小的就可以了。

代码如下:

#include 
#include
#include
#include
using namespace std;int n,m,i,j,t1,t2,x1,x2,y1,y2,l,r,a[100001], b[1000001];bool v[1000001];void prime()//筛出2~46340之间的质数{ memset(v,1,sizeof(v)); for(i=2;i<=46340;i++) if(v[i]){ a[++n]=i; for(j=2;j<=46340/i;j++) v[i*j]=false; }}int main(){ prime(); while(cin>>l>>r){ memset(v,1,sizeof(v)); if(l==1) v[0]=false; for(i=1;i<=n;i++) for(j=l/a[i];j<=r/a[i];j++) if(j>1) v[a[i]*j-l]=false;//去除L~R中的合数 m=0; for(i=l;i<=r;i++){ if(v[i-l]) b[++m]=i;//存储L~R中的质数 if(i==r) break; } t1=2147483647; t2=0; for(i=1;i
t2){ t2=j; x2=b[i]; y2=b[i+1]; } }//比较差的大小 if(!t2) cout<<"There are no adjacent primes."<

转载于:https://www.cnblogs.com/jacky567/p/10583528.html

你可能感兴趣的文章
Spring中使用Velocity模板
查看>>
上周热点回顾(8.18-8.24)
查看>>
Feature toggle
查看>>
day02
查看>>
gvim 配置Pydiction
查看>>
Linux安装指定mysql版本
查看>>
分布式锁的三种实现方式
查看>>
poj 2109 pow函数也能这么用?p的开n次方
查看>>
Oracle database link
查看>>
python调用shell小技巧
查看>>
TL431的几种常用用法
查看>>
js 经典闭包题目详解
查看>>
在项目中移除CocoaPods
查看>>
【洛谷】CYJian的水题大赛【第二弹】解题报告
查看>>
POJ 1703 Find them, Catch them【种类/带权并查集+判断两元素是否在同一集合/不同集合/无法确定+类似食物链】...
查看>>
L1-5. A除以B【一种输出格式错了,务必看清楚输入输出】
查看>>
Git一分钟系列--快速安装git客户端
查看>>
纵越6省1市-重新启动
查看>>
hive安装以及hive on spark
查看>>
jz1074 【基础】寻找2的幂
查看>>