poj2689 Prime Distance(素数区间筛法)
题目链接:http://poj.org/problem?id=2689
题目大意:输入两个数L和U(1<=L<U<=2 147 483 647),要找出两个相邻素数C1和C2(L<=C1<C2<=U)是距离最小的,如果相邻素数不止一对,选择最初的,还要找出两个相邻的素数D1,和D2是距离最大的(同样在有多对的情况下选择最初的)
其中L<U,L和U的差不超过1 000 000
输入样例:
2 17
14 17
输出样例:
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.
解题思路:需要求出给定范围内区间所有的素数,然后再把素数间的最大距离、最小距离求出来。所以我们首先得用筛法筛出区间[L,U]范围内的素数。
因为数据区间超过的上界打到21亿,不能将所有小于21亿的素数存下来,不过我们发现区间的长度不超过1 000 000,使用筛法筛掉[L,U]区间的所有非素数,需要知道[L,U]区间的所有非素数的素数因子(因为一个非素数是被它最小的素因子筛掉)。2 147 483 647内的数或者是素数,能被根号2 147 483 647内的素数整除,也就是说,[L,U]区间的所有非素数的素数因子都在根号2 147 483 647内。
可以预先将根号2 147 483 647内的所有素数找出来,然后用这些素数去筛掉指定区间的所有非素数
代码:
#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=;
int prime[maxn];
int l,u,tot=;
bool isprime[];
void getprime(int N){
memset(prime,,sizeof(prime));
for(int i=;i<=N;i++){
if(!prime[i])
prime[tot++]=i;
for(int j=;j<tot&&prime[j]*i<=N;j++){
prime[i*prime[j]]=;
if(i%prime[j]==) break;
}
}
}
int main(){
getprime();
while(cin>>l>>u){
memset(isprime,true,sizeof(isprime));
for(int i=;i<tot;i++){
int a=(l-)/prime[i]+,b=u/prime[i];
for(int j=a;j<=b;j++)if(j>) isprime[j*prime[i]-l]=;
}
if(l==) isprime[]=; //注意特判l=1的情况
int pos=-,l1,r1,l2,r2,maxdis=,mindis=0x3f3f3f3f;
for(int i=;i<=u-l;i++){
if(isprime[i]){
if(pos==-) {
pos=i;
continue;
}
if(i-pos<mindis){
l1=l+pos;
r1=l+i;
mindis=i-pos;
}
if(i-pos>maxdis){
maxdis=i-pos;
l2=l+pos;
r2=l+i;
}
pos=i;
}
}
if(maxdis==) printf("There are no adjacent primes.\n");
else printf("%d,%d are closest, %d,%d are most distant.\n",l1,r1,l2,r2);
}
return ;
}
poj2689 Prime Distance(素数区间筛法)的更多相关文章
- POJ2689 - Prime Distance(素数筛选)
题目大意 给定两个数L和U,要求你求出在区间[L, U] 内所有素数中,相邻两个素数差值最小的两个素数C1和C2以及相邻两个素数差值最大的两个素数D1和D2,并且L-U<1,000,000 题解 ...
- POJ-2689-Prime Distance(素数区间筛法)
链接: https://vjudge.net/problem/POJ-2689 题意: The branch of mathematics called number theory is about ...
- POJ-2689 Prime Distance,区间素数筛法
Prime Distance 只会埃氏筛法的弱鸡今天读了读挑战程序设计120页,明白了求小区间内素数的方 ...
- 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 ...
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- POJ2689 Prime Distance(数论:素数筛选模板)
题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...
- poj2689(素数区间筛法模板)
题目链接: http://poj.org/problem?id=2689 题意: 给出一个区间 [l, r] 求其中相邻的距离最近和最远的素数对 . 其中 1 <= l < r < ...
- ZOJ 1842 Prime Distance(素数筛选法2次使用)
Prime Distance Time Limit: 2 Seconds Memory Limit: 65536 KB The branch of mathematics called nu ...
- poj2689 Prime Distance题解报告
题目戳这里 [题目大意] 给定一个区间[L,R],求区间内的质数相邻两个距离最大和最小的. [思路分析] 其实很简单呀,很明显可以看出来是数论题,有关于质数的知识. 要注意一下的就是L和R的数据范围都 ...
随机推荐
- maven推送本地包到私服
前置要求:配置正确的settings.xml maven 推送 本地jar 到私服的命令示例: mvn deploy:deploy-file -DgroupId=com.oracle -Dartifa ...
- 对calc()的研究
1.calc是英文单词calculate(计算)的缩写,是css3的一个新增的功能,用来指定元素的长度 calc()最大的好处就是用在流体布局上 2.calc()使用通用的数学运算规则 使用“+”.“ ...
- 021:自定义path(或url)转换器
1.实现如下需求:用户可以根据articles/list/方式获取文章,其中文章分类是采用如下分类,实例如下: 第一种:获取python分类下的文章:/article/python/ 第二种:获取py ...
- Python实例教程
转自:http://codingdict.com/article/9026 Python 100例-01 题目: 输有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数? Python 1 ...
- Linux 系统参数优化
修改系统所有进程可打开的文件数量 sysctl -w fs.file-max=2097152sysctl -w fs.nr_open=2097152 > vi /etc/sysctl.conff ...
- Python3解leetcode First Bad Version
问题描述: You are a product manager and currently leading a team to develop a new product. Unfortunately ...
- NOIp 基础数论知识点总结
推荐阅读 NOIp 数学知识点总结: https://www.cnblogs.com/greyqz/p/maths.html Basic 常用素数表:https://www.cnblogs.com/g ...
- [CSP-S模拟测试]:天才绅士少女助手克里斯蒂娜(数学+树状数组)
题目描述 红莉栖想要弄清楚楼下天王寺大叔的显像管电视对“电话微波炉(暂定)”的影响. 选取显像管的任意一个平面,一开始平面内有个$n$电子,初始速度分别为$v_i$,定义飘升系数为$$\sum \li ...
- [CSP-S模拟测试]:Rectangle(模拟+树状数组)
题目描述 平面上有$n$个点,第$i$个点的坐标为$X_i,Y_i$.对于其中的一个非空点集$S$,定义$f(S)$为一个最小矩形,满足:$\bullet$覆盖$S$中所有的点(在边界上也算覆盖):$ ...
- (转)【mysql元数据库】使用information_schema.tables查询数据库和数据表信息 ---数据记录大小统计
转:https://www.cnblogs.com/ssslinppp/p/6178636.html https://segmentfault.com/q/1010000007268994?_ea=1 ...