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... ...
随机推荐
- django celery 定时任务
可参考上一篇:http://www.cnblogs.com/wumingxiaoyao/p/8515075.html 1. 安装 django-celery-beat pip3 install dja ...
- java ee7 -- Java Bean验证
针对对象.对象成员.方法.构造函数的数据验证. 1. 一个验证的小例子 (1) 添加引用jar <dependency> <groupId>org.hibernate.vali ...
- as3 去掉字符串空白问题
去掉内容的所有空白 function trim(str:String):String { })/g,""); } //[ ]内是一个中文空格一个英文空格 {}是说匹配一个到多个 / ...
- ABAP-动态编程
转载:http://www.cnblogs.com/jiangzhengjun/p/4293407.html 动态编程 动态的基本语法 多种不同的动态编程 动态字段 动态类型 指定结构.内表组件字段的 ...
- WINFORM小列子参考
1.用树型列表动态显示菜单 密码:zx9t 二.Popup窗口提醒(桌面右下角出现) 密码:cjjo 三.台历 密码:nq4m 四.文件复制 密码:lsqj 五.进度条 密码:byti 六 ...
- ios 给图片加文字
- (UIImage*) drawText:(NSString*)text inImage:(UIImage*)image { //prepare image context UIGraphicsBe ...
- @Transactionl注解
spring 事务注解 默认遇到throw new RuntimeException("...");会回滚 需要捕获的throw new Exception("...&q ...
- js中定时器之一
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- MongoDB 集合命令
集合命令 创建语法如下 name是要创建的集合的名称 options是一个文档,用于指定集合的配置,选项参数是可选的,所以只需要到指定的集合名称 可以不手动创建集合,向不存在的集合中第一次加入数据 ...
- global statement
[global statement] 在线程里,默认所有变量都是本线程局部变量,要想访问全局变量,则要先用global声明. 如全局有变量totalCount,线程中有语句 totalCount += ...