POJ2689:Prime Distance(大数区间素数筛)
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
Output
Sample Input
2 17
14 17
Sample Output
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.
题意:求一个长度不超过1e6的区间[L,R](L和R很大)中,距离最近和最远的素数对。
思路:求出1到sqrt(R)间的素数,然后利用这些素数筛去[L,R]中的合数。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int p[maxn+],vis[maxn+],cnt,L,R;
int wl,wr,vl,vr,tag[],times;
int main()
{
for(int i=;i<=maxn;i++){
if(!vis[i]) p[++cnt]=i;
for(int j=i+i;j<=maxn;j+=i) vis[j]=;
}
while(cin>>L>>R){
if(L==) L++;
times++;
for(int i=;i<=cnt;i++){
int a=(L-)/p[i]+;
int b=R/p[i];
for(int j=a;j<=b;j++)
if(j>) tag[j*p[i]-L]=times;
}
int pre=-; wl=wr=vl=vr=;
for(int i=;i<=R-L;i++){
if(tag[i]!=times) {
if(pre!=-) {
if(wl==) wl=vl=pre+L,wr=vr=i+L;
else {
if(i-pre>wr-wl) wl=pre+L,wr=i+L;
if(i-pre<vr-vl) vl=pre+L,vr=i+L;
}
}
pre=i;
}
}
if(wl==) printf("There are no adjacent primes.\n");
else printf("%d,%d are closest, %d,%d are most distant.\n",vl,vr,wl,wr);
}
return ;
}
POJ2689:Prime Distance(大数区间素数筛)的更多相关文章
- 题解报告:poj 2689 Prime Distance(区间素数筛)
Description The branch of mathematics called number theory is about properties of numbers. One of th ...
- POJ-2689 Prime Distance (两重筛素数,区间平移)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13961 Accepted: 3725 D ...
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- LightOj 1197 Help Hanzo 区间素数筛
题意: 给定一个区间a,b,a-b>=100000,1<=a<=b<=231,求出给定a,b区间内的素数的个数 区间素数筛 (a+i-1)/ ii向上取整,当a为 i 的整数倍 ...
- POJ-2689 Prime Distance,区间素数筛法
Prime Distance 只会埃氏筛法的弱鸡今天读了读挑战程序设计120页,明白了求小区间内素数的方 ...
- poj 2689 Prime Distance(大区间筛素数)
http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...
- POJ2689 Prime Distance(数论:素数筛选模板)
题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...
- 解题报告:poj2689 Prime Distance
2017-10-03 11:29:20 writer:pprp 来源:kuangbin模板 从已经筛选好的素数中筛选出规定区间的素数 /* *prime DIstance *给出一个区间[L,U],找 ...
- poj2689 Prime Distance题解报告
题目戳这里 [题目大意] 给定一个区间[L,R],求区间内的质数相邻两个距离最大和最小的. [思路分析] 其实很简单呀,很明显可以看出来是数论题,有关于质数的知识. 要注意一下的就是L和R的数据范围都 ...
随机推荐
- SGU 106 在区间范围内的线性方程解个数
题意:求解方程ax+by+c=0,在区间x1->x2和y1->y2的解的个数. 看似简单,真心a的不容易啊! 开始跪于第8组数据,原因是没用long long !后来改了,跪于12组,超时 ...
- HUNAN 11560 Yangyang loves AC(二分+贪心)
http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11560&courseid=0 题意:总共有n天,每天 ...
- codevs——1036 商务旅行
1036 商务旅行 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 某首都城市的商人要经常 ...
- codevs——2152 滑雪
2152 滑雪 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description trs喜欢滑雪.他来到了一个滑雪场,这个滑雪场 ...
- python和python-dev
问:python-dev是什么?为什么安装了python后有时还要安装python-dev? 答: linux发行版通常会把类库的头文件和相关的pkg-config分拆成一个单独的xxx-dev(el ...
- B站papi酱、陈一发、李云龙
李云龙-花田错 https://www.bilibili.com/video/av10842071/?from=timeline&isappinstalled=1 李云龙:你猜旅长怎么说? h ...
- HUD 1506 Largest Rectangle in a Histogram
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- 【Java TCP/IP Socket】TCP Socket(含代码)
TCP的Java支持 协议相当于相互通信的程序间达成的一种约定,它规定了分组报文的结构.交换方式.包含的意义以及怎样对报文所包含的信息进行解析,TCP/IP协议族有IP协议.TCP协议和UDP协议.现 ...
- php执行超时(nginx,linux环境)
与下面的参数有关 nginx: fastcgi_connect_timeout fastcgi_read_timeout fastcgi_send_timeout php-fpm:request_te ...
- uicollectionview 使用uibutton或者uiimageview实现旋转出现scale的问题
uicollectionview下单独使用uibutton然后setimage或者直接使用uiimageview然后一定角度旋转后发现size会变动 解决方案:添加uibutton到uicollect ...