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. java对IO的操作

    import java.io.*; public class HelloWorld { //Main method. public static void main(String[] args) { ...

  2. Difference Between ZIP and GZIP

    From: http://www.differencebetween.net/technology/difference-between-zip-and-gzip/ Summary: 1. GZIP ...

  3. hibernate 查询方式

    1.对象导航查询 2.OID查询 3.hql查询 4.QBC查询 5.本地sql查询 一.对象导航查询 示例: 查询id=6的user对象的所有角色: 二.OID查询 实例查询id=6的user对象 ...

  4. android 中使用svg

    http://www.see-source.com/blog/300000038/1189.html http://www.jianshu.com/p/30dfa5920658#

  5. R语言数据管理(二):模式与类

      最常用的4种数据类型是数值型(numeric).字符型(character)(字符串).日期型(Date)或POSIXct(基于日期的).逻辑型(logical)(TRUE或FALSE). 变量中 ...

  6. 中国移动OneNet平台上传GPS数据JSON格式

    最终目的输出 POST /devices/3225187/datapoints HTTP/1.1 api-key: R9xO5NZm6oVI4YBHvCPKEqtwYtMA Host: api.hec ...

  7. Scala window下安装

    第一步:Java 设置 检测方法前文已说明,这里不再描述. 如果还为安装,可以参考我们的Java 开发环境配置. 接下来,我们可以从 Scala 官网地址 http://www.scala-lang. ...

  8. JAVA中最方便的Unicode转换方法

    在命令行界面用native2ascii工具  1.将汉字转为Unicode:  C:\Program   Files\Java\jdk1.5.0_04\bin>native2ascii  测试 ...

  9. Android系统篇之—-编写简单的驱动程序并且将其编译到内核源码中【转】

    本文转载自:大神 通过之前的一篇文章,我们了解了 Android中的Binder机制和远程服务调用 在这篇文章中主要介绍了Android中的应用在调用一些系统服务的时候的原理,那么接下来就继续来介绍一 ...

  10. shell 批量创建_备份 mysql 数据库 表

    #!/bin/bash user=root password= socket=/var/lib/mysql/mysql.sock mycmd="mysql -u$user -p$passwo ...