链接:

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(素数区间筛法)的更多相关文章

  1. poj 2689 Prime Distance(大区间素数)

    题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...

  2. 题解报告:poj 2689 Prime Distance(区间素数筛)

    Description The branch of mathematics called number theory is about properties of numbers. One of th ...

  3. poj 2689 Prime Distance(大区间筛素数)

    http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...

  4. poj 2689 Prime Distance (素数二次筛法)

    2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...

  5. poj 2689 Prime Distance(区间筛选素数)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9944   Accepted: 2677 De ...

  6. 数论 - 素数的运用 --- poj 2689 : Prime Distance

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12512   Accepted: 3340 D ...

  7. [ACM] POJ 2689 Prime Distance (筛选范围大素数)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12811   Accepted: 3420 D ...

  8. POJ - 2689 Prime Distance (区间筛)

    题意:求[L,R]中差值最小和最大的相邻素数(区间长度不超过1e6). 由于非素数$n$必然能被一个不超过$\sqrt n$的素数筛掉,因此首先筛出$[1,\sqrt R]$中的全部素数,然后用这些素 ...

  9. POJ 2689 Prime Distance (素数筛选法,大区间筛选)

    题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...

  10. POJ 2689 Prime Distance (素数+两次筛选)

    题目地址:http://poj.org/problem?id=2689 题意:给你一个不超过1000000的区间L-R,要你求出区间内相邻素数差的最大最小值,输出相邻素数. AC代码: #includ ...

随机推荐

  1. LeetCode 206. 反转链表(Reverse Linked List) 16

    206. 反转链表 206. Reverse Linked List 题目描述 反转一个单链表. 每日一算法2019/5/19Day 16LeetCode206. Reverse Linked Lis ...

  2. SQL 先固定特殊的几行数据之外再按照某一字段排序方法(CASE 字段排序(CASE WHEN THEN)

    查询用户表的数据,管理员用户始终在最前面,然后再按照CreateTime排序: SELECT TOP * FROM [dbo].[User] WHERE ParentID = '**' ORDER B ...

  3. IdentityServer4 学习二

    进入identityserver4的官网:https://identityserver.io/ 找到文档 从overview下开始按照官方文档练习: 安装自定义模板 dotnet new -i Ide ...

  4. python 使用API调用和风天气获取天气情况并保存

    第一步.注册注册免费API和阅读技术文档: 注册地址:https://console.heweather.com 注册完成后,激活登录后,新建应用 .新建key KEY名称 密钥ID 密钥 类型下载城 ...

  5. golang之结构体结构体嵌入和匿名成员

    考虑一个二维的绘图程序,提供了一个各种图形的库,例如矩形.椭圆形.星形和轮形等几何形状.这里是其中两个的定义: type Circle struct { X, Y, Radius int } type ...

  6. Python的json操作

    对数据:    json = json.dumps(data)  编码  dict->string  排序sort_keys=True, 缩进indent=4, 分隔符separators=(' ...

  7. css 省略号的写法

    单行省略号 overflow: hidden; text-overflow:ellipsis; white-space: nowrap; width:500px; 多行省略号 overflow: hi ...

  8. java 读取文件流

    搬运自速学堂:https://www.sxt.cn/Java_jQuery_in_action/ten-iqtechnology.html JAVA中IO流体系: 四大IO抽象类 ·InputStre ...

  9. python入门-windows下python环境搭建

    1. 下载安装包 选择executable版,根据自己电脑的操作系统选择是32位还是64为. python3.6-64位下载 python3.6-32位下载 2. 安装python 下载之后是这样的 ...

  10. Java内存模型之从JMM角度分析DCL

    DCL,即Double Check Lock,中卫双重检查锁定.其实DCL很多人在单例模式中用过,LZ面试人的时候也要他们写过,但是有很多人都会写错.他们为什么会写错呢?其错误根源在哪里?有什么解决方 ...