POJ 2689 - Prime Distance - [埃筛]
题目链接:http://poj.org/problem?id=2689
Time Limit: 1000MS Memory Limit: 65536K
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
Output
Sample Input
2 17
14 17
Sample Output
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.
题意:
给出 $st$ 与 $ed$ ($1 \le st < ed \le 2147483647$ 且 $ed - st \le 1e6$),求 $[st,ed]$ 区间内,相邻的两个素数中,差最小的和差最大的(若存在差同样大的一对素数,则有先给出最小的一对素数);
题解:
显然,不可能直接去筛 $2147483647$ 以内的素数;
由于任何一个合数 $n$ 必定包含一个不超过 $\sqrt{n}$ 的质因子,
那么,我们不妨先用欧拉筛法筛出 $[0,46341]$ 区间内的素数($46341 \approx \sqrt{2147483647}$);
然后对于每个test case的 $[st,ed]$ 区间,用筛出来的质数再去标记 $[st,ed]$ 内的合数,剩下来的就是质数了。
AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
const int maxn=1e6+;
vector<ll> p;
bool vis[maxn]; const int MAX=;
bool noprm[MAX+];
vector<int> prm;
void Erato()
{
noprm[]=noprm[]=;
for(int i=;i<=MAX;i++)
{
if(noprm[i]) continue;
prm.pb(i);
for(int j=i;j<=MAX/i;j++) noprm[i*j]=;
}
} int main()
{
Erato(); ll L,R;
while(cin>>L>>R)
{
for(ll i=L;i<=R;i++) vis[i-L]=;
for(int i=;i<prm.size();i++)
{
ll st=max(2LL,L/prm[i]+(L%prm[i]>)), ed=R/prm[i];
for(ll k=st;k<=ed;k++) vis[k*prm[i]-L]=;
}
if(L==) vis[]=; p.clear();
for(ll i=L;i<=R;i++) if(!vis[i-L]) p.pb(i);
if(p.size()<=) cout<<"There are no adjacent primes.\n";
else
{
int mn=, mx=;
for(int i=;i<p.size();i++)
{
if(p[i]-p[i-]<p[mn]-p[mn-]) mn=i;
if(p[i]-p[i-]>p[mx]-p[mx-]) mx=i;
}
printf("%I64d,%I64d are closest, %I64d,%I64d are most distant.\n",p[mn-],p[mn],p[mx-],p[mx]);
}
}
}
POJ 2689 - Prime Distance - [埃筛]的更多相关文章
- POJ - 2689 Prime Distance (区间筛)
题意:求[L,R]中差值最小和最大的相邻素数(区间长度不超过1e6). 由于非素数$n$必然能被一个不超过$\sqrt n$的素数筛掉,因此首先筛出$[1,\sqrt R]$中的全部素数,然后用这些素 ...
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- poj 2689 Prime Distance (素数二次筛法)
2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...
- 数论 - 素数的运用 --- poj 2689 : Prime Distance
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12512 Accepted: 3340 D ...
- POJ 2689.Prime Distance-区间筛素数
最近改自己的错误代码改到要上天,心累. 这是迄今为止写的最心累的博客. Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total S ...
- [ACM] POJ 2689 Prime Distance (筛选范围大素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12811 Accepted: 3420 D ...
- poj 2689 Prime Distance(区间筛选素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9944 Accepted: 2677 De ...
- 题解报告:poj 2689 Prime Distance(区间素数筛)
Description The branch of mathematics called number theory is about properties of numbers. One of th ...
- poj 2689 Prime Distance(大区间筛素数)
http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...
随机推荐
- 一些Android手机的平台信息
1.OPPO A83 (2018-04-16) A83:/ $ cat /system/build.prop | grep "product"# ro.product.cpu.ab ...
- Community宣言
Community宣言 一个幽灵,共产主义的幽灵,在欧洲游荡.为了对这个幽灵进行神圣的围剿,旧欧洲的一切势力,教皇和沙皇.梅特涅和基佐.法国的激进派和德国的警察,都联合起来了. 有哪一个反对党不被它的 ...
- selenium面试题总结
2017年7月17日更新:已经更新部分答案 答案链接 今天有同学问到seleinum面试的时候会问到的问题,随便想了想,暂时纪录一下.欢迎大家在评论中提供更多问题. 去哪儿的面试题 selenium中 ...
- CentOS 6.5 x64下安装宝塔面板、阿里安骑士
一.安装宝塔: CentOS下命令(https://www.bt.cn/bbs/thread-1186-1-1.html) yum install -y wget && wget -O ...
- IPv6地址分类及表示方法
对于IPv4地址,我们知道分为A类.B类.C类.组播地址和留用地址,几大类,ABC类地址中还会有不同功能的如广播地址.私有地址等类型.那么IPv6的地址是怎么分类的呢?本文就带大家初步了解一下. 先说 ...
- FILESTREAM feature can't be enabled if you use cluster shared volumes
Create a SQL Cluster instance. Create Cluster Shared Volume Please note. No Share storage is added i ...
- PHPStorm + Homestead + Xdebug + Chrome Xdebug Helper 调试配置
话说 PHPStorm 写起代码来非常带感,各种提示补全和纠错,以及在 L5 中的命名空间功能更是强大到感动(新建类自动添加命名空间,自动引入命名空间,返回参数命名空间纠正等等).当然它的调试功能更是 ...
- 数字签名与HTTPS详解
因为HTTP协议本身存在着明文传输.不能很好的验证通信方的身份和无法验证报文的完整性等一些安全方面的确点,所以才有了HTTPS的缺陷.HTTPS确切的的说不是一种协议,而是HTTP + SSL (TS ...
- Selenium IDE 3.6 命令Command详解
学以致用,个人觉得要学老外的东西,最好的方法就是自己翻译一遍.因此准备把SIDE官网的一些文档,按工作所需做些翻译整理.本文是命令这一块的提纲,未全部完成,占坑中. Selenium IDE中的命令其 ...
- SkyWalking
介绍 SkyWalking 创建与2015年,提供分布式追踪功能.从5.x开始,项目进化为一个完成功能的Application Performance Management系统.他被用于追踪.监控和诊 ...