题目链接:传送门

题目:

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. JNDI学习总结(一)——JNDI数据源的配置(转)

    一.数据源的由来 在Java开发中,使用JDBC操作数据库的四个步骤如下:   ①加载数据库驱动程序(Class.forName("数据库驱动类");)   ②连接数据库(Conn ...

  2. Oracle如何解决日期格式“01-3月 -18”的显示问题(3种处理方式)

    今天查询表数据还是出现上次那种问题,但是每次都要去调用转化函数,比较麻烦,所以找一下资料,得到几种方式解决oralce的日期数据显示格式 问题描述: 解决方法 1)方法1:调用Oracle函数转化成日 ...

  3. 8188EU 在AM335X MC183上以AP+STA工作

    [目的] 8188EU 在AM335X MC183上以AP+STA工作. [环境] 1.  Ubuntu 16.04发行版 2.  linux-3.2.0-psp04.06.00.11 3.  MC1 ...

  4. Java 实现倒计时(由秒计算天、小时、分钟、秒)

    public class Countdown4 { private static long day = 0; private static long hour = 0; private static ...

  5. python-列表,元组,range

    # 列表# lst = ["光头强", 1, True, {}, (1, ), {123}, ["周杰伦",[], "周杰", " ...

  6. vue-router-6-命名视图

    //展示多个视图<router-view class="view one"></router-view> <router-view class=&qu ...

  7. 三:使用docker-machine安装虚拟机上的docker

    1.docker安装之后自带docker-machine:(需要win10专业版或mac) 2.如何远程管理一个docker-machine?(以下是Mac环境) 关闭本地的docker应用.运行do ...

  8. 第三篇 功能实现(2) (Android学习笔记)

    第三篇 功能实现(2) ●Activity的四种启动模式 Activity的启动模式有四种,分别是standard.singleTop.singleTask和singleInstance. 在Andr ...

  9. 4.1 C++多态的概念及前提条件

    参考:http://www.weixueyuan.net/view/6370.html 总结: 而多态的功能则是将函数名动态绑定到函数入口地址,这样的动态绑定过程称为运行期绑定. 而在运行期绑定的函数 ...

  10. mabatis学习(四)----解决字段名与实体类属性名不同的冲突

    在项目开发中,数据库中的字段名不一定和实体类的类名完全相同(当然大小写忽略),那么就可以在sql映射文件中解决此问题 一.创建需要的数据库和表 编写sql脚本,在navicat for mysql中执 ...