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. 阿里云CentOS 7.3安装Redis详细步骤

    ############  准备  ############### 从Redis官网下载Linux redis3.2.6版本,我下载的redis-3.2.6.tar.gz(目前最新稳定版),下载到/u ...

  2. POJ 1384 Piggy-Bank (完全背包)

    Piggy-Bank 题目链接: http://acm.hust.edu.cn/vjudge/contest/130510#problem/F Description Before ACM can d ...

  3. CTO爆料:2019程序员最需要了解的行业前沿技术是什么?

    安森,个推CTO 毕业于浙江大学,现全面负责个推技术选型.研发创新.运维管理等工作,已带领团队开发出针对移动互联网.金融风控等行业的多项前沿数据智能解决方案. 曾任MSN中国首席架构师,拥有十余年资深 ...

  4. 按ECS退出全屏模式

    <!DOCTYPE html><html><meta http-equiv="Content-Type" content="text/htm ...

  5. Mybatis一对一关联查询

    有两张表,老师表teacher和班级表class,一个class班级对应一个teacher,一个teacher对应一个class 需求是根据班级id查询班级信息(带老师的信息) 创建teacher和c ...

  6. Oracle JET(一)Oracle JET介绍

    Oracle JET (Oracle Javascript Extension Toolkit)是一款 Oracle 的 JavaScript 拓展工具包.简单来说 Oracle JET 是一个一堆好 ...

  7. Prototype js library

    Prototype An object-oriented JavaScript framework Prototype is a JavaScript framework that aims to e ...

  8. ANativeWindow_fromSurface

    c++后台若使用ANativeWindow_fromSurface将surface转化为ANativeWindow: 需要头文件:#include <android/native_window_ ...

  9. VB6 Webbowser控件与JS交互,无边框和屏蔽右键菜单

    1. 屏蔽右键菜单  在菜单中单击"工程"->"引用",在列表中找到"Microsoft HTML Object Library"打上 ...

  10. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_08 Map集合_4_Map集合遍历键找值方式

    键找值的方式 增强for 增强for的简化方式