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.

Source

    给出[L,R],求区间内的素数,R<=2147483647,R-L<=1000000, 注意到只用sqrt(R)以内的素数就可以筛出[L,R]里面的素数,
可以先对sqrt(MAX_INT)内的素数打一个表。对于[L,R]的询问,用小于等于sqrt(R)的素数筛一下然后统计一下就好了。注意L<2的时候
要特判一下否则容易把1也给打进去。
    

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
#define LL long long
#define mp make_pair
#define pb push_back
#define inf 0x3f3f3f3f
int maxn=;
vector<int>prime;
vector<int>p;
bool is[];
void init(){
is[]=is[]=;
for(LL i=;i<=maxn;++i){
if(!is[i]) prime.push_back(i);
for(LL j=;j<prime.size()&&i*prime[j]<=maxn;j++){
is[i*prime[j]]=;
if(i%prime[j]) break;
}
}
}
void solve(LL L,LL R){
p.clear();
memset(is,,sizeof(is));
for(LL i=;i<prime.size()&&1LL*prime[i]*prime[i]<=R;i++){
LL s=L/prime[i]+(L%prime[i]>);
if(s==)s=;
for(LL j=s;j*prime[i]<=R;j++){
if(j*prime[i]>=L) is[j*prime[i]-L]=;
}
}
for(int i=;i<=R-L;i++){
if(!is[i]&&i+L>=) p.push_back(i+L);
}
}
int main(){
LL L,R;
init();
while(scanf("%lld%lld",&L,&R)!=EOF){
solve(L,R); if(p.size()<) puts("There are no adjacent primes.");
else{
int c1,c2,m1,m2;
c1=m1=p[];
c2=m2=p[];
for(int i=;i<p.size();++i){
if(p[i]-p[i-]<c2-c1){
c1=p[i-];
c2=p[i];
}
if(p[i]-p[i-]>m2-m1){
m1=p[i-];
m2=p[i];
}
}
printf("%d,%d are closest, %d,%d are most distant.\n",c1,c2,m1,m2);
}
}
return ;
}

poj-2689-素数区间筛的更多相关文章

  1. poj2689(素数区间筛法模板)

    题目链接: http://poj.org/problem?id=2689 题意: 给出一个区间 [l, r] 求其中相邻的距离最近和最远的素数对 . 其中 1 <= l <  r < ...

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

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

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

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

  4. 大区间素数筛选(POJ 2689)

    /* *POJ 2689 Prime Distance *给出一个区间[L,U],找出区间内容.相邻的距离最近的两个素数和距离最远的两个素数 *1<=L<U<=2147483647 ...

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

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

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

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

  7. 素数筛 poj 2689

    素数筛 #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; ...

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

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

  9. lightoj1197 素数双筛,可以参考poj的那题双筛

    /* 判断一个数是否是素数,只要判断这个数有没有在[2,sqrt(n)]区间的因子 同样,对于大数短区间的筛选,同样可以用这种判断方式, 先筛出sqrt(n)范围内的素数,然后用这些素数去筛出区间内的 ...

  10. POJ 2689 筛法求素数

    DES:给出一个区间[L, U].找出这个区间内相邻的距离最近的两个素数和距离最远的两个素数.其中1<=L<U<=2147483647 区间长度不超过1000000. 思路:因为给出 ...

随机推荐

  1. (zhuan) Evolution Strategies as a Scalable Alternative to Reinforcement Learning

    Evolution Strategies as a Scalable Alternative to Reinforcement Learning this blog from: https://blo ...

  2. StringBuilder的三种删除方法比较

    分别用一千万次循环来比较StringBuilder的三种删除方法所用时间 未避免偶然性,再循环一百次来比较总时间 --主类 public class StringBuilderRemove { pub ...

  3. 【NOIP 2016】Day1 T2 天天爱跑步

    Problem Description 小 C 同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是一个养成类游戏,需要玩家每天按时上线,完成打卡任 ...

  4. 使用v-for循环写入html内容,每一项的数据的写入

    项目使用vue.js,在写某个dialog页面时,需要循环后台的数据(班级,班级学生名单,已选学生名单,发布时间,截止时间,答案显示等). 遇到的问题:循环绑定的值是相同的,而且改动一个值,其他ite ...

  5. 【Django】【二】模板

    1. Django-bootstrap3 guest>python -m pip install django-bootstrap3 [代码] settings.py ""& ...

  6. linux服务器安装brook服务端 使用brook客户端

    既然你已经找到了此文章,说明已经知道brook的用途了,不做介绍,下面讲安装方法: 连接服务器,随便cd一个安装目录,例如: mkdir brook && cd brook 2.进re ...

  7. Codeforces 786 C. Till I Collapse

    题目链接:http://codeforces.com/contest/786/problem/C 大力膜了一发杜教的代码感觉十分的兹瓷啊! 我们知道如果$k$是给定的我们显然是可以直接一遍$O(n)$ ...

  8. CentOS7 使用firewalld打开关闭防火墙以及端口

    1.firewalld的基本使用 启动 systemctl start firewalld 关闭 systemctl stop firewalld 查看状态 systemctl status fire ...

  9. django自定制Admin

    如果只是在admin中简单的展示及管理模型,那么在admin.py模块中使用admin.site.register将模型注册一下就好了: from django.contrib import admi ...

  10. STL_string.【转】C++中int、string等常见类型转换

    ZC:#include <sstream> ZC:貌似还有 istringstream 和 ostringstream ... https://www.cnblogs.com/gaobw/ ...