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.

解题思路:

这题做得我都是泪。不断地TLE,好不easy优化好了。又RE。代码也写得非常龊。就是正常的二次筛选素数。

因为数据非常大。第一次筛出46500以内的素数。再依据此筛选出区间内的素数。

注意:尽管给的数没有超int范围,但两数相乘是会超int范围的,我也是在这里RE了。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 1000005;
const int M = 46500;
const int INF = 999999999;
bool notprime[N];
int prime_1[M + 1], prime_2[N];
int num_1 = 0, num_2;
void Prime1() // 第一次筛出46500以内的素数
{
memset(notprime, false, sizeof(notprime));
for(int i = 2; i <= M; i++)
if(!notprime[i])
{
prime_1[num_1++] = i;
for(int j = 2 * i; j <= M; j += i)
notprime[j] = true;
}
}
void Prime2(int l, int u) // 第二次筛出给定范围内的素数
{
memset(notprime, false, sizeof(notprime));
num_2 = 0;
if(l < 2)
l = 2;
int k = sqrt(u * 1.0);
for(int i = 0; i < num_1 && prime_1[i] <= k; i++)
{
int t = l / prime_1[i];
if(t * prime_1[i] < l)
t++;
if(t <= 1)
t = 2;
for(int j = t; (long long)j * prime_1[i] <= u; j++) // 相乘会超范围,用long long
notprime[j * prime_1[i] - l] = 1;
}
for(int i = 0; i <= u - l; i++)
if(!notprime[i])
prime_2[num_2++] = i + l;
}
int main()
{
int l, u, dis, a_1, b_1, a_2, b_2, minn, maxx;;
Prime1();
while(scanf("%d%d", &l, &u) != EOF)
{
minn = INF, maxx = -1;
Prime2(l, u);
if(num_2 < 2)
{
printf("There are no adjacent primes.\n");
continue;
}
for(int i = 1; i < num_2 && prime_2[i] <= u; i++)
{
dis = prime_2[i] - prime_2[i - 1];
if(dis > maxx)
{
a_1 = prime_2[i - 1];
a_2 = prime_2[i];
maxx = dis;
}
if(dis < minn)
{
b_1 = prime_2[i-1];
b_2 = prime_2[i];
minn = dis;
}
}
printf("%d,%d are closest, %d,%d are most distant.\n", b_1, b_2, a_1, a_2);
}
return 0;
}

Prime Distance(二次筛素数)的更多相关文章

  1. poj 2689 Prime Distance(大区间筛素数)

    http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...

  2. POJ2689 Prime Distance(数论:素数筛选模板)

    题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...

  3. [ACM] POJ 2689 Prime Distance (筛选范围大素数)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12811   Accepted: 3420 D ...

  4. poj2689Prime Distance(大区间筛素数)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19635   Accepted: 5273 D ...

  5. POJ2689:Prime Distance(大数区间素数筛)

    The branch of mathematics called number theory is about properties of numbers. One of the areas that ...

  6. [POJ268] Prime Distance(素数筛)

    /* * 二次筛素数 * POJ268----Prime Distance(数论,素数筛) */ #include<cstdio> #include<vector> using ...

  7. poj 2689 Prime Distance (素数二次筛法)

    2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...

  8. POJ-2689 Prime Distance (两重筛素数,区间平移)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13961   Accepted: 3725 D ...

  9. ZOJ 1842 Prime Distance(素数筛选法2次使用)

    Prime Distance Time Limit: 2 Seconds      Memory Limit: 65536 KB The branch of mathematics called nu ...

随机推荐

  1. 解决for循环下变量显示一致的问题

    for(var i=0;i<10;i++){ setTimeOut(function(){ console.log("i:",i); },100) } 上面显示的打印出来结果 ...

  2. css控制超出部分自动省略...

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. 万能的搜索--之BFS(三)

    接着(一)start (二)广度优先搜索(BFS) 广度优先搜索(又称宽度优先搜索算法)是最简便的图的搜索算法之一,这一算法也是很多重要的图的算法的原型.   Dijkstra单源最短路径算法和Pri ...

  4. NOI2018_Day1_T1_归程

    题目描述 本题的故事发生在魔力之都,在这里我们将为你介绍一些必要的设定. 魔力之都可以抽象成一个 n 个节点.m 条边的无向连通图(节点的编号从 1 至 n). 我们依次用 l,a 描述一条边的长度. ...

  5. selenium——操作滚动条

    在自动化测试的过程中,难免会应用到翻页键,但是webdriver提供的方法都是操作当前页面可见的元素,对于未在当前范围展示的翻页键,该如何操作呢? 小编在这里介绍一种方法:使用JavaScript操作 ...

  6. Canvas标签

    1.Canvas标签: HTML5<canvas>元素用于图形的绘制,通过脚本(通常是javascript)来完成<canvas>标签只是图形容器,必须使用脚本来绘制图形.你可 ...

  7. 条款19:设计class犹如设计TYPE(Treat class design as type design)

    NOTE: 1.Class 的设计就是type的设计.在定义一个新type之前,请确认自己已经考虑过本条款所有主题(具体参考effective c++).

  8. python re 正则表达式

    元字符和其含义 . 匹配除换行符以外的任意字符 \ 转义字符,使后一个字符改变原来的意思 \w 匹配字母.数字.下划线:[A-Za-z0-9_] \W 匹配特殊字符:[^A-Za-z0-9_] \s ...

  9. Postfix telnet www.azengna.com 25 Connection Refused 但是localhost连接成功

    修改配置文件 vi /etc/postfix/main.cf 原先配置信息 .... inet_interfaces = all #inet_interfaces = $myhostname,loca ...

  10. POJ 1161 Walls(Floyd , 建图)

    题意: 给定n个城市, 然后城市之间会有长城相连, 长城之间会围成M个区域, 有L个vip(每个vip会处于一个城市里)要找一个区域聚会, 问一共最少跨越多少个长城. 分析: 其实这题难就难在建图, ...