poj2689Prime Distance(大区间筛素数)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 19635 | Accepted: 5273 |
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
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
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(大区间筛素数)的更多相关文章
- poj 2689 Prime Distance(大区间筛素数)
http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- M - Help Hanzo LightOJ - 1197 (大区间求素数)
题意: 求[a,b]之间的素数的个数 数很大...数组开不起 所以要想到转化 因为小于等于b的合数的最小质因子 一定小于等于sqrt(b),所以只需要求出来[0,sqrt(b)]的素数 然后取倍数删 ...
- POJ2689-Prime Distance-区间筛素数
最近改自己的错误代码改到要上天,心累. 这是迄今为止写的最心累的博客. Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ 2689.Prime Distance-区间筛素数
最近改自己的错误代码改到要上天,心累. 这是迄今为止写的最心累的博客. Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ-2689 Prime Distance (两重筛素数,区间平移)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13961 Accepted: 3725 D ...
- LightOJ1197 Help Hanzo —— 大区间素数筛选
题目链接:https://vjudge.net/problem/LightOJ-1197 1197 - Help Hanzo PDF (English) Statistics Forum Tim ...
- Help Hanzo lightof 1197 求一段区间内素数个数,[l,r] 在 [1,1e9] 范围内。r-l<=1e5; 采用和平常筛素数的方法。平移区间即可。
/** 题目:Help Hanzo lightof 1197 链接:https://vjudge.net/contest/154246#problem/M 题意:求一段区间内素数个数,[l,r] 在 ...
- POJ2689 Prime Distance(数论:素数筛选模板)
题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...
随机推荐
- 如何快速成长?我的java之路!
由于一些外部的原因,我不得不从自己熟悉的php领域,转战到java战场.我个人觉得还是有些心得吧,不管怎么样,或多或少可能都会有那么些经历的人,和你一起走在这世上!尽管你不知道TA是谁. 其实,转换一 ...
- IIC-BUS INTERFACE
I2C总线是由Philips公司开发的一种简单.双向二线制同步串行总线.它只需要两根线即可在连接于总线上的器件之间传送信息. To control multi-master IIC-bus opera ...
- Java钉钉开发_02_免登授权(身份验证)(附源码)
源码已上传GitHub: https://github.com/shirayner/DingTalk_Demo 一.本节要点 1.免登授权的流程 (1)签名校验 (2)获取code,并传到后台 (3) ...
- Bear and Floodlight 状态压缩DP啊
Bear and Floodlight Time Limit: 4000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u ...
- VB.net shell、IO.File.Open、Process.Start、Shellexecute API 运用经验总结
打开文件还有很多方法,但我了解到运用较多的是上面几种- -,为了防止以后忘记,先把了解到的写下来. 1.Shell 这个看了很多网页,最靠谱的运用方法: Shell("cmd.exe /c ...
- eclipse的插件开发-启动时间
今天晚上看<深入理解java虚拟机>时,作者在书中有一段,eclipse优化的章节,其中涉及到了eclipse启动时间检测的插件开发 于是翻了翻资料,也开发了一个自己的插件 如图是开发后启 ...
- 将 Intent 序列化,像 Uri 一样传递 Intent!!!
一.真的需要new一个Intent吗? 在 Android 中,打开一个 Activity ,有多少种方式?不过不管是使用什么方式,最终都没办法逃避创建一个 Intent ,然后startActivi ...
- js 将一大段时间均分为很多个小时间段
最近写项目,遇到一个将选中时间段平均分割为若干小段,然后根据小段时间在数据库查询求均值的问题,后台大哥犯懒,非说后台做不了,让我分好传给他ヾ(. ̄□ ̄)ツ゜゜゜好气呦,但还要保持微笑,我就是这么懂礼貌 ...
- IsKeyboardFocused -- 键盘焦点
<Trigger Property="IsKeyboardFocused" Value="true"> <!--<Setter Prop ...
- 验证代理IP
##author:wuhao#import urllib.requestfrom http import cookiejar import xlrd import threading #有效的代理,可 ...