POJ2689 Prime Distance(数论:素数筛选模板)
题目链接:传送门
题目:
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(数论:素数筛选模板)的更多相关文章
- POJ-2689 Prime Distance,区间素数筛法
Prime Distance 只会埃氏筛法的弱鸡今天读了读挑战程序设计120页,明白了求小区间内素数的方 ...
- POJ 2689 Prime Distance (素数筛选法,大区间筛选)
题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...
- POJ 2689 Prime Distance(素数筛选)
题目链接:http://poj.org/problem?id=2689 题意:给出一个区间[L, R],找出区间内相连的,距离最近和距离最远的两个素数对.其中(1<=L<R<=2,1 ...
- poj2689 Prime Distance(素数区间筛法)
题目链接:http://poj.org/problem?id=2689 题目大意:输入两个数L和U(1<=L<U<=2 147 483 647),要找出两个相邻素数C1和C2(L&l ...
- POJ-2689 Prime Distance (两重筛素数,区间平移)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13961 Accepted: 3725 D ...
- 解题报告:poj2689 Prime Distance
2017-10-03 11:29:20 writer:pprp 来源:kuangbin模板 从已经筛选好的素数中筛选出规定区间的素数 /* *prime DIstance *给出一个区间[L,U],找 ...
- poj 2689 Prime Distance (素数二次筛法)
2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...
- UVA 10140 - Prime Distance(数论)
10140 - Prime Distance 题目链接 题意:求[l,r]区间内近期和最远的素数对. 思路:素数打表,打到sqrt(Max)就可以,然后利用大的表去筛素数.因为[l, r]最多100W ...
- PAT甲题题解-1059. Prime Factors (25)-素数筛选法
用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...
随机推荐
- Windows系统目录解释
目录 说明 C:\Program Files 64位程序安装目录 C:\Program Files (x86) 32位程序安装目录 C:\Windows 操作系统主要目录 C:\Windows\Sys ...
- 学习笔记-AngularJs(三)
学习笔记-AngularJs(二)写了个所有程序语言入门时都必须要写的Hello World,那么从现在开始做那个之前说过的互联网大佬介绍的学习例子,当然这里开始会慢慢按照之前说过的目录来搭建这个学习 ...
- 原生js(form)验证,可以借鉴下思路,应用到工作中
我在工作中时常使用form验证,在目前的公司做的表单验证用的angular的form组件,对于一个有追求的前端,或者应用在移动端写个form验证,引入angular或者jquery组件等验证,难免显得 ...
- os 模块 和 os模块下的path模块
import os # os 主要用于与操作系统进行交互 #获取当前的工作目录 print(os.getcwd()) #切换工作目录 os .chdir("D:\上海python全栈4期\d ...
- flask下载zip文件报错TypeError
报错内容:TypeError: make_conditional() got an unexpected keyword argument 'accept_ranges' 报错行自己代码如下: dir ...
- dgango中admin下添加搜索功能
admin下添加搜索功能: 在表单中加入search_fields = ['ip','hostname'] 可模糊匹配 当有人在管理搜索框中进行搜索时,Django将搜索查询分解成单词,并返回包含 ...
- day044 cssy其他样式 js初识
float: 浮动 .t1{ float: right/left; } 关于浮动的两个特点: 1.浮动的框可以向左或向右移动,知道他的外边缘碰到包括框或另一个浮动框的边框为止. 2.由于浮动框不在文档 ...
- day16 初识面向对象
今天主要学习内容: 1.初始面向对象 2 .类 ,对象 3,面向对象和面向过程的对比 4.面向对象的三大特征 1,初始面向对象 面向过程 : 一切以实物的发展流程为中心 面向对象: 一切以对象为中心, ...
- 重载的方式写Python的post请求
#encoding=utf-8#__author__="Lanyangyang" import unittestimport requestsimport json # This ...
- Win10安装mysql-8.0.11-winx64详细步骤
安装 mysql-8.0.11-winx64 https://blog.csdn.net/qq_20788055/article/details/80372577