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. linux c编程:进程环境

    一 进程终止: ⼀个进程可以登记若⼲个(具体⾃⼰验证⼀下)个函数,这些函数由exit⾃动调⽤,这些函数被称为终⽌处理函数, atexit函数可以登记这些函数. exit调⽤终⽌处理函数的顺序和atex ...

  2. QT5使用Webkti

    Qt 5.3 使用原来的QT4.8.4项目时QWebView .QWebFrame等类无法编译通过. 出现原因:QWebView .QWebFrame.QWebPage.QWebInspector等这 ...

  3. 【SHARE】WEB前端学习资料

    参考资料:https://github.com/karlhorky/learn-to-program 学习网站:http://www.codecademy.com/learn https://www. ...

  4. TCP标准模板

    伪代码 #创建一个TCP服务器 ss = socket() #创建服务器套接字 ss.bind() #把地址绑定到套接字上 ss.listen() #监听连接 inf_loop: #服务器无线循环 c ...

  5. LeetCode:寻找重复数【287】

    LeetCode:寻找重复数[287] 题目描述 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数 ...

  6. Ubuntu安装SSH + Windows上配置Putty

    1. Ubuntu安装SSH 命令: # sudo apt-get install openssh-server 2. 启动SSH Server 命令: # sudo /etc/init.d/ssh ...

  7. Data Structure Graph: strong connectivity

    如果为undirected graph就是dfs或者bfs,如果都能visit则为连通O(V+E). 如果为directed graph就先dfs或者bfs,再reverse direct,再dfs或 ...

  8. leetcode 900. RLE Iterator

    Write an iterator that iterates through a run-length encoded sequence. The iterator is initialized b ...

  9. P3506 [POI2010]MOT-Monotonicity 2

    题目 P3506 [POI2010]MOT-Monotonicity 2 第一次切掉没题解的题\(qwq\) 做法 首先确定\(a_i\)的位置后显然就能确定\(a_{i+1}\)的位置,建一棵权值线 ...

  10. 剑指offer之 奇数偶数数组位置调整且保存顺序不变

    public class Solution { public void reOrderArray(int [] array) { reOrderCore(array,array.length); } ...