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. 思路:因为给出 ...
随机推荐
- 良品铺子:“新零售”先锋的IT必经之路
良品铺子:“新零售”先锋的IT必经之路 云计算 大数据 CIO班 CIO 互联网+ 物联网 电子政务 2017-12-29 09:25:34 来源:互联网抢沙发 摘要:2017年被称为“新零售”元年 ...
- HDU 3400 Line belt (三分套三分)
http://acm.split.hdu.edu.cn/showproblem.php?pid=3400 题意: 有两条带子ab和cd,在ab上的速度为p,在cd上的速度为q,在其它地方的速度为r.现 ...
- Ajax - 发送请求原理
1,什么是ajax? Asynchronous JavaScript and XML(当然现在xml已经由json代替): 主要是用于前后台的交互(表单提交已经被废弃): 使用场景:前台获取数据.表单 ...
- python学习 day018打卡 反射
本节主要内容: 1.isinstance,type,issubclass 2.区分函数和方法 3.反射(重点) 一.isinstance,type,issubclass issubclass():判断 ...
- 理解 Redis(7) - Set 值
unordered collection of unique strings.set值是唯一的字符串的无序集合, 把握住两个特点: 唯一, 无序. 清空所有的数据, 并清理显示界面: 127.0.0. ...
- Oracle 12C ORA-65096: 公用用户名或角色名无效
先说基本用法: 先按11G之前进行 conn / as sysdba; create user test identifed by test; ORA-65096: 公用用户名或角色名无效. 查官方文 ...
- [osg]OSG使用更新回调来更改模型
使用回调类实现对场景图形节点的更新.本节将讲解如何使用回调来实现在每帧的更新遍历(update traversal)中进行节点的更新. 回调概览 用户可以使用回调来实现与场景 ...
- 搞定'express' 不是内部或外部命令,也不是可运行的程序或批处理文件
1 -- 官方下nodejs,一路next完成, node -v 没问题 可以检测到版本号,node环境ok~ 2-- 本地 安装express :npm install express -g 检测 ...
- bootstrap的渲染机制
bootstrap的渲染机制. http://www.cnblogs.com/djtao/p/5942620.html 源码解析: http://www.cnblogs.com/ahole/p/588 ...
- VIM编辑器和VI编辑器的区别
vi 和vim 的区别 写在前面:这个两个"东西"着实让我烦恼一阵子,但是自己一直没当回事,但是遇到了好几次再决定彻底把他们搞的明白,一下是我通过查找资料了解到的关于这两个编辑器的 ...