codeforces147 div2 C题

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值