POJ.2689-Prime Distance(二次筛法+思维)
Language:
Prime Distance
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 32911 | Accepted: 8353 |
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 17 14 17
Sample Output
2,3 are closest, 7,11 are most distant. There are no adjacent primes.
Source
思路:二次筛法,首先筛一遍\(2\leq i \leq sqrt(2^{31})-1\)内的素数,然后利用数组将素数存起来,然后由于l~r区间很小,我们只需要将我们已知的素数通过埃氏筛法再筛一遍就可以得到了,需要注意的是利用了离散化的操作,即在第二次筛法时,每次用G[i-l]来标记i;
再写一点,这个题还是有些许的考思维,因为拿到题很容易无从下手,我当时想了一个假算法,在思考时没有想到用筛法直接映射标记\([l,r]\)区间,以后做题要向多方向思考一下,感觉这个题纯粹是用了埃氏筛法的原理,这在以后的做题中也很有帮助;
code:
/*************************************************************************
> File Name: problemC.cpp
# Author: Badwoman
# mail: 1194446133@qq.com
> Created Time: 2020年12月11日 星期五 18时43分16秒
************************************************************************/
#include<set>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define int long long
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define bep(i,a,b) for(int i=a;i>=b;--i)
#define lowbit(x) (x&(-x))
using namespace std;
const int N = 1e5+10,inf = 0x3f3f3f3f;
int n,arr[N],l,r;
bool st[N];
int prime[N],tot;
void getprime(){
rep(i,2,N){
if(!st[i]){
prime[++tot] = i;
if(i>10000)continue;
for(int j=i*i;j<=N;j+=i){
st[j] = true;
}
}
}
}
bool G[N*10+10];
void solve(){
getprime();
while(~scanf("%lld%lld",&l,&r)){
memset(G,0,sizeof G);
rep(i,1,tot){
if(prime[i]>r)break;
//printf("%lld\n",i);
int f = l/prime[i];
for(int j=f*prime[i];j<=r;j+=prime[i]){
//printf("%lld %lld\n",prime[i],j);
if(j == prime[i])continue;
if(j-l<0)continue;
G[j-l] = true;
}
}
if(l == 1)G[0] = true;
int now[N],k=0, ansl = -1,ansr = -1,x = -1,y = -1,dis1 = -1,dis2 = inf;
rep(i,0,r-l){
if(G[i]==false){
now[++k] = i;
}
}
rep(i,2,k){
if(now[i]-now[i-1]>dis1)ansl = now[i-1]+l,ansr = now[i]+l,dis1 = now[i]-now[i-1];
if(now[i]-now[i-1]<dis2)x = now[i-1]+l,y = now[i]+l,dis2 = now[i]-now[i-1];
}
if(dis1 == -1)puts("There are no adjacent primes.");
else printf("%lld,%lld are closest, %lld,%lld are most distant.\n",x,y,ansl,ansr);
}
}
main(){
solve();
}

浙公网安备 33010602011771号