POJ2689 Prime Distance
题意
Language: Prime Distance
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]中的所有质数筛出来。
预处理出中的所有质数,用类似Eratothenes的方法把合数划去。最后扫一遍就好了。
时间复杂度
代码
#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; }
静渊以有谋,疏通而知事。
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
· MQ 如何保证数据一致性?
· 《HelloGitHub》第 108 期