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 ...
随机推荐
- LinuxMint 编译 LittlevGL GUI
编译必须 安装arm-linux-gcc-4.4.3.tar.gz PC simulator You can try out the Littlev Graphics Library using on ...
- 网页添加tittle前的图标logo
在head标签中 <link rel="icon" href="~/favicon.ico" type="image/x-icon" ...
- C-基础:冒泡排序
#include <fstream> #include <iostream.h> //--------------------------------------------- ...
- MySQL-03 SQL语句设计
学习要点 SQL语句分类 DML语句 DML 查询语句 SQL语句分类 数据操纵语言(DML):用来操纵数据库中数据的命令.包括:SELECT.INSERT.UPDATE.DELETE. 数据定义语言 ...
- pc端移动端兼容的大图轮播
body, html { width: 100%; } * { margin:; padding:; list-style: none; } .haha { list-style-type: none ...
- dubbo负载均衡策略和集群容错策略
dubbo负载均衡策略 random loadbalance 默认情况下,dubbo是random load balance随机调用实现负载均衡,可以对provider不同实例设置不同的权重,会按照权 ...
- java用XSSFWorkbook实现读写Excel
/** * 读取Excel文件的内容 * @param inputStream excel文件,以InputStream的形式传入 * @param sheetName sheet名字 * @retu ...
- 20. ROUTINES
20. ROUTINES ROUTINES表提供有关存储例程(存储过程和存储函数)的信息. ROUTINES表不包含内置SQL函数或用户定义函数(UDF). 名为mysql.proc Name的列表示 ...
- 第七章:systemverilog过程语句
systemverilog增加了一些新的操作符和过程语句: 1.新的操作符 递增/递减 赋值操作符 设置成员操作符inside 有无关通配符==?/!=? 操作数改进(类型/尺寸/符号强制转换) 2. ...
- python_random模块
random模块 import random print(random.random()) # 大于0且小于1之间的小数 print(random.randint(1, 6)) # 大于等于1且小于等 ...