题目链接:http://poj.org/problem?id=2689

Time Limit: 1000MS Memory Limit: 65536K

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.

题意:

给出 $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 - [埃筛]的更多相关文章

  1. POJ - 2689 Prime Distance (区间筛)

    题意:求[L,R]中差值最小和最大的相邻素数(区间长度不超过1e6). 由于非素数$n$必然能被一个不超过$\sqrt n$的素数筛掉,因此首先筛出$[1,\sqrt R]$中的全部素数,然后用这些素 ...

  2. poj 2689 Prime Distance(大区间素数)

    题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...

  3. poj 2689 Prime Distance (素数二次筛法)

    2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...

  4. 数论 - 素数的运用 --- poj 2689 : Prime Distance

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12512   Accepted: 3340 D ...

  5. POJ 2689.Prime Distance-区间筛素数

    最近改自己的错误代码改到要上天,心累. 这是迄今为止写的最心累的博客. Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  6. [ACM] POJ 2689 Prime Distance (筛选范围大素数)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12811   Accepted: 3420 D ...

  7. poj 2689 Prime Distance(区间筛选素数)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9944   Accepted: 2677 De ...

  8. 题解报告:poj 2689 Prime Distance(区间素数筛)

    Description The branch of mathematics called number theory is about properties of numbers. One of th ...

  9. poj 2689 Prime Distance(大区间筛素数)

    http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...

随机推荐

  1. Canvas 旋转的图片

    var image = new Image(), counter = 0; image.onload = function () { var CANVAS_WIDTH = 300, CANVAS_HE ...

  2. Linux下清理内存和Cache方法见下文:

    暂时目前的环境处理方法比较简单: 在root用户下添加计划任务: */10 * * * * sync;echo 3 > /proc/sys/vm/drop_caches; 每十分钟执行一次,先将 ...

  3. 中期linux课程考试题

    [口头表达] 1)请描述你了解的磁盘分区的相关知识2)什么是rsync,你有什么生产环境的应用?3)在生产环境中,公司的IDC机房即将超过254台机器,请问你有什么解决方案来规划扩展IDC机房的内网网 ...

  4. top 命令

    首先介绍top中一些字段的含义: VIRT:virtual memory usage 虚拟内存1.进程"需要的"虚拟内存大小,包括进程使用的库.代码.数据等 2.假如进程申请100 ...

  5. LNMP环境的搭建(yum)方法(精)

    第一 先安装nginx nginx在官方CentOS社区yum里面没有,需要在nginx的官方网站去下载yum的配置文件 官方:https://www.nginx.com/resources/wiki ...

  6. 使用Markdown写作

    简介 Markdown是一种轻量级标记语言,创始人为约翰·格鲁伯(John Gruber).它允许人们"使用易读易写的纯文本格式编写文档,然后转换成有效的XHTML(或者HTML)文档&qu ...

  7. [HDFS Manual] CH8 HDFS Snapshots

    HDFS Snapshots HDFS Snapshots 1. 概述 1.1 Snapshottable目录 1.2 快照路径 2. 带快照的更新 3. 快照操作 3.1 管理操作 3.2 用户操作 ...

  8. Unity输出PC端(Windows) 拖拽文件到app中

    需求:给策划们写一个PC端(Window)的Excel导表工具.本来用OpenFile打开FileExplorerDialog后让他们自己选择想要添加的Excel文件就行了,结果有个需求是希望能拖拽E ...

  9. C++实现景区信息管理系统

    景区信息管理系统 实现了: 1.1 建立主程序应用菜单选项 主程序应用菜单选项包含所实现的所有功能,并且对选项采用数字标识进行选择,对其他错误输入可以进行判别,提示输入错误. 1.2 导游线路图的创建 ...

  10. count(*)、count(1)和count(列名)的区别

    count(*).count(1)和count(列名)的区别 1.执行效果上:   l  count(*)包括了所有的列,相当于行数,在统计结果的时候,不会忽略列值为NULL l  count(1)包 ...