poj 2689 Prime Distance 筛法+区间筛素数
来源:互联网 发布:自动阅读小说的软件 编辑:程序博客网 时间:2024/05/22 01:50
poj 2689 Prime Distance 筛法+区间筛素数
Prime Distance
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 16488 Accepted: 4384
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.
L和U的范围很大,所以不能直接使用线性筛,而L和U的距离不超过一百万,所以可以先找2~sqrt(INF)的素数,再用筛法找L和U之间的素数,这种筛法是找一个区间内的素数,比较经典,具体看代码:
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<queue>using namespace std;#define M 65538#define LL long longconst int maxn=1000001;bool visit[M];bool is[maxn];int ans[maxn];LL p[M];int cnt=0;void iii()//找2~sqrt(INF)的素数,65537是素数且大于sqrt(2^31-1);{ memset(visit,true,sizeof(visit)); visit[1]=0; for(long long i=2; i<M; ++i) { if(visit[i]==true) { p[cnt++]=i; for(long long j=i*i; j<M; j+=i) { visit[j]=false; } } }}int main(){ int n,m; iii(); while(~scanf("%d %d",&n,&m)) { for(int i=0; i<maxn; i++) is[i]=1; if (n == 1) n = 2; int d=sqrt(m+0.5); for (LL i = 0; i < cnt && p[i]<= d; i++)//区间筛法,找到大于等于n,小于等于m的素数 { int s = n/ p[i] + (n% p[i] > 0); if (s == 1) s = 2; for (LL j = s; j*p[i] <= m; j++) if (j*p[i] >= n) is[j*p[i]-n] = 0; } int c = 0; for (int i = 0; i <= m-n; i++) if (is[i]) ans[c++] = i + n; if (c < 2) { printf("There are no adjacent primes.\n"); continue; } else { int min1=ans[0],min2=ans[1],max1=ans[0],max2=ans[1]; for(int i=1; i<c-1; i++) { if(ans[i+1]-ans[i]<min2-min1) { min2=ans[i+1]; min1=ans[i]; } if(ans[i+1]-ans[i]>max2-max1) { max2=ans[i+1]; max1=ans[i]; } } printf("%d,%d are closest, %d,%d are most distant.\n",min1,min2,max1,max2); } } return 0;}
1 0
- 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(大区间素数筛选)
- [ACM] POJ 2689 Prime Distance (大区间素数筛选)
- POJ题目2689 Prime Distance(任何区间素数筛选)
- poj 2689 Prime Distance 二次筛素数
- poj 2689 Prime Distance(筛素数)
- POJ 2689 Prime Distance (大素数筛)
- 杭电-2084 数塔
- JS 对象直接量方法创建对象
- Max Sum(DP)
- 可重入与不可重入
- JNI入门学习笔记
- poj 2689 Prime Distance 筛法+区间筛素数
- 面向对象(上)-----课堂笔记
- MQ消息中间件技术
- 安装部署(四) Ubuntu 配置 Java MongoDB驱动
- [转]Android优质技术资源合集
- POJ 1011--Sticks
- 好用的网站
- Android Systemproperties用法
- 命令模式