Prime Distance
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13961 | Accepted: 3725 |
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).
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 17 14 17
Sample Output
2,3 are closest, 7,11 are most distant. There are no adjacent primes. 题目大意:给一个区间,找出这个区间内相邻的两个素数中差最大和最小的两个。题目解析:两种方法,一种是 枚举 + Miller_Rabbin快速判定素数,另一种是将标记数组区间平移,两次筛素数。 第一种方法:
1 # include<iostream> 2 # include<cstdio> 3 # include<cstring> 4 # include<cstdlib> 5 # include<algorithm> 6 using namespace std; 7 # define ll long long 8 unsigned mypow(unsigned a,unsigned b,unsigned m) 9 { 10 if(b==0) 11 return 1; 12 if(b==1) 13 return a%m; 14 ll temp=mypow(a,b/2,m); 15 temp*=temp; 16 temp%=m; 17 if(b&1) 18 temp*=a; 19 temp%=m; 20 return temp; 21 } 22 bool Miller_Rabbin(unsigned x) 23 { 24 if(x==2) 25 return true; 26 for(int i=1;i<=2;++i){ 27 unsigned a=rand()%(x-2)+2; 28 if(mypow(a,x-1,x)!=1) 29 return false; 30 } 31 return true; 32 } 33 int main() 34 { 35 unsigned a,b; 36 unsigned l1,l2,r1,r2,t; 37 int minn,maxn; 38 while(scanf("%u%u",&a,&b)!=EOF) 39 { 40 if(a==1) 41 a=2; 42 minn=1000005; 43 maxn=0; 44 l1=l2=r1=r2=a; 45 bool yy=true; 46 for(int i=a;i<=b;++i){ 47 if(Miller_Rabbin(i)){ 48 if(yy){ 49 t=l1=r1=a; 50 yy=false; 51 } 52 else{ 53 if(minn>i-t){ 54 minn=i-t; 55 l1=t; 56 l2=i; 57 } 58 if(maxn<i-t){ 59 maxn=i-t; 60 r1=t; 61 r2=i; 62 } 63 } 64 t=i; 65 } 66 } 67 if(maxn==0){ 68 printf("There are no adjacent primes.\n"); 69 }else 70 printf("%u,%u are closest, %u,%u are most distant.\n",l1,l2,r1,r2); 71 } 72 return 0; 73 }
这种暴力的方法效率不高,在UVa上取30个随机数能AC,在ZOJ上取20个随机数能AC,但在POJ上无论如何都AC不了。原因就是这三个OJ对时间的要求分别是3s,2s,1s。
下面是两重筛的实现。标记数组用的很灵活。
1 # include<iostream> 2 # include<cstdio> 3 # include<cmath> 4 # include<map> 5 # include<vector> 6 # include<cstring> 7 # include<algorithm> 8 using namespace std; 9 const int N=46340; 10 int pri[N],mark[1000010],cnt; 11 vector<unsigned>v; 12 void init() 13 { 14 cnt=0; 15 fill(mark,mark+N+5,1); 16 for(int i=2;i<=N;++i){ 17 if(mark[i]) 18 pri[cnt++]=i; 19 for(int j=0;j<cnt&&i*pri[j]<=N;++j){ 20 mark[i*pri[j]]=0; 21 if(i%pri[j]==0) 22 break; 23 } 24 } 25 } 26 void work(unsigned a,unsigned b) 27 { 28 v.clear(); 29 if(a==1) 30 ++a; 31 memset(mark,0,sizeof(mark)); 32 for(int i=0;i<cnt;++i){ 33 if(pri[i]>b) 34 break; 35 for(int c=a/pri[i];c*pri[i]<=b;++c){ 36 if(c<=1) 37 continue; 38 if(c*pri[i]<a) 39 continue; 40 mark[c*pri[i]-a]=1; 41 } 42 } 43 for(int i=0;i<=b-a;++i){ 44 if(mark[i]==0) 45 v.push_back(i+a); 46 } 47 } 48 void solve() 49 { 50 int l=v.size(); 51 if(l<2){ 52 printf("There are no adjacent primes.\n"); 53 return ; 54 } 55 int minn=1<<30,maxn=0; 56 unsigned l1,l2,r1,r2; 57 for(int i=1;i<l;++i){ 58 if(minn>v[i]-v[i-1]){ 59 minn=v[i]-v[i-1]; 60 l1=v[i-1]; 61 l2=v[i]; 62 } 63 if(maxn<v[i]-v[i-1]){ 64 maxn=v[i]-v[i-1]; 65 r1=v[i-1]; 66 r2=v[i]; 67 } 68 } 69 printf("%u,%u are closest, %u,%u are most distant.\n",l1,l2,r1,r2); 70 } 71 int main() 72 { 73 init(); 74 unsigned a,b; 75 while(scanf("%u%u",&a,&b)!=EOF) 76 { 77 work(a,b); 78 solve(); 79 } 80 return 0; 81 }