Prime Distance POJ - 2689 线性筛
一个数 $n$ 必有一个不超过 $\sqrt n$ 的质因子。
打表处理出 $1$ 到 $\sqrt n$ 的质因子后去筛掉属于 $L$ 到 $R$ 区间的素数即可。
Code:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int Range=50000;
const int N=1000000+233;
int f[N],vis[Range+233],prime[Range];
int cnt;
void get_prime(){
for(int i=2;i<=Range;++i){
if(!vis[i])prime[++cnt]=i;
for(int j=1;j<=cnt&&prime[j]*i<=Range;++j){
vis[prime[j]*i]=1;
if(i%prime[j]==0)break;
}
}
}
int main(){
get_prime();
int L,U;
while(scanf("%d%d",&L,&U)!=EOF){
memset(f,0,sizeof(f));
if(L==1)L=2;
for(int i=1;i<=cnt;++i){
int a=L%prime[i]==0?L/prime[i]:L/prime[i]+1;
int b=U/prime[i];
for(int j=a;j<=b;++j)if(j>1)f[j*prime[i]-L]=1;
}
int p=-1,x1,x2,maxans=-1,minans=N,y1,y2;
for(int i=0;i<=U-L;++i)
if(f[i]==0){
if(p==-1){p=i;continue;};
if(maxans<i-p){maxans=i-p,x1=p+L,x2=i+L;}
if(minans>i-p){minans=i-p,y1=p+L,y2=i+L;}
p=i;
}
if(maxans==-1)
cout<<"There are no adjacent primes."<<endl;
else cout<<y1<<","<<y2<<" are closest, "<<x1<<","<<x2<<" are most distant."<<endl;
}
return 0;
}
Prime Distance POJ - 2689 线性筛的更多相关文章
- Prime Distance POJ - 2689 (数学 素数)
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- Sum of Consecutive Prime Numbers POJ - 2739 线性欧拉筛(线性欧拉筛证明)
题意:给一个数 可以写出多少种 连续素数的合 思路:直接线性筛 筛素数 暴力找就行 (素数到n/2就可以停下了,优化一个常数) 其中:线性筛的证明参考:https://blog.csdn.net ...
- POJ-2689 Prime Distance (两重筛素数,区间平移)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13961 Accepted: 3725 D ...
- 题解报告:poj 2689 Prime Distance(区间素数筛)
Description The branch of mathematics called number theory is about properties of numbers. One of th ...
- Prime Distance(二次筛素数)
Description The branch of mathematics called number theory is about properties of numbers. One of th ...
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- poj 2689 Prime Distance (素数二次筛法)
2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...
- POJ 3126 - Prime Path - [线性筛+BFS]
题目链接:http://poj.org/problem?id=3126 题意: 给定两个四位素数 $a,b$,要求把 $a$ 变换到 $b$.变换的过程每次只能改动一个数,要保证每次变换出来的数都是一 ...
- 数论 - 素数的运用 --- poj 2689 : Prime Distance
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12512 Accepted: 3340 D ...
随机推荐
- bzoj 1207 [HNOI2004]打鼹鼠 小技巧
Description 鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的.根据这个特点阿Q编写了一个打鼹鼠的游戏:在一个n*n的网格中,在某些时刻鼹鼠会在某一个网格探 ...
- Linux内核crash/Oops异常定位分析方法
在内核开发的过程中,经常会碰到内核崩溃,比如空指针异常,内存访问越界.通常我们只能靠崩溃之后打印出的异常调用栈信息来定位crash的位置和原因.总结下分析的方法和步骤. 通常oops发生之后,会在串口 ...
- 在小程序中实现全局混入,以混入的形式扩展小程序的api
GitHub: https://github.com/WozHuang/mp-extend 相关文章: 小程序全局状态管理,在页面中获取globalData和使用globalSetData 通过页面预 ...
- IntelliJ IDEA 2017.1.6 x64 的破解
方式一 现在用这个 http://idea.imsxm.com/好使 步骤如下,点击help按钮,选择Register 点击license server 修改下面的服务器激活地址 方式二 由于Je ...
- 为DK2应用程序迁移到Oculus CV1做好准备!
为DK2应用程序迁移到Oculus CV1做好准备! 本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/arti ...
- 【CodeForces 987C】Three displays
[链接] 我是链接,点我呀:) [题意] [题解] 动态规划 设dp[i][j]表示前i个数字,选了j个的最小花费. dp[i][j] = min(dp[k][j-1]+b[i]);//其中a[i]& ...
- Python - 采用 contextmanage 简化代码
contextlib.contextmanage Python 2.7 documents: https://docs.python.org/2.7/library/contextlib.html?h ...
- 2015 Multi-University Training Contest 8 hdu 5383 Yu-Gi-Oh!
Yu-Gi-Oh! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: ...
- Oracle-查看用户对象信息
--视图(可查看拥有者.对象名称.创建时间.上次修改时间) SELECT t.OBJECT_NAME, t.CREATED, t.LAST_DDL_TIME FROM user_objects t o ...
- STL之效率比較
1.vector 变长一维数组,连续存放的内存块,有保留内存.堆中分配内存: 支持[]操作,高效率的随机訪问: 在最后添加元素时,一般不须要分配内存空间,速度快:在中间或開始操作元素时要进行内存拷贝效 ...