Prime Distance
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19635   Accepted: 5273

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.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector> #define MAXN 67890
#define MAXM 1000005
#define LL long long
#define INF 1000000007 using namespace std; LL p[MAXN];
bool prime[MAXN];
bool vis[MAXM];
int tol;
LL l,r;
LL maxl,maxr,minl,minr;
LL maxn,minn;
LL a,b;
vector<LL>v; void prim(){
memset(prime,false,sizeof prime);
tol=;
for(int i=;i<MAXN;i++){
if(prime[i]==false)
p[tol++]=i;
for(int j=;j<tol&&i*p[j]<MAXN;j++){
prime[i*p[j]]=true;
if(i%p[j]==)
break;
}
}
return ;
} void init(){
memset(prime,false,sizeof prime);
memset(vis,false,sizeof vis);
maxn=-;
minn=INF;
v.clear();
} int main(){
prim();
while(scanf("%lld%lld",&l,&r)!=EOF){
init();
if(l==) l=;
for(int i=;i<tol;i++){
a=(l-)/p[i]+;
b=r/p[i];
for(int j=a;j<=b;j++){
if(j>)
vis[j*p[i]-l]=true;
}
}
for(int i=;i<=r-l;i++){
if(vis[i]==false)
v.push_back(i+l);
}
if(v.size()<=){
puts("There are no adjacent primes.");
continue;
}
for(int i=;i<(int)v.size()-;i++){
if(v[i+]-v[i]>maxn){
maxn=v[i+]-v[i];
maxl=v[i];
maxr=v[i+];
}
if(v[i+]-v[i]<minn){
minn=v[i+]-v[i];
minl=v[i];
minr=v[i+];
}
}
printf("%lld,%lld are closest, %lld,%lld are most distant.\n",minl,minr,maxl,maxr);
}
return ;
}

poj2689Prime Distance(大区间筛素数)的更多相关文章

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

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

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

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

  3. M - Help Hanzo LightOJ - 1197 (大区间求素数)

    题意: 求[a,b]之间的素数的个数 数很大...数组开不起 所以要想到转化 因为小于等于b的合数的最小质因子 一定小于等于sqrt(b),所以只需要求出来[0,sqrt(b)]的素数  然后取倍数删 ...

  4. POJ2689-Prime Distance-区间筛素数

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

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

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

  6. POJ-2689 Prime Distance (两重筛素数,区间平移)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13961   Accepted: 3725 D ...

  7. LightOJ1197 Help Hanzo —— 大区间素数筛选

    题目链接:https://vjudge.net/problem/LightOJ-1197 1197 - Help Hanzo    PDF (English) Statistics Forum Tim ...

  8. Help Hanzo lightof 1197 求一段区间内素数个数,[l,r] 在 [1,1e9] 范围内。r-l<=1e5; 采用和平常筛素数的方法。平移区间即可。

    /** 题目:Help Hanzo lightof 1197 链接:https://vjudge.net/contest/154246#problem/M 题意:求一段区间内素数个数,[l,r] 在 ...

  9. POJ2689 Prime Distance(数论:素数筛选模板)

    题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...

随机推荐

  1. windows10 下安装node失败 出现2502 2503的解决办法

    下载node出现以下异常    : the install has encountered an unexpected errer installing this package.this may i ...

  2. openlayer的凸包算法实现

    最近在要实现一个openlayer的凸多边形,也遇到了不小的坑,就记录一下 1.具体的需求: 通过在界面点击,获取点击是的坐标点,来绘制一个凸多边形. 2.思考过程: 1)首先,我们得先获取点击事件发 ...

  3. Linux入门之常用命令(6)Bash命令重定向 管线命令

    命令重定向 将目前所得数据转移到其他地方 >  将输出结果导入文件 如  ls -l / >test     (1)若test文件不存在则创建 (2)若test文件存在 清空后写入 > ...

  4. 洗礼灵魂,修炼python(2)--python安装和配置

    安装python和基本配置: python官方下载地址:www.python.org 打开网站,然后下载对应(32位和64位,windows版还是linux版)的版本,你可以选择python3或者2, ...

  5. Manacher’s Algorithm (神啊)

    (转载自)http://blog.csdn.net/hopeztm/article/details/7932245 这里描述了一个叫Manacher’s Algorithm的算法. 算法首先将输入字符 ...

  6. poj1014二进制优化多重背包

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53029   Accepted: 13506 Descri ...

  7. 读Zepto源码之Fx模块

    fx 模块为利用 CSS3 的过渡和动画的属性为 Zepto 提供了动画的功能,在 fx 模块中,只做了事件和样式浏览器前缀的补全,没有做太多的兼容.对于不支持 CSS3 过渡和动画的, Zepto ...

  8. MMORPG战斗系统随笔(三)、AI系统简介

    在设计一款游戏的时候,如果我们是玩家,是希望自己能够操作角色畅玩游戏的.在一款MMORPG游戏中,大部分的实际游戏角色,是需要玩家来操作的,通过在游戏大世界相互完成游戏中的任务等等来体验游戏.在大世界 ...

  9. Android签名机制---签名过程

    大神文章:http://blog.csdn.net/jiangwei0910410003/article/details/50402000 一.知识点 1.数据摘要(数据指纹).签名文件,证书文件 2 ...

  10. PS+HTML测试

    PS: 1. 写出标尺.参考线.网格线.放大和缩小的快捷键 2. 写出RGB模式和CMYK颜色模式里每一个字母代表什么颜色 3. 写出暖色和冷色各3种 4. 写出常用的抠图工具(6个以上) 5. 写出 ...