Prime Distance
作者:互联网
Prime Distance
http://poj.org/problem?id=2689
#include<iostream>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string.h>
using namespace std;
typedef long long ll;
const int N = 1e5+7;
vector<ll>prime(N);
bool a[N];
int cnt = 0;
void init() {
for(int i = 2 ; i < N ; i++) {
if(!a[i])
prime[cnt++] = i;
for(int j = 0 ; j < cnt && prime[j]*i < N ; j++)
a[prime[j]*i] = 1;
}
return ;
}
bool mp[N*10];
int main() {
init();
ll l,r;
while(cin >> l >> r) {
memset(mp,0,sizeof(mp));
for(int i = 0 ; i < cnt && 1ll*prime[i]*prime[i] <= 1ll*r ; i++)
for(int j = max(2ll,l/prime[i]) ; j*prime[i] <= r ; j++)
mp[prime[i]*j-l]=1;
ll pre1 = -1, len1 = 0, t1;
ll pre2 = -1, len2 = 0x3f3f3f3f3f, t2;
for(int i = max(2ll,l) ; i <= r ; i++) {
if(mp[i-l])
continue;
if(pre1!=-1)
if(i-pre1 > len1) {
t1 = i;
len1 = i-pre1;
}
pre1 = i;
if(pre2!=-1)
if(i-pre2 < len2) {
t2 = i;
len2 = i-pre2;
}
pre2 = i;
}
if(!len1) {
puts("There are no adjacent primes.");
continue;
}
printf("%lld,%lld are closest, %lld,%lld are most distant.\n",1ll*t2-len2,t2,1ll*t1-len1,t1);
}
}
标签:Prime,Distance,cnt,int,prime,len1,include,lld 来源: https://www.cnblogs.com/zhaohanzheng/p/16069874.html