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. idea生成springboot jpa的实体对象

    在idea的database里面添加上数据库 File-->Project Structure Modules--->点击加号----->选择JPA  选择确认之后再主面板上就会出现 ...

  2. Hive学习之路 (五)DbVisualizer配置连接hive

    一.安装DbVisualizer 下载地址http://www.dbvis.com/ 也可以从网上下载破解版程序,此处使用的版本是DbVisualizer 9.1.1 具体的安装步骤可以百度,或是修改 ...

  3. ajax原理和XmlHttpRequest对象

    Ajax的原理简单来说通过XmlHttpRequest对象来向服务器发异步请求,从服务器获得数据,然后用javascript来操作DOM而更新页面.这其中最关键的一步就是从服务器获得请求数据.要清楚这 ...

  4. scrapy运行方式

    1,在cmd 命令行下执行 scrapy crawl  demo (爬虫主逻辑的 name= 'demo '的名字) 2, 也可以在spider目录下添加一个py文件,加入以下代码 from scra ...

  5. web应用下的安全问题以及tomcat/nginx对应解决方法(持续更新、亲测可解决问题)

    最近一券商那边扫描反馈了下面几个非业务型安全漏洞,要求解决,如下: XSS 自己写个脚本response的时候对特殊字符进行了处理,或者网上搜下一堆(不要忘了回车.换行). HTML form wit ...

  6. ssh连接linux服务器不断开- "Write failed: Broken pipe"

    我自己用阿里云的服务器的时候,发现ssh连上以后,一会不用就断掉了,非常不方便,服务端的系统是ubuntu. 查了些东西,原来可以去配置服务端的sshd,或者客户端的ssh,就行了. 1,配置服务器端 ...

  7. 20145105 《Java程序设计》实验五总结

    实验五 JAVA网络编程 一.实验内容 用老师给的代码,实现服务器和客户端 和结对同学实现服务器与客户端的连接 客户端输入数据并加密,将密文发送给服务器,服务器接收密文并解密 二.实验步骤 用老师给的 ...

  8. 实验二Java面向对象程序设计

    一.单元测试 了解三种代码: 1.伪代码:类似于自然语言说明,描述实现逻辑思维 2.产品代码:程序员编辑的开发代码,要求可修改.可移植 3.测试代码:我理解是相当于开发软件在软件开放之前,程序员找到b ...

  9. 20165211 2017-2018-2 《Java程序设计》第7周学习总结

    20165211 2017-2018-2 <Java程序设计>第7周学习总结 教材学习内容总结 本周,我学习了书本上第十一章的内容,以下是我整理的主要知识. 第十一章 JDBC和MySQL ...

  10. Python3基础 str count 获得子字符串出现的次数

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...