码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 237C

时间:2014-07-29 10:25:58      阅读:395      评论:0      收藏:0      [点我收藏+]

标签:des   os   io   for   代码   div   ar   line   

题目:

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);
}

Codeforces 237C,布布扣,bubuko.com

Codeforces 237C

标签:des   os   io   for   代码   div   ar   line   

原文地址:http://www.cnblogs.com/tianxia2s/p/3873629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!