题目链接:传送门

题目:

Prime Distance
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: Accepted: Description
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 and itself). The first prime numbers are ,,, 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, , are the only adjacent primes that are also adjacent numbers.
Your program is given numbers: L and U (<=L< U<=,,,), 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
Each line of input will contain two positive integers, L and U, with L < U. The difference between L and U will not exceed ,,. Output
For each L and U, the output will either be the statement that there are no adjacent primes (because there are less than two primes between the two given numbers) or a line giving the two pairs of adjacent primes. Sample Input Sample Output , are closest, , are most distant.
There are no adjacent primes.

思路:

大区间素数筛选。预处理小素数,拿去筛大素数就好了。

上kuangbin大大的模板。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std;
const int MAX_N = 1e5 + ; int prime[];
void getPrime() {
memset(prime, , sizeof prime);
for (int i = ; i < MAX_N; i++) {
if (!prime[i]) prime[++prime[]] = i;
for (int j = ; j <= prime[] && prime[j] <= MAX_N/i; j++) {
prime[prime[j]*i] = ;
if (i%prime[j] == ) break;
}
}
} bool notprime[];
int prime2[MAX_N];
void getPrime2(int L, int R) {
memset(notprime, false, sizeof notprime);
if (L < ) L = ;
for (int i = ; i <= prime[] && (long long)prime[i]*prime[i] <= R; i++)
for (int j = max(, L/prime[i] + (L%prime[i] > )); (long long)j*prime[i] <= R; j++)
if ((long long)j*prime[i] >= L)
notprime[j*prime[i]-L] = true;
prime2[] = ;
for (int i = ; i <= R-L; i++)
if (!notprime[i])
prime2[++prime2[]] = i+L;
} int main()
{
getPrime();
int L, U;
while (~scanf("%d%d", &L, &U)) {
getPrime2(L, U);
if (prime2[] < ) puts("There are no adjacent primes.");
else {
int x1 = , x2 = 1e7, y1 = , y2 = ;
for (int i = ; i <= prime2[]; i++) {
if (prime2[i] - prime2[i-] < x2 - x1)
x2 = prime2[i], x1 = prime2[i-];
if (prime2[i] - prime2[i-] > y2 - y1)
y2 = prime2[i], y1 = prime2[i-];
}
printf("%d,%d are closest, %d,%d are most distant.\n", x1, x2, y1, y2);
}
}
return ;
}

POJ2689 Prime Distance(数论:素数筛选模板)的更多相关文章

  1. POJ-2689 Prime Distance,区间素数筛法

                                                    Prime Distance 只会埃氏筛法的弱鸡今天读了读挑战程序设计120页,明白了求小区间内素数的方 ...

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

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

  3. POJ 2689 Prime Distance(素数筛选)

    题目链接:http://poj.org/problem?id=2689 题意:给出一个区间[L, R],找出区间内相连的,距离最近和距离最远的两个素数对.其中(1<=L<R<=2,1 ...

  4. poj2689 Prime Distance(素数区间筛法)

    题目链接:http://poj.org/problem?id=2689 题目大意:输入两个数L和U(1<=L<U<=2 147 483 647),要找出两个相邻素数C1和C2(L&l ...

  5. POJ-2689 Prime Distance (两重筛素数,区间平移)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13961   Accepted: 3725 D ...

  6. 解题报告:poj2689 Prime Distance

    2017-10-03 11:29:20 writer:pprp 来源:kuangbin模板 从已经筛选好的素数中筛选出规定区间的素数 /* *prime DIstance *给出一个区间[L,U],找 ...

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

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

  8. UVA 10140 - Prime Distance(数论)

    10140 - Prime Distance 题目链接 题意:求[l,r]区间内近期和最远的素数对. 思路:素数打表,打到sqrt(Max)就可以,然后利用大的表去筛素数.因为[l, r]最多100W ...

  9. PAT甲题题解-1059. Prime Factors (25)-素数筛选法

    用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...

随机推荐

  1. 牛客网 PAT 算法历年真题 1002 :数字分类 (20)

    1002 :数字分类 (20) 时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 给定一系列正整数,请按要求对数字 ...

  2. git开发过程的配置和使用

    git开发过程的使用 1.创建仓库 2.新建项目,填写项目名称等信息 3.初始化仓库,创建git仓库 git init 4.配置个人信息(配置过可忽略) git config --global use ...

  3. 用linux命令连接无线网络-转载

    首先是用到的工具: ifconfigrouteiwlistiwconfig 后两个是无线工具 从现在开始,按我的步骤做 (##后面的是说明部分) 1.开启无线,如果是笔记本,开启无线开关,或用Fn+F ...

  4. 分布式链路追踪(Sleuth、Zipkin)

    技术背景 在微服务架构中,随着业务发展,系统拆分导致系统调用链路愈发复杂,一个看似简单的前端请求可能最终需要调用很多次后端服务才能完成,那么当整个请求出现问题时,我们很难得知到底是哪个服务出了问题导致 ...

  5. prototype:构造函数的真相、原型链

    函数不是构造函数,但是当且仅当使用 new 时,函数调用会变成 ‘构造函数调用’.那么对 ’构造函数‘ 最准确的解释是:所有带 new 的函数调用. Nothing 只是一个普通的函数,但使用 new ...

  6. POST提交表单时EnType设置问题

    POST提交表单时EnType设置问题 首先知道enctype这个属性管理的是表单的MIME编码.共有三个值可选: 1.application/x-www-form-urlencoded 2.mult ...

  7. 稀疏 部分 Checkout

    To easily select only the items you want for the checkout and force the resulting working copy to ke ...

  8. 读书笔记 C# Type类型与泛型有关的某些属性浅析

    IsGenericType 如果类型为泛型,则返回 true. GetGenericArguments 返回 Type 对象数组,这些对象表示为构造类型提供的类型变量,或泛型类型定义的类型参数.如果是 ...

  9. word-wrap与break-word属性的区别

    共同点 word-wrap:break-word与word-break:break-all都能把长单词强行断句 不同点 word-wrap:break-word会首先起一个新行来放置长单词,新的行还是 ...

  10. Android : 网络adb配置及有线端口占用解决方法

    一.调试环境: Android Debug Bridge version 1.0.40: Nexus6P平板(Android 8.0系统): 二.网络ADB调试: 1. Android设备除了用有线u ...