/*
This program solve Problem C codeforces round #147 div 2
Date: 26/10/2012
-------------------------------
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std;
const int maxn = 10000010;
//------------------------------------
bool isprime[maxn];
int nPrime[maxn];
int a,b,k;
//------------------------------------
/*Sieve for all prime from 1 to 1000000
nPrime[i] indicates the number of prime numbers from 1 to i
isprime[i] indicates whether i is prime or not
*/
void sieve() {
int i,j;
i=2;
memset(isprime,true,sizeof(isprime));
isprime[1]=false;
nPrime[1]=0;
nPrime[2]=1;
while (i<=int(sqrt(maxn)))
{
if (isprime[i])
{
j=i+i;
while (j<=maxn)
{
isprime[j]=false;
j+=i;
}
i++;
}
else i++;
}
for (int i=3; i<=maxn; i++)
if (isprime[i]) nPrime[i]=nPrime[i-1]+1;
else nPrime[i]=nPrime[i-1];
}
//------------------------------------
/* Check if the value of l satisfies the problem condition */
bool ok(int l) {
int eps;
for (int i=a; i<=b-l+1; i++)
{
eps=0;
if (isprime[i]) eps++;
if (nPrime[i+l-1]-nPrime[i]+eps<k) return(false);
}
return(true);
}
//------------------------------------
/* Search for the smallest value satisfies from 1 to b-a+1*/
int search(int a, int b) {
int l,r,m,eps;
l=a; r=b;
do
{
m=(l+r)/2; // Take middle value
if (ok(m)) r=m; // If ok, search for the lower bound
else l=m; // If not, it must be somewhere in the upper bound
} while (r-l>1);
if (ok(l)) return(l); // Lower bound first...
else if (ok(r)) return(r); // Then upper bound
return(-1); // Cant find anything
}
//------------------------------------
int main() {
sieve();
cin >> a >> b >> k;
cout << search(1,b-a+1);
return 0;
}
//------------------------------------
Introduction
This is my blog of programming, I take notes and leave codes of computer science problems I solved here. Be my guest to comment :)
Friday, October 26, 2012
Codeforces round #147 div 2 - Problem C
Labels:
Mathematic
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment