题目链接:传送门

题目:

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. pickle file in matlab

    Load pickle files in Matlab Posted on June 12, 2013 by xcorr   http://xcorr.net/2013/06/12/load-pick ...

  2. Win10系列:VC++绘制几何图形3

    在绘制三角形之前,首先需要创建一个三角形,打开D2DBasicAnimation.h头文件,在D2DBasicAnimation类中添加如下的代码: private:     //声明成员变量obje ...

  3. 红黑树与AVL

     红黑树和avl树都属于自平衡二叉树: 两者查找.插入.删除的时间复杂度相同: 包含n个内部结点的红黑树的高度是o(logn); TreeMap是一个红黑树的实现,能保证插入的值保证排序       ...

  4. hdu1695

    题解: 莫比乌斯反演 设f[i]=Σgcd(i,j)%z==0 则f[i]=Σgcd(i,j)==zd 成莫比乌斯反演关系 代码: #include<cstdio> #include< ...

  5. Docker的安装以及使用Docker安装jenkins,gogs,tomcat(一)

    (1)Docker的安装  官网安装链接 :https://yeasy.gitbooks.io/docker_practice/content/ 卸载旧版本 旧版本的 Docker 称为 docker ...

  6. day20 类的约束

    今日所学 : 1 .类的约束 2 .异常处理 try except raise 3. MD5加密 4. 日记处理(不要记,留一份,侧重点再用) 1 .类的约束 1) 写一个父类,父类中的某个方法要抛出 ...

  7. transiton,transform,animation,border-image

    animation,transition,transform三者联系与区别: https://www.jianshu.com/p/0e0e1903b80d transform: 使用小技巧: tran ...

  8. dapper 简单多表查询

    public List<Book> GetBookList() { List<Book> bList = null; try { using (var t = new SqlC ...

  9. Linux系统管理常用命令用法总结(2)

    1.free指令会显示内存的使用情况,包括实体内存,虚拟的交换文件内存,共享内存区段,以及系统核心使用的缓冲区等. 语法:free [-bkmotV][-s <间隔秒数>] 参数说明: - ...

  10. Linux文件系统命令 ln

    命令:ln 功能:Linux下文件的链接功能,区别,软链接需要-s选项,硬链接不需要.相同的是,都是同步变化的,不过软链接不需要占用空间,硬链接占用空间 用法:软链接:ln -s 源文件 目标文件 硬 ...