poj 2689 Prime Distance 二次筛法
来源:互联网 发布:suse linux 安装教程 编辑:程序博客网 时间:2024/05/16 15:58
Prime Distance
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 10269 Accepted: 2772
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 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).
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).
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 1,000,000.
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
2 1714 17
Sample Output
2,3 are closest, 7,11 are most distant.There are no adjacent primes.
Source
Waterloo local 1998.10.17
求给定区间内的相邻素数之差的最大值与最小值
用筛选法求给定区间内的素数,再求相邻素数之差的最大值与最小值
用筛选法求给定区间内的素数,再求相邻素数之差的最大值与最小值
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <vector>#include <cmath>#define N 1000010#define inf 2147483647using namespace std;#define ll long longbool isP[N];ll p[N];int pn;ll s,t;void init(){ isP[1] = true; pn = 0; for(int i=2; i<N; i++) { if(!isP[i]) { p[++pn] = i; for(int j=i; 1ll*j*i<N; j++) isP[i*j] = true; } }}int main(){ init(); ll a,b,i,j; ll s,t; ll maxd,mind; ll x1,y1,x2,y2; while(scanf("%lld%lld",&a,&b)!=EOF) { if(a<2) a = 2; s = t = -1; maxd = -1; mind = inf; memset(isP,false,sizeof(isP)); for(i=1; i<=pn&&p[i]*p[i]<=b; i++) { j=max(a/p[i],p[i]); for(; p[i]*j<=b; j++) { if(p[i]*j<a) continue; isP[p[i]*j-a] = true; } } for(i=a; i<=b; i++) { if(!isP[i-a]) { s = t; t = i; if(s!=-1&&t!=-1) { if(mind>t-s) { mind = t-s; x1 = s; y1 = t; } if(maxd<t-s) { maxd = t-s; x2 = s; y2 = t; } } } } if(maxd==-1) printf("There are no adjacent primes.\n"); else printf("%lld,%lld are closest, %lld,%lld are most distant.\n",x1,y1,x2,y2); }}
- poj 2689 Prime Distance 二次筛法
- poj 2689 Prime Distance 二次筛素数
- poj Prime Distance(暴力 二次筛法)
- poj 2689 Prime Distance 筛法/二次筛法/区间素数
- Poj 2689 Prime Distance(素数筛+二次素数筛)
- POJ 2689 Prime Distance (二次?筛素数)
- POJ 2689 Prime Distance 二次筛选
- [数论] POJ 2689 Prime Distance 二次筛选
- poj 2689 Prime Distance 二次筛法,找出给定区间所有约数
- POJ 2689 Prime Distance 素数筛法
- POJ 2689 Prime Distance【素数筛法】
- poj 2689 Prime Distance
- POJ 2689 Prime Distance
- POJ 2689 Prime Distance
- poj 2689 Prime Distance
- POJ 2689 Prime Distance
- poj 2689 Prime Distance
- POJ 2689 Prime Distance
- C语言的编译链接过程的介绍
- 有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
- 在C 中子类继承和调用父类的构造函数方法
- RAID磁盘阵列数据恢复
- 多个USB视频捕捉的连接问题
- poj 2689 Prime Distance 二次筛法
- C++中为什么一定要出现虚函数,用函数覆盖不可以解决问题么
- nyoj-221-Tree
- 将字符串转化为时间,犯了一个低级错误
- 理解warden(安装启动)
- 交换数据方法总结
- 合作版机房收费系统SVN的安装步骤
- 【VB.NET2010】遍历进程,获取进程映像路径
- eclipse maven plugin 插件 安装 和 配置