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. 利用ASP.NET操作IIS (可以制作安装程序)

    很多web安装程序都会在IIS里添加应用程序或者应用程序池,早期用ASP.NET操作IIS非常困难,不过,从7.0开始,微软提供了 Microsoft.Web.Administration 类,可以很 ...

  2. Day2 基本数据类型

    一.python数据类型 1.1数字 2 是一个整数的例子. 长整数 不过是大一些的整数. 3.23和52.3E-4是浮点数的例子.E标记表示10的幂.在这里,52.3E-4表示52.3 * 10-4 ...

  3. Linux 安装Anaconda 4.4.0

    安装步骤参考了官网的说明:https://docs.anaconda.com/anaconda/install/linux.html 具体步骤如下:  1.在官网下载地址 https://www.an ...

  4. mysql技能提升篇 - Sqlyog高级应用

    mysql作为绝大部分公司使用的数据库,自然是牛牛牛! 每个人都能设计数据库,都能从删库到跑路.但是,如何做到更好,更快,更准地建立你的mysql数据库,这是个值得关注的问题(尽管很多人已经去搞大数据 ...

  5. How many Knight Placing? UVA - 11091

    How many Knight Placing? Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %l ...

  6. php中常用的字符串大小写转换函数实例解释

    PHP字符串处理函数中,最为简单的几个函数,相关解释就不上了,直接看例子. PHP字符串处理函数中,最为简单的几个函数,相关解释就不上了,直接看例子. strtolower函数.strtoupper函 ...

  7. Spark版wordcount,并根据词频进行排序

    import org.apache.spark.{SparkConf, SparkContext}/** * Created by loushsh on 2017/10/9. */object Wor ...

  8. VisualStudio快捷键大全

    Ctrl+m+Crtr+o折叠所有大纲Ctrl+M+Crtr+P: 停止大纲显示Ctrl+K+Crtr+C: 注释选定内容Ctrl+K+Crtr+U: 取消选定注释内容Ctrl+J : 列出成员 智能 ...

  9. Nginx平滑升级源码分析

    一.平滑升级步骤 1.重命名之前的sbin/nginx文件,将新的nginx文件放到sbin/目录下 #mv ./sbin/nginx ./sbin/nginx.old #cp ~/nginx ./s ...

  10. Echarts数据可视化tooltip提示框,开发全解+完美注释

    全栈工程师开发手册 (作者:栾鹏) Echarts数据可视化开发代码注释全解 Echarts数据可视化开发参数配置全解 6大公共组件详解(点击进入): title详解. tooltip详解.toolb ...