Codeforces 237C

时间:2023-12-17 22:15:14

题目:

Description

You've decided to carry out a survey in the theory of prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors.

Consider positive integers aa + 1, ..., b (a ≤ b). You want to find the minimum integer l (1 ≤ l ≤ b - a + 1) such that for any integerx (a ≤ x ≤ b - l + 1) among l integers xx + 1, ..., x + l - 1 there are at least k prime numbers.

Find and print the required minimum l. If no value l meets the described limitations, print -1.

Input

A single line contains three space-separated integers a, b, k (1 ≤ a, b, k ≤ 106a ≤ b).

Output

In a single line print a single integer — the required minimum l. If there's no solution, print -1.

Sample Input

Input
2 4 2
Output
3
Input
6 13 1
Output
4
Input
1 4 3
Output
-1

代码:

#include <cstdio>
#include <cstring>
const int maxn = 1000010;
int sum[maxn],a,b,k;
bool pri[maxn];
void init(){
for(int i = 2;i < maxn;i++){
sum[i] = sum[i-1];
if(pri[i]) continue;
sum[i]++;
for(int j = i+i;j < maxn;j += i)
pri[j] = 1;
}
}

bool check(int mid){
for(int i = a;i <= b-mid+1;i++){
if(sum[i+mid-1]-sum[i-1] < k)
return 0;
//如果是这种情况说明结果是一定不会满足条件的,我们应该重新确定一个更大的min值来进行判断
}
return 1;
}

int main(){
init();
scanf("%d%d%d",&a,&b,&k);
if(sum[b]-sum[a-1] < k){
printf("-1\n");
return 0;

//当所给的区间范围内都没有满足条件的素数个数的时候,结果可以直接的返回
//但是如果满足了这个条件的情况下,l是一定会有解的
int l = 1,r = b-a+1,ans;
while(l <= r){
int mid = (l+r)>>1;
if(check(mid)) ans = mid,r = mid-1;
else l = mid+1;
}
printf("%d\n",ans);
}