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. Cmdow-一个win32窗口管理命令行工具

    最近有个需求,将同一个程序运行8个实例,并按照规则在两个窗口上分布,本以为用bat就可以实现,结果发现没那么容易,搜了很久找到了这个工具cmdow.exe,发现这个东西真不错. 符合了我们项目的需求: ...

  2. es分片shard的数量

    适当的提升分片数量可以提升建立索引的速度: 一般情况下:一个索引库建立5-20个分片是最合适的: 注意:如果分片过少或者过多,都会降低检索的速度 分片数过多会导致: 1. 会导致打开比较多的文件2. ...

  3. ThreadLocal在Spring事务管理中的应用

    ThreadLocal是用来处理多线程并发问题的一种解决方案.ThreadLocal是的作用是提供线程的局部变量,在多线程并发环境下,提供了与其他线程隔离的局部变量.通常这样的设计的情况是因为这个局部 ...

  4. SpringMVC开发中遇到的异常1:No primary or default constructor found for interface java.util.List

    Request processing failed; nested exception is java.lang.IllegalStateException: No primary or defaul ...

  5. windows编程,消息函数中拦截消息的问题

    很多年没有写windows窗口程序了,今天自制基于vulkan的程序时遇到了一些问题,部分代码如下: LRESULT CALLBACK XWindow::WndProc(HWND hWnd, UINT ...

  6. 通过HookNtCreateSection 动态监控驱动sys、动态链接库dll、可执行文件exe加载

    [cpp] view plaincopyprint? /* windows2003 x86/x64 window7 x86 windows2008 R2 x64测试通过 */ #include < ...

  7. LinkedBlockingDeque 源码分析

    LinkedBlockingDeque LinkedBlockingDeque 能解决什么问题?什么时候使用 LinkedBlockingDeque? 1)LinkedBlockingDeque 是基 ...

  8. Java 位运算超全面总结

    1.原码.反码.补码 关于原码.反码.补码的相关知识作者不打算在这里长篇大论,相关知识已有别的大佬总结很好了,还请老铁自行 Google,不过有篇知乎回答是作者学编程以来见过对相关知识最通俗易懂,生动 ...

  9. PMBOK

    项目章程的内容1. 基于项目干系人的需求和期望提出的要求.2. 项目必须满足的业务要求或产品需求.3. 项目的目的或项目立项的理由.4. 委派的项目经理及项目经理的权限级别.5. 概要的里程碑进度计划 ...

  10. Mysql 免安装版本配置

    1. 安装命令 (制定安装目录的my.ini文件) mysqld --install MySQL --defaults-file="C:\mysql-5.7.26-winx64\bin\my ...