POJ-2689-Prime Distance(素数区间筛法)
链接:
https://vjudge.net/problem/POJ-2689
题意:
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).
思路:
考虑素数区间筛法, 然后遍历一遍素数即可。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
using namespace std;
typedef long long LL;
const int INF = 1e9;
const int MAXN = 1e6+10;
LL l, r;
int Isprime[MAXN];
int Prime[MAXN];
int Islarge[MAXN];
int cnt;
void Euler()
{
memset(Isprime, 0, sizeof(Isprime));
memset(Islarge, 0, sizeof(Islarge));
cnt = 0;
int n = sqrt(r);
for (int i = 2;i <= n;i++)
{
if (Isprime[i] == 0)
Prime[++cnt] = i;
for (int j = i;j <= n/i;j++)
Isprime[j*i] = 1;
}
for (int i = 1;i <= cnt;i++)
{
int s = l/Prime[i];
int e = r/Prime[i];
for (int j = max(s, 2);j <= e;j++)
{
Islarge[1LL*Prime[i]*j-l] = 1;
}
}
}
int main()
{
while(~scanf("%lld%lld", &l, &r))
{
Euler();
/*
for (int i = 1;i <= cnt;i++)
cout << Prime[i] << ' ';
cout << endl;
*/
vector<int> p;
if (l == 1)
Islarge[0] = 1;
for (int i = 0;i <= r-l;i++)
{
if (Islarge[i] == 0)
p.push_back(i);
}
if (p.size() < 2)
puts("There are no adjacent primes.");
else
{
int mmax = 0, mmin = INF;
int mal, mar, mil, mir;
for (int i = 1;i < (int)p.size();i++)
{
if (p[i]-p[i-1] > mmax)
{
mmax = p[i]-p[i-1];
mal = p[i-1];
mar = p[i];
}
if (p[i]-p[i-1] < mmin)
{
mmin = p[i]-p[i-1];
mil = p[i-1];
mir = p[i];
}
}
mil += l, mir += l, mal += l, mar += l;
printf("%d,%d are closest, %d,%d are most distant.\n", mil, mir, mal, mar);
}
}
return 0;
}
POJ-2689-Prime Distance(素数区间筛法)的更多相关文章
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- 题解报告:poj 2689 Prime Distance(区间素数筛)
Description The branch of mathematics called number theory is about properties of numbers. One of th ...
- poj 2689 Prime Distance(大区间筛素数)
http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...
- poj 2689 Prime Distance (素数二次筛法)
2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...
- poj 2689 Prime Distance(区间筛选素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9944 Accepted: 2677 De ...
- 数论 - 素数的运用 --- poj 2689 : Prime Distance
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12512 Accepted: 3340 D ...
- [ACM] POJ 2689 Prime Distance (筛选范围大素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12811 Accepted: 3420 D ...
- POJ - 2689 Prime Distance (区间筛)
题意:求[L,R]中差值最小和最大的相邻素数(区间长度不超过1e6). 由于非素数$n$必然能被一个不超过$\sqrt n$的素数筛掉,因此首先筛出$[1,\sqrt R]$中的全部素数,然后用这些素 ...
- POJ 2689 Prime Distance (素数筛选法,大区间筛选)
题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...
- POJ 2689 Prime Distance (素数+两次筛选)
题目地址:http://poj.org/problem?id=2689 题意:给你一个不超过1000000的区间L-R,要你求出区间内相邻素数差的最大最小值,输出相邻素数. AC代码: #includ ...
随机推荐
- 修改Linux服务器中的MySql密码
1.可以直接在数据库中修改,因为知道root密码,所以直接登录 mysql -uroot -p 2.查看一下数据库,修改root密码需要使用如下图所示的mysql数据库 3.通过use mysql指明 ...
- 《Mysql - 幻读》
一:准备 - 为了深入了解幻读,准备数据. CREATE TABLE `t` ( `id` ) NOT NULL, `c` ) DEFAULT NULL, `d` ) DEFAULT NULL, PR ...
- 题解 CF1216D 【Swords】
大水题,感觉比C题水多了...(证明倒是挺难) 题目大意:额,这个(实在总结不出) 还是题目描述吧:仓库里有$n$种相同数量($x$把)的剑(但你不知道有多少),一天有$y$人闯进了仓库,每人拿了$z ...
- python学习-40 生产者和消费者模型
import time def buy(name): # 消费者 print('%s上街去买蛋' %name) while True: eggs=yield print('%s买了%s' %(name ...
- go guid 和uuid生成
1 安装 开始-运行 输入 cmd 回车 输入 go get -u github.com/typa01/go-utils 安装完毕后 2 使用 a 首先引入包 import ( goutil ...
- mysql创建唯一索引UNIQUE INDEX,以及报错“#失败原因: [Execute: Duplicate entry '733186700' for key 'uniq_video_id_index']”
要给t_video_prods表的video_id字段创建唯一所以,可以使用下面这条语句: alter table t_video_prods add UNIQUE INDEX `uniq_video ...
- Vue、SPA实现登陆
axios/qs/vue-axios安装及使用步骤 首先我们要下载三个依赖包,方便后面的开发使用需要: npm install axios -S axios是vue2提倡使用的轻量版的ajax.它 ...
- Security Access Control Strategy && Method And Technology Research - 安全访问控制策略及其方法技术研究
1. 访问控制基本概念 访问控制是网络安全防范和客户端安全防御的重要基础策略,它的主要任务是保证资源不被非法使用.保证网络/客户端安全最重要的核心策略之一. 访问控制包括 入网访问控制 网络权限控制 ...
- hdu 2181.。。。
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- sprint boot websocket 服务端+html5 示例测试
包依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...