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. Java集合(五)--LinkedList源码解读

    首先看一下LinkedList基本源码,基于jdk1.8 public class LinkedList<E> extends AbstractSequentialList<E> ...

  2. 硬盘写入 iso

    https://www.jb51.net/softjc/508796.html WinImage 正确操作是要有两个or以上-硬盘. 这样才能写入你要装的操作系统 测试或者安装

  3. 三、C++ const分析

    1.C语言中的const: const修饰的变量是只读的,本质还是变量 const修饰的局部变量在栈上分配空间 const修饰的全局变量在只读存储区分配空间 const只在编译期有用,在运行期无效 c ...

  4. Linux CentOS 知识和常用命令

    1.常用热键 [Tab]它具有“命令补全”与“文件补全”的功能[Ctrl+C]中断执行中的程序组合键[Ctrl+d]键盘输入结束.也可以用来替代 exit 2.Linux 常用编辑器 vi 和 vim ...

  5. IDEA ctrl+alt+L 格式化快捷键无效时解决

    这几天发现自己Intellij IDEA ctrl+alt+L格式化代码无效 设置里面按照快捷键搜索 按了 ctrl+alt+L 也没反应 但是我设置的确实是默认的 ctrl+alt+L 最后终于找到 ...

  6. Shell函数和正则表达式

    1. shell函数 shell中允许将一组命令集合或语句形成一段可用代码,这些代码块称为shell函数.给这段代码起个名字称为函数名,后续可以直接调用该段代码. 格式: func() {   #指定 ...

  7. 2019年,Python工程师必考的6个面试题,Python面试题No5

    第1题:Python里面如何实现tuple和list的转换? 函数tuple(seq)可以把所有可迭代的(iterable)序列转换成一个tuple, 元素不变,排序也不变 list转为tuple: ...

  8. 【HDU 6153】A Secret (KMP)

    Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,whi ...

  9. SpringMVC修改功能

    articleList.jsp <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" ...

  10. ACboy needs your help(分组背包)

    ACboy has N courses this term, and he plans to spend at most M days on study.Of course,the profit he ...