2018.09.01 poj2689 Prime Distance(埃式筛法)
传送门
一道挺有趣的。
第一眼以为每个数都用miller_rabin判一次,但感觉会被卡时间啊。
继续分析发现可以晒出sqrt(r)中的所有素数,然后用类似埃式筛法的方法晒出[l,r]" role="presentation" style="position: relative;">[l,r][l,r]之间的素数。
然后就没了。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 1000005
#define ll long long
using namespace std;
bool f[N];
ll pri[150005],vis[150005],tot=0;
inline void init(ll len){
for(ll i=2;i<=len;++i){
if(!vis[i])pri[++tot]=i;
for(ll j=1;j<=tot;++j){
ll k=pri[j]*i;
if(k>len)break;
vis[k]=1;
if(i%pri[j]==0)break;
}
}
}
inline void check(ll l,ll r){
memset(f,1,sizeof(f));
if(l==1)f[0]=0;
for(ll i=1;pri[i]*pri[i]<=r;++i){
ll tmp=pri[i];
for(ll j=(l/tmp)*tmp;j<=r;j+=tmp){
if(j<l||j==tmp)continue;
f[j-l]=0;
}
}
}
int main(){
init(50000);
ll l,r;
while(~scanf("%lld%lld",&l,&r)){
check(l,r);
ll cnt=0,tmp,mx=0,mn=1e9,pos,mx1,mx2,mn1,mn2;
for(ll i=0;i<=r-l;++i){
if(f[i]){
++cnt;
if(cnt>=2){
tmp=i-pos;
if(tmp>mx)mx=tmp,mx1=pos,mx2=i;
if(tmp<mn)mn=tmp,mn1=pos,mn2=i;
}
pos=i;
}
}
if(cnt>=2)printf("%lld,%lld are closest, %lld,%lld are most distant.\n",mn1+l,mn2+l,mx1+l,mx2+l);
else printf("There are no adjacent primes.\n");
}
return 0;
}
2018.09.01 poj2689 Prime Distance(埃式筛法)的更多相关文章
- POJ-2689 Prime Distance,区间素数筛法
Prime Distance 只会埃氏筛法的弱鸡今天读了读挑战程序设计120页,明白了求小区间内素数的方 ...
- poj2689 Prime Distance题解报告
题目戳这里 [题目大意] 给定一个区间[L,R],求区间内的质数相邻两个距离最大和最小的. [思路分析] 其实很简单呀,很明显可以看出来是数论题,有关于质数的知识. 要注意一下的就是L和R的数据范围都 ...
- POJ2689 Prime Distance(数论:素数筛选模板)
题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...
- POJ-2689 Prime Distance (两重筛素数,区间平移)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13961 Accepted: 3725 D ...
- 解题报告:poj2689 Prime Distance
2017-10-03 11:29:20 writer:pprp 来源:kuangbin模板 从已经筛选好的素数中筛选出规定区间的素数 /* *prime DIstance *给出一个区间[L,U],找 ...
- POJ 2689 - Prime Distance - [埃筛]
题目链接:http://poj.org/problem?id=2689 Time Limit: 1000MS Memory Limit: 65536K Description The branch o ...
- POJ2689:Prime Distance(大数区间素数筛)
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- POJ2689 - Prime Distance(素数筛选)
题目大意 给定两个数L和U,要求你求出在区间[L, U] 内所有素数中,相邻两个素数差值最小的两个素数C1和C2以及相邻两个素数差值最大的两个素数D1和D2,并且L-U<1,000,000 题解 ...
- POJ2689 Prime Distance 质数筛选
题目大意 求区间[L, R]中距离最大和最小的两对相邻质数.R<2^31, R-L<1e6. 总体思路 本题数据很大.求sqrt(R)的所有质数,用这些质数乘以j, j+1, j+2... ...
随机推荐
- JAVA时间进行比较和转换,时间加减得到天数
转自:https://blog.csdn.net/iteye_8535/article/details/82246006 JAVA时间进行比较和转换,时间加减得到天数 1. 把时间类型的字符串转为DA ...
- FindWindow 查找窗口
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As ...
- EOF与子过程返回
在2000及其以上系统,P处理语句GOTO新增了:EOF系统标签,意思是移动到当前P处理文件的结尾,EOF==END OF FILE的缩写,意为文件结尾,主要表现形式为:GOTO :EOFOR ...
- 可视化库-seaborn-热力图(第五天)
1. 画一个基本的热力图, 通过热力图用来观察样本的分布情况 import matplotlib.pyplot as plt import numpy as np np.random.seed(0) ...
- libcur+openssl的编译,使之支持SSL<转>
本机环境: Visual Studio 2010 . Windows 7 64 bit 1: 下载文件 1.1 libcurl: curl-7.49.1.zip 地址: https://curl.ha ...
- idea常用设置(持续更新)
1.注释模板 Setting里找到live Templates (1)创建一个Templates Group (2)在Templates Group下创建Live Template 2.常用内置模板 ...
- SRC是在本位置显示:source的缩写,源的意思 HREF是点击后连接的目标:HyperlinkReference,超链接引用
SRC是在本位置显示:source的缩写,源的意思HREF是点击后连接的目标:HyperlinkReference,超链接引用
- unity 查找脚本被场景中哪些对象引用
在需要查找的脚本上右键: 在场景中已经显示出所有引用该脚本的对象
- JAVA中的异常疑点解析
1 final, finally, finalize的区别. final 用于声明属性,方法和类,分别表示属性不可变,方法不可覆盖,类不可继承. 内部类要访问局部变量,局部变量必须定义成final类型 ...
- python -m
影响sys.path python xxx.py python -m xxx.py 这是两种加载py文件的方式:1叫做直接运行2把模块当作脚本来启动 直接启动是把脚本所在的目录放到了sys.path属 ...