Prime Distance
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15820   Accepted: 4202

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.
注意:L可能为1
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = ;
typedef long long LL;
LL l, u;
bool isPrime[MAXN], isSmallPrime[MAXN];
int prime[MAXN], len;
void prep()
{
memset(isPrime, true, sizeof(isPrime));
memset(isSmallPrime, true, sizeof(isSmallPrime));
isSmallPrime[] = false;
isSmallPrime[] = false;
len = ;
for(LL i = ; i * i <= u; i++)
{
if(isSmallPrime[i])
{
for(LL j = i + i; j * j <= u; j += i)
{
isSmallPrime[j] = false;
}
for(LL j = max(i + i, (l + i - ) / i * i); j <= u; j += i)
{
isPrime[j-l] = false;
}
}
}
for(LL i = l; i <= u; i++)
{
if(isPrime[i-l])
{
prime[len++] = i;
}
}
}
int main()
{
while(scanf("%I64d %I64d", &l, &u) != EOF)
{
if(l == ) l++;
prep();
if(len <= )
{
printf("There are no adjacent primes.\n");
continue;
}
int mind = 0x3f3f3f3f, a, b;
int maxd = , c, e;
for(int i = ; i < len; i++)
{
int d = prime[i] - prime[i-];
if(mind > d)
{
mind = d;
a = prime[i-];
b = prime[i];
}
if(maxd < d)
{
maxd = d;
c = prime[i-];
e = prime[i];
}
}
printf("%d,%d are closest, %d,%d are most distant.\n", a, b, c, e);
}
return ;
}

POJ2689:素数区间筛选的更多相关文章

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

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

  2. POJ-2689-Prime Distance(素数区间筛法)

    链接: https://vjudge.net/problem/POJ-2689 题意: The branch of mathematics called number theory is about ...

  3. HDOJ/HDU 2710 Max Factor(素数快速筛选~)

    Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 < ...

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

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

  5. poj2689 Prime Distance(素数区间筛法)

    题目链接:http://poj.org/problem?id=2689 题目大意:输入两个数L和U(1<=L<U<=2 147 483 647),要找出两个相邻素数C1和C2(L&l ...

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

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

  7. HDU 2136 Largest prime factor(查找素数,筛选法)

    题目梗概:求1000000以内任意数的最大质因数是第几个素数,其中 定义 1为第0个,2为第1个,以此类推. #include<string.h> #include<stdio.h& ...

  8. HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)

    Problem Description Give you a lot of positive integers, just to find out how many prime numbers the ...

  9. hdu Diophantus of Alexandria(素数的筛选+分解)

    Description Diophantus of Alexandria was an egypt mathematician living in Alexandria. He was one of ...

随机推荐

  1. debian dhcp配置

    1 将/etc/network/interfaces中设置成dhcp auto eth0iface eth0 inet dhcp 2 重启网络服务 /etc/init.d/networking res ...

  2. Feign-独立使用-实战

    目录 写在前面 1.1.1. 短连接API的接口准备 1.1.2. 申明远程接口的本地代理 1.1.3. 远程API的本地调用 写在最后 疯狂创客圈 亿级流量 高并发IM 学习实战 疯狂创客圈 Jav ...

  3. python基础-第六篇-6.4模块混战

    我们之前接触多的编程方式就是函数式编程,而且喜欢就一个文件里写完所有的程序代码,这样做在前期感觉还不错,不过一旦你的程序变复杂,在易读性和排错方面就感觉好吃力,功能界限不明显,那今天我们就来讲讲怎么用 ...

  4. 我的Android进阶之旅------>Android关于Log的一个简单封装

    android.util.Log类,可以方便地用于在编码调试过程中打印日志.但是在发布后的产品中,如果有太多的日志打印,则会严重地影响性能.对android.util.Log类做一个简单的封装,当产品 ...

  5. 短时程突触可塑性(short-term synaptic plasticity)

    介绍 神经元的突触可塑性一般被认为是大脑学习与记忆的分子生物学机制,它是指突触传递效率增强或减弱的变化现象.若这种变化只持续数十毫秒到几分,便称之为短时程突触可塑性,其中效率增强与减弱分别叫做短时程增 ...

  6. ob 函数讲解

    ob的基本原则:如果ob缓存打开,则echo的数据首先放在ob缓存.如果是header信息,直接放在程序缓存.当页面履行到最后,会把ob缓存的数据放到程序缓存,然后依次返回给涉猎器.下面我说说ob的基 ...

  7. php 验证邮箱的方法

    在开发 web系统时,经常在注册或者登陆或者邮箱保护的时候会需要验证邮箱,现在我来分享邮箱验证的一些小tips.(多说一句,现在基本用手机号注册登录是趋势了,匹配手机号我后面再讲了). 1.最开始也是 ...

  8. 通过GPRS将GPS数据上传到OneNet流程

    AT OK AT+CGCLASS="B" OK AT+CGDCONT=1,"IP","CMNET" OK AT+CGATT=1 OK AT+ ...

  9. 免费好用的Diff和Merge工具大总结

    总结:比较下来:diffmerge和P4merge最好用,kdiff比较专业些,支持自动merge. 一 csdiff 下载:http://www.componentsoftware.com/Prod ...

  10. Visual Studio 2017 扩展推荐

    ReSharper : 首先的是Resharper,这个基本是目前是我开发过程中必备的工具集,唯一的缺点就是吃内存,所以你的内存要是低于8G,就不要使用它了.它的特点可以快速重构.高亮显示错误.导航和 ...