Prime Distance
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).
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.
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.
2 17
14 17
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.
题意:给出一个区间,问你相邻的距离最大的和最小的分别是谁和谁
思路:由于l和r的范围很大,不可能全部不素数筛出来,因为是int类型内的,所以所有的合数的最大因子为2^16,这样就可以根据前面晒出来的的素数将后面的合数筛掉了。
有一个点超级坑,就是L=1的时候,这个时候不做一些标记的话,最小肯定是1,2了,要注意这一点
对于区间内连两个素数都没有的直接输出没有就行了,剩下的就是暴力枚举了
对于区间l,r之间的素数,如果L/prime是整数,那么就当前开始即可,对于不是整数的,可定要在+1,是的temp*prime>L
#include<iostream> #include<stdio.h> #include<string.h> #include<string> #include<cmath> using namespace std; #define ll long long #define mod 1000000007 #define N 1000010 ll prime[N]; int vis[N]; ll prime2[N]; int k; void getprime(int n) { k=1; for(int i=2;i<n;i++) { if(!vis[i]) prime[k++]=i; for(int j=1;j<k&&i*prime[j]<n;j++) { vis[i*prime[j]]=1; if(i%prime[j]==0) break; } } } int num; void getprime2(int l,int r) { memset(vis,0,sizeof(vis)); for(int i=1;i<k&&prime[i]*prime[i]<r;i++) { int temp=l/prime[i]; if(l%prime[i]!=0) temp++; if(temp==1) temp=2; for(int j=temp;j*prime[i]<=r;j++) if(j*prime[i]>=l) vis[j*prime[i]-l]=1; } num=0; for(int i=0;i<=r-l;i++) if(!vis[i]) prime2[num++]=i+l; } int main() { getprime(N); int l,r; while(cin>>l>>r) { memset(prime2,0,sizeof(prime2)); memset(vis,0,sizeof(vis)); if(l==1||l==0) l=2; getprime2(l,r); if(num<2) cout<<"There are no adjacent primes."<<endl; else { int x1=0,x2=100000000,y1=0,y2=-1; for(int i=0;i<num-1;i++) { if(prime2[i+1]-prime2[i]<x2-x1) { x1=prime2[i]; x2=prime2[i+1]; } if(prime2[i+1]-prime2[i]>y2-y1) { y1=prime2[i]; y2=prime2[i+1]; } } printf("%d,%d are closest, %d,%d are most distant.\n",x1,x2,y1,y2); } } return 0; }