http://codeforces.com/contest/237/problem/C
二分l,枚举x,赛后才做出,读题能力有待加强
代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stack>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
int biao[1000005];
int a,b,k;
int check(int l)
{
int sign=0;
for(int i=a;i<=a+l-1;i++)
if(!biao[i])
sign++;
//printf("~%d %d\n",l,sign);
if(sign<k)
return 0;
for(int i=a+1;i<=b-l+1;i++)
{
if(!biao[i-1])
sign--;
if(!biao[i+l-1])
sign++;
//printf("!%d\n",sign);
if(sign<k)
return 0;
}
return 1;
}
int main()
{
int len=0;
scanf("%d%d%d",&a,&b,&k);
int i,j;
memset(biao,0,sizeof(biao));
biao[1]=1;
for(i=2;i<=1000000;i++)
{
if(!biao[i])
{
for( j=i+i;j<=1000000;j+=i)
{
biao[j]=1;
}
}
}
int left=1;
int right=b-a+1;
while(left<right)
{
int mid=(left+right)/2;
// printf("%d %d\n",mid,check(mid));
if(check(mid))
right=mid;
else
left=mid+1;
}
if(check(left))
printf("%d\n",left);
else
printf("-1");
}