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. MySQL数据库----表与表之间的关系

    表1 foreign key 表2 则表1的多条记录对应表2的一条记录,即多对一 利用foreign key的原理我们可以制作两张表的多对多,一对一关系 多对多: 表1的多条记录可以对应表2的一条记录 ...

  2. Redis设置密码重启后失效的解决方案

    原因可能有两个: 1.只是单纯的通过命令行设置了密码,这种设置方式是临时的,当服务器重启后,密码会失效. config set requirepass yourPassword 解决方案:在redis ...

  3. jquery中的load方法加载页面无法缓存问题

    在A页面中调用JQuery中的load方法,加载另一个B页面,B页面中的样式文件和JS文件无法从浏览器缓存中获取,每次都是实时获取.这是因为B页面的HTML经load方法处理后,会为每个样式和JS文件 ...

  4. C++设计模式 之 “单一职责”模式:Decorator、Bridge

    part 1 “单一职责”模式 在软件组件的设计中,如果责任划分的不清晰,使用继承得到的结果往往是随着需求的变化,子类急剧膨胀,同时充斥着重复代码,这时候的关键是划清责任. 典型模式 Decorato ...

  5. 04:sqlalchemy操作数据库

    目录: 1.1 ORM介绍(作用:不用原生SQL语句对数据库操作) 1.2 安装sqlalchemy并创建表 1.3 使用sqlalchemy对表基本操作 1.4 一对多外键关联 1.5 sqlalc ...

  6. oracle一些工作笔记

    表空间: oracle表空间对应的数据文件: SELECT t1.name, t2.name FROM v$tablespace t1, v$datafile t2 WHERE t1.ts#=t2.t ...

  7. 20145317彭垚 MSF基础应用

    20145317彭垚 MSF基础应用 基础问题回答 用自己的话解释什么是exploit,payload,encode? exploit就相当于是载具,将真正要负责攻击的代码传送到靶机中,我觉得老师上课 ...

  8. 20165310 Java实验五《网络编程与安全》

    20165310 Java实验五<网络编程与安全> 任务一 题目:①编写MyBC.java实现中缀表达式转后缀表达式的功能:②编写MyDC.java实现从上面功能中获取的表达式中实现后缀表 ...

  9. 使用node连接MongoDB数据 综本地及linux服务器记

    gitee地址 启动mongo D:\MongoDB> ./bin/mongod --dbpath ./data/db MongoDB 提供了简单的 HTTP 用户界面. 如果你想启用该功能,需 ...

  10. 如何高效判断java数组是否包含某个值

    在java中,我们如何判断一个未排序数组中是否包含一个特定的值?这在java中是一个频繁非常实用的操作.那么什么样的方法才是最高效的方式?当然 ,这个问题在Stack Overflow也是得票率非常高 ...