Description

The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no proper factors (it is only evenly divisible by 1 and itself). The first prime numbers are 2,3,5,7 but they quickly become less frequent. One of the interesting questions is how dense they are in various ranges. Adjacent primes are two numbers that are both primes, but there are no other prime numbers between the adjacent primes. For example, 2,3 are the only adjacent primes that are also adjacent numbers. 
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

Each line of input will contain two positive integers, L and U, with L < U. The difference between L and U will not exceed 1,000,000.

Output

For each L and U, the output will either be the statement that there are no adjacent primes (because there are less than two primes between the two given numbers) or a line giving the two pairs of adjacent primes.

Sample Input

2 17
14 17

Sample Output

2,3 are closest, 7,11 are most distant.
There are no adjacent primes.

Source

    给出[L,R],求区间内的素数,R<=2147483647,R-L<=1000000, 注意到只用sqrt(R)以内的素数就可以筛出[L,R]里面的素数,
可以先对sqrt(MAX_INT)内的素数打一个表。对于[L,R]的询问,用小于等于sqrt(R)的素数筛一下然后统计一下就好了。注意L<2的时候
要特判一下否则容易把1也给打进去。
    

 #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-素数区间筛的更多相关文章

  1. poj2689(素数区间筛法模板)

    题目链接: http://poj.org/problem?id=2689 题意: 给出一个区间 [l, r] 求其中相邻的距离最近和最远的素数对 . 其中 1 <= l <  r < ...

  2. poj 2689 Prime Distance(大区间筛素数)

    http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...

  3. poj 2689 Prime Distance(大区间素数)

    题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...

  4. 大区间素数筛选(POJ 2689)

    /* *POJ 2689 Prime Distance *给出一个区间[L,U],找出区间内容.相邻的距离最近的两个素数和距离最远的两个素数 *1<=L<U<=2147483647 ...

  5. POJ 2689.Prime Distance-区间筛素数

    最近改自己的错误代码改到要上天,心累. 这是迄今为止写的最心累的博客. Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  6. POJ - 2689 Prime Distance (区间筛)

    题意:求[L,R]中差值最小和最大的相邻素数(区间长度不超过1e6). 由于非素数$n$必然能被一个不超过$\sqrt n$的素数筛掉,因此首先筛出$[1,\sqrt R]$中的全部素数,然后用这些素 ...

  7. 素数筛 poj 2689

    素数筛 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; ...

  8. poj 2689 Prime Distance (素数二次筛法)

    2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...

  9. lightoj1197 素数双筛,可以参考poj的那题双筛

    /* 判断一个数是否是素数,只要判断这个数有没有在[2,sqrt(n)]区间的因子 同样,对于大数短区间的筛选,同样可以用这种判断方式, 先筛出sqrt(n)范围内的素数,然后用这些素数去筛出区间内的 ...

  10. POJ 2689 筛法求素数

    DES:给出一个区间[L, U].找出这个区间内相邻的距离最近的两个素数和距离最远的两个素数.其中1<=L<U<=2147483647 区间长度不超过1000000. 思路:因为给出 ...

随机推荐

  1. 良品铺子:“新零售”先锋的IT必经之路

    良品铺子:“新零售”先锋的IT必经之路 云计算 大数据 CIO班 CIO 互联网+ 物联网 电子政务 2017-12-29 09:25:34  来源:互联网抢沙发 摘要:2017年被称为“新零售”元年 ...

  2. HDU 3400 Line belt (三分套三分)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=3400 题意: 有两条带子ab和cd,在ab上的速度为p,在cd上的速度为q,在其它地方的速度为r.现 ...

  3. Ajax - 发送请求原理

    1,什么是ajax? Asynchronous JavaScript and XML(当然现在xml已经由json代替): 主要是用于前后台的交互(表单提交已经被废弃): 使用场景:前台获取数据.表单 ...

  4. python学习 day018打卡 反射

    本节主要内容: 1.isinstance,type,issubclass 2.区分函数和方法 3.反射(重点) 一.isinstance,type,issubclass issubclass():判断 ...

  5. 理解 Redis(7) - Set 值

    unordered collection of unique strings.set值是唯一的字符串的无序集合, 把握住两个特点: 唯一, 无序. 清空所有的数据, 并清理显示界面: 127.0.0. ...

  6. Oracle 12C ORA-65096: 公用用户名或角色名无效

    先说基本用法: 先按11G之前进行 conn / as sysdba; create user test identifed by test; ORA-65096: 公用用户名或角色名无效. 查官方文 ...

  7. [osg]OSG使用更新回调来更改模型

    使用回调类实现对场景图形节点的更新.本节将讲解如何使用回调来实现在每帧的更新遍历(update traversal)中进行节点的更新.        回调概览       用户可以使用回调来实现与场景 ...

  8. 搞定'express' 不是内部或外部命令,也不是可运行的程序或批处理文件

    1 -- 官方下nodejs,一路next完成, node -v 没问题 可以检测到版本号,node环境ok~ 2-- 本地 安装express :npm install express  -g 检测 ...

  9. bootstrap的渲染机制

    bootstrap的渲染机制. http://www.cnblogs.com/djtao/p/5942620.html 源码解析: http://www.cnblogs.com/ahole/p/588 ...

  10. VIM编辑器和VI编辑器的区别

    vi 和vim 的区别 写在前面:这个两个"东西"着实让我烦恼一阵子,但是自己一直没当回事,但是遇到了好几次再决定彻底把他们搞的明白,一下是我通过查找资料了解到的关于这两个编辑器的 ...