Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9944   Accepted: 2677

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

 
用long long 才过啊!!!
 
 
 
 #include <stdio.h>
#include <string.h>
#include <math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=;
int cnt,p[N+],flag[N+];
void get_prime()
{
int i,j;
for(i=;i<N;i++)
{
if(!flag[i])
p[cnt++]=i;;
for(j=;j<cnt&&p[j]*i<N;j++)
{
flag[i*p[j]]=;
if(i%p[j]==)
break;
}
}
} int a[],pp[];
int main()
{
get_prime();
//printf("%d**%d**\n",p[0],p[1]);
ll l,r,i,j;
while(~scanf("%lld%lld",&l,&r))
{
//if(l>r)swap(l,r);
if(l<)l=;
for(i=;i<=r-l;i++)a[i]=;
ll sum=r-l+;//printf("*****\n");
for(i=;a[i]<=r&&i<cnt;i++)
for(j=l/p[i]*p[i];j<=r;j+=p[i])
{
if(j>=l&&j/p[i]>&&a[j-l])
a[j-l]=,sum--;
} if(sum<){printf("There are no adjacent primes.\n");continue;} ll cp=;
for(i=;i<=r-l;i++)
if(a[i]) pp[cp++]=i+l;
ll max,min,pos1,pos2;
max=min=pp[]-pp[];
pos1=pos2=;
for(i=;i<cp;i++)
{
if(max<pp[i]-pp[i-])
{
max=pp[i]-pp[i-];
pos1=i;
}
if(min>pp[i]-pp[i-])
{
min=pp[i]-pp[i-];
pos2=i;
}
}
printf("%d,%d are closest, %d,%d are most distant.\n",pp[pos2-],pp[pos2],pp[pos1-],pp[pos1]) ;
}
return ;
}

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. [ACM] POJ 2689 Prime Distance (筛选范围大素数)

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

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

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

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

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

  7. POJ 2689 Prime Distance (素数筛选法,大区间筛选)

    题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...

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

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

  9. POJ 2689 Prime Distance (素数+两次筛选)

    题目地址:http://poj.org/problem?id=2689 题意:给你一个不超过1000000的区间L-R,要你求出区间内相邻素数差的最大最小值,输出相邻素数. AC代码: #includ ...

随机推荐

  1. three arrays

    three arrays 字典树上贪心 #include<bits/stdc++.h> using namespace std; ][][]; ][]; ]; ]; ][]; ][]; i ...

  2. [CSP-S模拟测试]:visit(组合数学+exLucas+CRT)

    题目传送门(内部题6) 输入格式 第一行包含两个整数$T$,$MOD$:第二行包含两个整数$n$,$m$,表示$dirty$房子的位置. 输出格式 一行一个整数,表示对$MOD$取模之后的答案. 样例 ...

  3. 使用清华大学提供的 Anaconda 镜像下载 Python 软件包

    使用清华大学提供的 Anaconda 镜像下载 Python 软件包 pip install -i http://pypi.tuna.tsinghua.edu.cn/simple tensorflow ...

  4. 利用 TensorFlow 入门 Word2Vec

    利用 TensorFlow 入门 Word2Vec 原创 2017-10-14 chen_h coderpai 博客地址:http://www.jianshu.com/p/4e16ae0aad25 或 ...

  5. LeetCode 10——正则表达式匹配

    1. 题目 2. 解答 在 回溯算法 中我们介绍了一种递归的思路来求解这个问题. 此外,这个问题也可以用动态规划的思路来解决.我们定义状态 \(P[i][j]\) 为子串 \(s[0, i)\) 和 ...

  6. C# winform OpenFileDialog用法

    https://jingyan.baidu.com/article/e52e36156fa6d240c60c51c8.html 详情看看这个.

  7. Delphi XE2 之 FireMonkey 入门(21) - 和 FMX 相关的类(表)

    TObject TPersistent TComponent IInterface,IInterfaceComponentReference         TBasicAction TControl ...

  8. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_09 序列化流_6_练习_序列化集合

  9. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_01 File类_8_File类遍历(文件夹)目录功能

    遍历这个目录下的文件 遍历一个文件就会报错’ 不存在的路径,也会报空指针异常 遍历可以获取到隐藏的文件夹和文件.常见一个隐藏的文件和文件夹 ListFiles私有类型的数组

  10. drop_duplicates()函数

    1dataframe删除某一列的重复元素,默认只留下第一次出现的 inplace参数设置为true时直接在原数据上修改,为False时,生成副本. 注意所有函数中inplace一旦设置为True,此时 ...