#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1001000;
#define inf (1<<29)
// p[i] is i-th prime's position
bool pp[maxn];
int p[maxn] , cnt = 0;
int ss[maxn] , tt[maxn];
void init() {
cnt = 0;
pp[0] = pp[1] = 1;
tt[0] = tt[1] = -1;
for(int i=2;i<maxn;i++) {
if(!pp[i]) {
p[cnt++] = i;
for(int j=i+i;j<maxn;j+=i) {
pp[j] = true;
}
}
tt[i] = cnt - 1;
}
for(int i=0;i<maxn;i++) {
if(!pp[i]) ss[i] = tt[i];
else ss[i] = tt[i] + 1;
}
}
int main() {
init();
int a , b , k;
while(~scanf("%d%d%d" , &a,&b,&k)) {
int s = ss[a] , t = tt[b];
int num = t - s + 1;
//printf("debug:\n");
//printf("s is %d , t is %d\n" , s , t);
//printf("first pri is %d , last prime is %d\n" , p[s] , p[t]);
if(num < k) {
printf("-1\n");
continue;
}
int ans = 0;
int tmp;
tmp = b - p[t-k+1] + 1;
if(tmp > ans) ans = tmp;
tmp = p[s+k-1] - a + 1;
if(tmp > ans) ans = tmp;
for(int i=s;i+k<=t;i++) {
tmp = p[i+k] - p[i];
if(tmp > ans) ans = tmp;
}
printf("%d\n" , ans);
}
return 0;
}
Codeforces 237C
最新推荐文章于 2017-04-05 12:03:31 发布