POJ 2689 Prime Distance
来源:互联网 发布:易语言网络验证 编辑:程序博客网 时间:2024/06/05 18:37
Prime Distance
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 12495 Accepted: 3334
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
题目大意:
给你两个数m,n让你求在m,n这个区间内,那两个素数的差最小,那两个素数的差最大。
题解:
原本一般的素数筛选就能解决此类问题,但是由于,m,n的值太大,因此的采用分段的素数筛选。因为m,n的取值到了2000000000以上,因此我们第一段的素数筛选的范围设定为50000,(50000*50000>2,147,483,647),并且题目中还提到m,n之间的差值不超过1000000,因此需要设定一个1000000大小的标记数组,同时在自己测试数据时,也要注意这一点。
#include<stdio.h>#include<string.h>bool flag[50010],f[1000010];//flag、f都是标记数组long long a[1000010],p[50010],p1[50010];//a、p、p1都数存储素数的数组long long z;void ss(){ long long i,j; z=0; memset(flag,true,sizeof(flag)); for (i = 2; i <=50000; i++) //求出50000之间的素数 { if (flag[i]) { p[z++]=i; for (j = i * i; j <=50000; j += i) { flag[j]=0; } } }}int main(){ long long m,n,i,cont,mmin,mmax,xmin,xmax,sz,k; ss(); while(~scanf("%I64d%I64d",&m,&n)) { mmin=2147483647; mmax=-1; cont=0; if(n<50000) { for(i=m;i<=n;i++) { if(flag[i] && i!=1)//注意判断m=1的情况 p1[cont++]=i;//存储这个区间中的素数 } if(cont>1) { for(i=0;i<cont-1;i++) { if(p1[i+1]-p1[i]>mmax)//取相差最大的两个素数 { mmax=p1[i+1]-p1[i]; xmax=i; } if(p1[i+1]-p1[i]<mmin)//取相差最小的两个素数 { mmin=p1[i+1]-p1[i]; xmin=i; } } printf("%I64d,%I64d are closest, %I64d,%I64d are most distant.\n",p1[xmin],p1[xmin+1],p1[xmax],p1[xmax+1]); } else//表示在这个区间内不存在素数或者只有一个素数 printf("There are no adjacent primes.\n"); } else { sz=n-m;//sz 的范围是不大于1000000的 for(i=0;i<=sz;i++) f[i]=true; for(i=0;i<z && p[i]*p[i]<=n;i++) { k=m/p[i];//从一个大于m的素数开始取 if(k*p[i]<m) k++; if(k<=1)//去除m为1的情况 ++k; while(k*p[i]<=n) { f[k*p[i]-m]=false; ++k; } } for(i=0;i<=sz;i++) { if(f[i]) a[cont++]=i+m; } if(cont>1) { for(i=0;i<cont-1;i++) { if(a[i+1]-a[i]>mmax) { mmax=a[i+1]-a[i]; xmax=i; } if(a[i+1]-a[i]<mmin) { mmin=a[i+1]-a[i]; xmin=i; } } printf("%I64d,%I64d are closest, %I64d,%I64d are most distant.\n",a[xmin],a[xmin+1],a[xmax],a[xmax+1]); } else printf("There are no adjacent primes.\n"); } } return 0;}
0 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 2689Prime Distance
- poj 2689 Prime Distance
- POJ 2689 Prime Distance
- POJ 2689 Prime Distance
- poj 2689 Prime Distance
- POJ 2689 Prime Distance
- svn 积累
- hdu4945(dp+组合数学)
- 因为一个调用疏忽引发的问题
- android广播(内部类)使用
- 关于CentOS系统中,文件权限第11位上是一个点的解读
- POJ 2689 Prime Distance
- voj P1677 陶陶的名字 kmp
- 400 Bad Request
- *a+=1和*a++的运算
- Linux系统巡检常用命令
- 物理rowid
- 图片3d轮放查看效果
- LeetCode: Wildcard Matching
- Visual Studio的配色方案(超级多)