博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[POJ2689]Prime Distance
阅读量:4596 次
发布时间:2019-06-09

本文共 1459 字,大约阅读时间需要 4 分钟。

题目:Prime Distance

传送门:

分析:

1. $ [L,R] $ 只有1e6,于是暴力写了个millRabin测试,时间复杂度大约$ O(N*k*logN) $然后Tle。

2. 可以考虑一下二次筛法。$ [L,R] $ 中每一个合数一定包含一个不超过$ \sqrt n $ 的质因子。

3. 我们把$ [ 1,\sqrt R ] $ 的质数筛出来。

4. 再标记 $ i*p, i \in [ [\frac{L}{p}] , [\frac{R}{p}] ] $ ,就是把$ [L,R] $之间的合数筛掉。这个常数很小,时间复杂度$ O(\sum{\frac{R-L}{p}}) $ 。 

5. 剩下的就是质数啦。

 

#include 
#include
#include
#include
using namespace std;typedef long long LL;const int INF=1<<30,maxN=50005;int pn,p[maxN],vis[1000007];void getPrime(){ for(int i=2;i
0 && j<=(LL)R;j+=p[i])use[j]=1; } if(L==1)vis[0]=1;}int main(){ getPrime(); for(int L,R;~scanf("%d%d",&L,&R);){ int i=L,last=0,*use=vis-L;int mn0=0,mn1=INF,mx0=0,mx1=0; getPrimeInLR(L,R); for(;i<=R && !last;++i)if(!use[i])last=i; for(;i>0 && i<=R;++i) if(!use[i]){ if(i-last
mx1-mx0){mx1=i;mx0=last;} last=i; } if(!mn0)puts("There are no adjacent primes."); else printf("%d,%d are closest, %d,%d are most distant.\n",mn0,mn1,mx0,mx1); } return 0;}

 

素数筛的常数计算:

#include 
#include
#include
#include
using namespace std;typedef long long LL;const int INF=1<<30,maxN=50005;int pn,p[maxN],vis[1000007];void getPrime(){ for(int i=2;i
View Code

 

转载于:https://www.cnblogs.com/hjj1871984569/p/10367635.html

你可能感兴趣的文章
09-Python之迭代器,生成器
查看>>
Java逆向入门(一)
查看>>
泛型与非泛型代码性能比较
查看>>
杂项_眼见非实(ISCCCTF)
查看>>
代码审计_弱类型整数大小比较绕过
查看>>
PHP函数方法
查看>>
[译]你真的了解外边距折叠吗
查看>>
c#中IList<T>与List<T>
查看>>
python 多线程删除MySQL表
查看>>
ibatis报错
查看>>
SCN学习
查看>>
mysql的启动
查看>>
TCP端口状态说明ESTABLISHED、TIME_WAIT、 CLOSE_WAIT
查看>>
自己电脑能ping别人的,但别人电脑去不能跟我们的电脑通信
查看>>
制作自动化系统安装U盘
查看>>
python模块之xml.etree.ElementTree
查看>>
谷歌模拟
查看>>
【NOI2012】迷失游乐园
查看>>
postgresql 自定义排序
查看>>
任务就绪表OS_PrioGetHighest函数
查看>>