poj-2689-素数区间筛
Description
Your program is given 2 numbers: L and U (1<=L< U<=2,147,483,647), and you are to find the two adjacent primes C1 and C2 (L<=C1< C2<=U) that are closest (i.e. C2-C1 is the minimum). If there are other pairs that are the same distance apart, use the first pair. You are also to find the two adjacent primes D1 and D2 (L<=D1< D2<=U) where D1 and D2 are as distant from each other as possible (again choosing the first pair if there is a tie).
Input
Output
Sample Input
2 17
14 17
Sample Output
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
#define LL long long
#define mp make_pair
#define pb push_back
#define inf 0x3f3f3f3f
int maxn=;
vector<int>prime;
vector<int>p;
bool is[];
void init(){
is[]=is[]=;
for(LL i=;i<=maxn;++i){
if(!is[i]) prime.push_back(i);
for(LL j=;j<prime.size()&&i*prime[j]<=maxn;j++){
is[i*prime[j]]=;
if(i%prime[j]) break;
}
}
}
void solve(LL L,LL R){
p.clear();
memset(is,,sizeof(is));
for(LL i=;i<prime.size()&&1LL*prime[i]*prime[i]<=R;i++){
LL s=L/prime[i]+(L%prime[i]>);
if(s==)s=;
for(LL j=s;j*prime[i]<=R;j++){
if(j*prime[i]>=L) is[j*prime[i]-L]=;
}
}
for(int i=;i<=R-L;i++){
if(!is[i]&&i+L>=) p.push_back(i+L);
}
}
int main(){
LL L,R;
init();
while(scanf("%lld%lld",&L,&R)!=EOF){
solve(L,R); if(p.size()<) puts("There are no adjacent primes.");
else{
int c1,c2,m1,m2;
c1=m1=p[];
c2=m2=p[];
for(int i=;i<p.size();++i){
if(p[i]-p[i-]<c2-c1){
c1=p[i-];
c2=p[i];
}
if(p[i]-p[i-]>m2-m1){
m1=p[i-];
m2=p[i];
}
}
printf("%d,%d are closest, %d,%d are most distant.\n",c1,c2,m1,m2);
}
}
return ;
}
poj-2689-素数区间筛的更多相关文章
- poj2689(素数区间筛法模板)
题目链接: http://poj.org/problem?id=2689 题意: 给出一个区间 [l, r] 求其中相邻的距离最近和最远的素数对 . 其中 1 <= l < r < ...
- poj 2689 Prime Distance(大区间筛素数)
http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- 大区间素数筛选(POJ 2689)
/* *POJ 2689 Prime Distance *给出一个区间[L,U],找出区间内容.相邻的距离最近的两个素数和距离最远的两个素数 *1<=L<U<=2147483647 ...
- POJ 2689.Prime Distance-区间筛素数
最近改自己的错误代码改到要上天,心累. 这是迄今为止写的最心累的博客. Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ - 2689 Prime Distance (区间筛)
题意:求[L,R]中差值最小和最大的相邻素数(区间长度不超过1e6). 由于非素数$n$必然能被一个不超过$\sqrt n$的素数筛掉,因此首先筛出$[1,\sqrt R]$中的全部素数,然后用这些素 ...
- 素数筛 poj 2689
素数筛 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; ...
- poj 2689 Prime Distance (素数二次筛法)
2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...
- lightoj1197 素数双筛,可以参考poj的那题双筛
/* 判断一个数是否是素数,只要判断这个数有没有在[2,sqrt(n)]区间的因子 同样,对于大数短区间的筛选,同样可以用这种判断方式, 先筛出sqrt(n)范围内的素数,然后用这些素数去筛出区间内的 ...
- POJ 2689 筛法求素数
DES:给出一个区间[L, U].找出这个区间内相邻的距离最近的两个素数和距离最远的两个素数.其中1<=L<U<=2147483647 区间长度不超过1000000. 思路:因为给出 ...
随机推荐
- 剥开比原看代码10:比原是如何通过/create-key接口创建密钥的
作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...
- pgAdmin的数据恢复
DOC 本地添加server 1.设置备份.恢复的exe路径.一般在pgAdmin的安装路径下可以找到 2.恢复restore,备份backup
- 在 2016 年学 JavaScript 是一种什么样的体验?(React从入门到放弃)
jquery 年代 vs 前端模块化 http://blog.csdn.net/offbye/article/details/52793921 ++ 嘿,我最近接到一个 Web 项目,不过老实说,我这 ...
- Java方法中捕获多个异常的处理机制
/** * @author wangyunhan * @throws Exception */ public static void main(String[] argßs) throws Excep ...
- 【Java】【异常】
java中2种方法处理异常:1.在发⽣异常的地方直接处理:2.将异常抛给调用者,让调⽤者处理.异常分类1.检查性异常: java.lang.Exception2.运⾏期异常: java.lang.Ru ...
- go 单进程并发
demo1 // This sample program demonstrates how to create goroutines and // how the scheduler behaves. ...
- Springboot:没有src/main/resources目录(引入图片时(或静态资源时)发现没有该目录)
今天想在Springboot项目想引进静态资源时,发现自己的项目里没有教程里面的 src/main/resources这个目录,目录如下 我的项目目录结构是这样的,但是还不是很清楚下面的那个src目录 ...
- 写了一个Hy的vscode语法高亮插件
-------2018 8 3----------- 把函数名和参数改了,正则有点古怪,参考自带的lambda表达式才搞定 但彩色括号走了弯路,各种配图有彩色括号的插件其实很少是自己搞的,其实只要再装 ...
- jenkins之从0到1利用Git和Ant插件打war包并自动部署到tomcat(第四话):把war包远程部署到tomcat
上一节介绍了如何用Ant插件来打war包,本节接着介绍如何把build好的war自动部署到tomcat中 1.先安装 Deploy to container Plugin插件 2. 在构建后操作中添加 ...
- seo中的竞价排名是什么
seo中的竞价排名是什么 一.总结 一句话总结:竞价排名的基本特点是按点击付费,推广信息出现在搜索结果中(一般是靠前的位置),如果没有被用户点击,则不收取推广费. 搜索引擎的一种推广广告的方式 1.竞 ...