POJ2689 Prime Distance

题意

Language:
Prime Distance
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 25344Accepted: 6620

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.

Source

分析

不可能预处理出所有质数,由于U-L很小,所以考虑把[L,U]中的所有质数筛出来。

预处理出U中的所有质数,用类似Eratothenes的方法把合数划去。最后扫一遍就好了。

时间复杂度O(UloglogU+(UL)loglogU)

代码

#include<iostream>
#include<vector>
#include<cstring>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;
rg char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') w=-1;
ch=getchar();
}
while(isdigit(ch))
data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x){
return x=read<T>();
}
typedef long long ll;
using namespace std;
co int N=100006,L=1000006,M=46340,INF=0x7fffffff;
bool v[L];
vector<int> p,ans;
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
memset(v,1,sizeof v);
for(int i=2;i<=M;++i)
if(v[i]){
p.push_back(i);
for(int j=2;j<=M/i;++j) v[i*j]=0;
}
unsigned l,r;
while(~scanf("%d%d",&l,&r)){
memset(v,1,sizeof v);
ans.clear();
if(l==1) v[0]=0;
for(unsigned i=0;i<p.size();++i)
for(unsigned j=(l-1)/p[i]+1;j<=r/p[i];++j)
if(j>1) v[p[i]*j-l]=0;
for(unsigned i=l;i<=r;++i)
if(v[i-l]) ans.push_back(i);
int minn=INF,maxx=0,x1,y1,x2,y2;
for(unsigned i=0;i+1<ans.size();++i){
int num=ans[i+1]-ans[i];
if(num<minn) minn=num,x1=ans[i],y1=ans[i+1];
if(num>maxx) maxx=num,x2=ans[i],y2=ans[i+1];
}
if(!maxx) puts("There are no adjacent primes.");
else printf("%d,%d are closest, %d,%d are most distant.\n",x1,y1,x2,y2);
}
return 0;
}

posted on   autoint  阅读(130)  评论(0编辑  收藏  举报

编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
· MQ 如何保证数据一致性?
· 《HelloGitHub》第 108 期

导航

点击右上角即可分享
微信分享提示