Prime Distance
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13961   Accepted: 3725

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. 题目大意:给一个区间,找出这个区间内相邻的两个素数中差最大和最小的两个。
题目解析:两种方法,一种是 枚举 + Miller_Rabbin快速判定素数,另一种是将标记数组区间平移,两次筛素数。 第一种方法:
 # include<iostream>
# include<cstdio>
# include<cstring>
# include<cstdlib>
# include<algorithm>
using namespace std;
# define ll long long
unsigned mypow(unsigned a,unsigned b,unsigned m)
{
if(b==)
return ;
if(b==)
return a%m;
ll temp=mypow(a,b/,m);
temp*=temp;
temp%=m;
if(b&)
temp*=a;
temp%=m;
return temp;
}
bool Miller_Rabbin(unsigned x)
{
if(x==)
return true;
for(int i=;i<=;++i){
unsigned a=rand()%(x-)+;
if(mypow(a,x-,x)!=)
return false;
}
return true;
}
int main()
{
unsigned a,b;
unsigned l1,l2,r1,r2,t;
int minn,maxn;
while(scanf("%u%u",&a,&b)!=EOF)
{
if(a==)
a=;
minn=;
maxn=;
l1=l2=r1=r2=a;
bool yy=true;
for(int i=a;i<=b;++i){
if(Miller_Rabbin(i)){
if(yy){
t=l1=r1=a;
yy=false;
}
else{
if(minn>i-t){
minn=i-t;
l1=t;
l2=i;
}
if(maxn<i-t){
maxn=i-t;
r1=t;
r2=i;
}
}
t=i;
}
}
if(maxn==){
printf("There are no adjacent primes.\n");
}else
printf("%u,%u are closest, %u,%u are most distant.\n",l1,l2,r1,r2);
}
return ;
}

这种暴力的方法效率不高,在UVa上取30个随机数能AC,在ZOJ上取20个随机数能AC,但在POJ上无论如何都AC不了。原因就是这三个OJ对时间的要求分别是3s,2s,1s。

下面是两重筛的实现。标记数组用的很灵活。

 # include<iostream>
# include<cstdio>
# include<cmath>
# include<map>
# include<vector>
# include<cstring>
# include<algorithm>
using namespace std;
const int N=;
int pri[N],mark[],cnt;
vector<unsigned>v;
void init()
{
cnt=;
fill(mark,mark+N+,);
for(int i=;i<=N;++i){
if(mark[i])
pri[cnt++]=i;
for(int j=;j<cnt&&i*pri[j]<=N;++j){
mark[i*pri[j]]=;
if(i%pri[j]==)
break;
}
}
}
void work(unsigned a,unsigned b)
{
v.clear();
if(a==)
++a;
memset(mark,,sizeof(mark));
for(int i=;i<cnt;++i){
if(pri[i]>b)
break;
for(int c=a/pri[i];c*pri[i]<=b;++c){
if(c<=)
continue;
if(c*pri[i]<a)
continue;
mark[c*pri[i]-a]=;
}
}
for(int i=;i<=b-a;++i){
if(mark[i]==)
v.push_back(i+a);
}
}
void solve()
{
int l=v.size();
if(l<){
printf("There are no adjacent primes.\n");
return ;
}
int minn=<<,maxn=;
unsigned l1,l2,r1,r2;
for(int i=;i<l;++i){
if(minn>v[i]-v[i-]){
minn=v[i]-v[i-];
l1=v[i-];
l2=v[i];
}
if(maxn<v[i]-v[i-]){
maxn=v[i]-v[i-];
r1=v[i-];
r2=v[i];
}
}
printf("%u,%u are closest, %u,%u are most distant.\n",l1,l2,r1,r2);
}
int main()
{
init();
unsigned a,b;
while(scanf("%u%u",&a,&b)!=EOF)
{
work(a,b);
solve();
}
return ;
}

POJ-2689 Prime Distance (两重筛素数,区间平移)的更多相关文章

  1. [ACM] POJ 2689 Prime Distance (筛选范围大素数)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12811   Accepted: 3420 D ...

  2. poj 2689 Prime Distance(大区间素数)

    题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...

  3. poj 2689 Prime Distance (素数二次筛法)

    2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...

  4. poj 2689 Prime Distance(大区间筛素数)

    http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...

  5. 题解报告:poj 2689 Prime Distance(区间素数筛)

    Description The branch of mathematics called number theory is about properties of numbers. One of th ...

  6. 数论 - 素数的运用 --- poj 2689 : Prime Distance

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12512   Accepted: 3340 D ...

  7. POJ - 2689 Prime Distance (区间筛)

    题意:求[L,R]中差值最小和最大的相邻素数(区间长度不超过1e6). 由于非素数$n$必然能被一个不超过$\sqrt n$的素数筛掉,因此首先筛出$[1,\sqrt R]$中的全部素数,然后用这些素 ...

  8. poj 2689 Prime Distance(区间筛选素数)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9944   Accepted: 2677 De ...

  9. POJ 2689 Prime Distance (素数+两次筛选)

    题目地址:http://poj.org/problem?id=2689 题意:给你一个不超过1000000的区间L-R,要你求出区间内相邻素数差的最大最小值,输出相邻素数. AC代码: #includ ...

随机推荐

  1. Eclipse出现&quot;Running Android Lint has encountered a problem&quot;解决方案

    安装eclipse for android 时候的错误记录,转载自:http://blog.csdn.net/chenyufeng1991/article/details/47442555 (1)打开 ...

  2. 光盘 iso 镜像制作相关命令操作

    1. 安装制作工具 mkisofs yum install mkisofs -y 2. Linux 操作系统镜像 iso 打包 mkisofs -o /root/.iso \ -V mini7 -b ...

  3. android贪吃蛇(超级简陋版)

    public class body { public int ax;//代表X周变量 public int ay;//代表Y轴变量 public int getAx() { return ax; } ...

  4. C++STL之迭代器

    迭代器 迭代器提供对一个容器中的对象的访问方法,并且定义了容器中对象的范围.迭代器就如同一个指针.事实上,C++的指针也是一种迭代器.但是,迭代器不仅仅是指针,因此你不能认为他们一定具有地址值.例如, ...

  5. EF Code First 学习笔记:关系

      一对多关系 项目中最常用到的就是一对多关系了.Code First对一对多关系也有着很好的支持.很多情况下我们都不需要特意的去配置,Code First就能通过一些引用属性.导航属性等检测到模型之 ...

  6. HTML相对路径 当前目录、上级目录、根目录、下级目录表示法

    文件引用时经常需要用到相对目录.如(js,css,图片等) "./"  ---------       源代码所在的当前目录(可省略) "../"  ----- ...

  7. Elastarchsearch安装搭建(一)

    Elasticsearch是一个实时分布式搜索和分析引擎.一个基于Apache Lucene(TM)的开源搜索引擎.无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进.性能最好的.功能最全 ...

  8. human pose estimation

    2D Pose estimation主要面临的困难:遮挡.复杂背景.光照.真实世界的复杂姿态.人的尺度不一.拍摄角度不固定等. 单人姿态估计 传统方法:基于Pictorial Structures, ...

  9. Redis——Linux(centos7.x)下Redi和PHP Redis插件安装——【一】

    Redis 安装 下载地址:http://redis.io/download,下载最新文档版本. $ wget http://download.redis.io/releases/redis-4.0. ...

  10. tensorFlow(四)浅层神经网络

    tensorFlow见基础 实验 MNIST数据集介绍 MNIST是一个手写阿拉伯数字的数据集. 其中包含有60000个已经标注了的训练集,还有10000个用于测试的测试集. 本次实验的任务就是通过手 ...