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. python基础一day4 元组

    结果:     join:返回一个字符串     列表转化为字符串       可迭代对象都可以     结果:    不报错什么也不执行  结果:

  2. OpenCV:应用篇

    手势跟踪识别 车牌检测 人脸识别 去雾 图像阈值分割提取

  3. 从多表连接后的select count(*)看待SQL优化

    从多表连接后的select count(*)看待SQL优化 一朋友问我,以下这SQL能直接改写成select count(*) from a吗? SELECT COUNT(*) FROM a LEFT ...

  4. Linux基础学习-通过VM安装RHEL7.4

    虚拟机安装RHEL7.4 1.VM虚拟机设置 这里我们配置的虚拟机为1核1G,50G硬盘,NAT模式 2.Linux安装 这里时区我们选择中国上海,时间需要调整一下相差8小时. 这里添加一下中文语言支 ...

  5. jquery 打星评分插件

    <link rel="stylesheet" href="/static/vendor/raty/jquery.raty.css"> <scr ...

  6. MySQL 优化 之 Copying to tmp table on disk

    项目中遇到了慢查询问题 Sql语句 SELECT sum(price) AS price, `member_id` FROM `crm_upload` GROUP BY member_id ORDER ...

  7. 给Django中的url起名字

    url反转  =>reverse 1.from django.shortcuts  import  reverse 2. 利用reverse函数对URL名称进行反转  reverse(url名称 ...

  8. P1880 NOIP 1995石子合并

    复习(du) 这道题,发现思想真不错 当时背板子打下来的 要下晚自习了,明天更注释吧 #include<iostream> #include<queue> #include&l ...

  9. Java学习之接口概念

    Java语言只支持单重继承,不支持多继承,即一个类只能有一个父类.但是在实际应用中,又经常需要使用多继承来解决问题.为了解决该问题,Java语言提供接口来实现类的多继承问题. 接口(英文interfa ...

  10. jquery获取ul中li的值