Prime Distance(二次筛素数)
Description
(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
Output
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(二次筛素数)的更多相关文章
- poj 2689 Prime Distance(大区间筛素数)
http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...
- POJ2689 Prime Distance(数论:素数筛选模板)
题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...
- [ACM] POJ 2689 Prime Distance (筛选范围大素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12811 Accepted: 3420 D ...
- poj2689Prime Distance(大区间筛素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19635 Accepted: 5273 D ...
- POJ2689:Prime Distance(大数区间素数筛)
The branch of mathematics called number theory is about properties of numbers. One of the areas that ...
- [POJ268] Prime Distance(素数筛)
/* * 二次筛素数 * POJ268----Prime Distance(数论,素数筛) */ #include<cstdio> #include<vector> using ...
- poj 2689 Prime Distance (素数二次筛法)
2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...
- POJ-2689 Prime Distance (两重筛素数,区间平移)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13961 Accepted: 3725 D ...
- ZOJ 1842 Prime Distance(素数筛选法2次使用)
Prime Distance Time Limit: 2 Seconds Memory Limit: 65536 KB The branch of mathematics called nu ...
随机推荐
- python制作二维码
1.安装MyQR pip install MyQR 2.打开pycharm,新建项目文件 from MyQR import myqr 2.1黑白二维码 2.2,彩色动态二维码 3.当前项目本地 ...
- CAD交互绘制多段线(网页版)
多段线又被称为多义线,表示一起画的都是连在一起的一个复合对象,可以是直线也可以是圆弧并且它们还可以加不同的宽度. 主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下 ...
- Open Cascade创建自己的MFC文档程序
项目初始设置在Visual studio中创建一个单文档MFC项目(本例以MFCTest为名称): 在项目属性的VC++页面设置包含目录.库目录,在链接器的输入中添加OCC库目录下的所有.lib文件名 ...
- Eaton Char-Lynn Motor : Performance Of Small Displacement Motors
The small-displacement supercharged motor replaces the large-displacement motor with the speed of li ...
- python3 yum not found
vi /urs/bin/yum 将#!/usr/bin/python的python改为python2.x(你系统的python2的版本)
- check.pl
比对两个文件并纠错 #!/usr/bin/perl use strict; use warnings; ###############################################m ...
- springmvc下载那些事
文件的上传下载一般在项目中还是非常实用的,此处专门整理一下文件的下载,至于文件的上传实现将在后续中补上.文件的下载多用于模板文件的下载,这在项目中用的还是挺多的.今天用到了就整理出来了,以供搬运工们借 ...
- UITextView与UITextfield的区别
IOS中的UITextView和UITextField都是文本输入控件并都能够调用系统键盘.本次特酷把介绍UITextView和UITextField的区别.简单来说,UITextView和UITex ...
- json_decode 解析带BOM头文件错误
//取前三个字符 并转化为ASCII 判断是否为BOM文件 $charset[1] = substr($result, 0, 1); $charset[2] = substr($result, 1, ...
- Java 常用集合笔记
自增数组 ArrayList<Integer>G[]=new ArrayList[N] 详细笔记 相关题目 栈 Stack<Integer> stack=new Stack&l ...