您的位置:首页 > 其它

Codeforces 237C (Primes on Interval)二分

2016-10-17 19:36 417 查看
Primes on Interval

Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u

Submit

Status

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 a, a + 1, …, b (a ≤ b). You want to find the minimum integer l (1 ≤ l ≤ b - a + 1) such that for any integer x (a ≤ x ≤ b - l + 1) among l integers x, x + 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 ≤ 106; a ≤ 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

问你在【a, b】区间里面取最小长度 l 的区间里面最少有 k 个素数

二分区间找最小的就好了

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define M 1000010
#define INF 0x3f3f3f3f

bool prime[M];
int dp[M], a, b, k;

void isprime()
{
prime[1] = 1;
for(int i=2; i*i<M; i++)
{
if(!prime[i])
{
for(int j=i*i; j<M; j+=i)
{
prime[j] = 1;
}
}
}
}
void init()
{
dp[1] = 0;
dp[0] = 0;
for(int i=1; i<M; i++)
{
if(!prime[i])
{
dp[i] = dp[i-1] + 1;
}
else
{
dp[i] = dp[i-1];
}
}
}
bool judge(int mid)
{
for(int i=a; i<=b; i++)
{
if(i + mid - 1 > b) break;
if(dp[i+mid-1] - dp[i-1] < k)
{
return false;
}
}
return true;
}
int main()
{
isprime();
init();
while(scanf("%d%d%d", &a, &b, &k) != EOF)
{
int ans = -1, left = k, right = b - a + 1;
int mid = 0;
while(left <= right)
{
mid = (right + left) >> 1;
if(judge(mid))
{
ans = mid;
right = mid - 1;
}
else
{
left = mid + 1;
}
}
printf("%d\n", ans);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: