链接:

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. Lombok - 使用注解让你的JavaBean变得更加简洁

    Lombok - 工具简介: Lombok是一个编译时注释预处理器,有助于在编译时注入一些代码.Lombok提供了一组在开发时处理的注释,以将代码注入到Java应用程序中,注入的代码在开发环境中立即可 ...

  2. hdu 2167 题解

    题目 题意 一个数字正方形(所有数都是两位的正整数),取了一个数后,它的周围 $ 8 $ 个数都不能被选,问最大取数总和. 注意数据范围 $ 3=< n <=15 $ 我们可能一开始会去想 ...

  3. Linux01学习第一天 man

    Linux标准的读音:哩呐科斯 Linux是一种类UNIX的系统,具有以下特点: 1.免费开源 2.模块化程度高 3.广泛的硬件支持 4.安全稳定 5.多用户,多任务(所以常应用于系统运维,以及合作开 ...

  4. Java两整数相除保留两位小数

    int num1 = 7; int num2 = 9; // 创建一个数值格式化对象 NumberFormat numberFormat = NumberFormat.getInstance(); / ...

  5. 括号匹配问题 —— Deque双端队列解法

    题目: 给定一个只包括 '(',')','{','}','[',']'?的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合.左括号必须以正确的顺序闭合.注意空字符串可 ...

  6. 全栈项目|小书架|服务器开发-NodeJS 项目分包

    唠嗑 参考的是慕课网七月老师的课程,七月的课质量真的挺高的,推荐一波.这次的小书架项目源码不会全部公开,因为用了七月老师课程的绝大部分代码.虽然代码不全,但是只要思路看得懂,代码实现就很简单了. 小书 ...

  7. puppet工作原理之模块使用

    一.模块介绍 1.什么是模块 通常情况把manifest文件分解成易于理解得结构,例如类文件,配置文件分类存放,并通过某种机制整合使用,这种机制就是模块,有助于结构化.层次化的方式使用puppet,p ...

  8. windows + Eclipse 汉化

    https://www.eclipse.org/babel/downloads.php 下载Eclipse 对应版本 汉化包解压 复制文件夹里的内容到eclipse 文件夹下对应的文件里 重启ecli ...

  9. js实现CheckBox全选或者不全选

    <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">< ...

  10. k8s 开源web操作平台

    https://kuboard.cn/install/install-dashboard.html kuborad