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. Spark任务流程笔记

    Spark学习笔记总结 02. Spark任务流程 1. RDD的依赖关系 RDD和它依赖的父RDD(s)的关系有两种不同的类型,即窄依赖(narrow dependency)和宽依赖(wide de ...

  2. JavaScript new Boolean(false) 其实是true

    Boolean类型是JavaScript原始数据类型(primitive type)之一:常用来表示 真或假,是或否:这个类型只有两个值:保留字true和false 一般用于控制语句:如下 if(Bo ...

  3. ThinkPHP中:使用递归写node_merge()函数

    需求描述: 现有一个节点集合 可以视为一个二维数组 array(5) { [0] => array(4) { ["id"] => string(1) "1&q ...

  4. PLT文件 和 DXF文件

    PLT: CAM/CAD类似软件处理的图像文件的文件格式 DXF: AutoCAD(Drawing Interchange Format或者Drawing Exchange Format) 绘图交换文 ...

  5. 在Java环境上运行redis

    首先你得有Java环境,不多说,参考http://jingyan.baidu.com/article/f96699bb8b38e0894e3c1bef.html 下载redis驱动包 链接:http: ...

  6. FTP基本操作类大全,外加c#基础公共帮助类

    总结平时用到的一些FTP操作类,方便需要的用到.github地址:https://github.com/Jimmey-Jiang/Common.Utility 1.连接FTP服务器 /// <s ...

  7. 【转】Windows自动连接、断开无线网络

    前提是先连接到指定的WiFi网络上. 然后通过 netsh wlan export profile 将网络配置文件导出,然后使用如下命令添加配置文件到指定的网络接口上,再执行连接命令即可. netsh ...

  8. ZOJ2105 终于找到错误

    ZOJ2105:点击打开链接 错误代码 #include<stdio.h> #include<stdlib.h> int q[110]; int main() { int a, ...

  9. cocos2dx - android环境配置及编译

    接上一节内容:cocos2dx - 伤害实现 本节主要讲Android环境配置及编译 在第一节中setup.py的配置里,我们没有配置对应的ndk,sdk,ant的路径,在这里需要先配置好环境变量. ...

  10. win10 sdk 是否向下兼容

    向下兼容(downward compatibility),又称向后兼容(backward compatibility).回溯兼容,在计算机中指在一个程序.库或硬件更新到较新版本后,用旧版本程序创建的文 ...