nyoj646

来源:互联网 发布:网络教育法学类本科 编辑:程序博客网 时间:2024/06/14 08:08

题意: 求最小的l。
思路:二分。
这题在nyoj上给的时限为3秒,在codefoeces上为1秒。

AC代码:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=1000010;int vis[maxn];int s[maxn];void init(){    memset(vis,0,sizeof(vis));    memset(s,0,sizeof(s));    s[1]=0;    for(int i=2; i<maxn; i++){        if(!vis[i]){            s[i]=s[i-1]+1;            for(int j=i+i; j<maxn; j+=i) vis[j]=1;        }else {            s[i]=s[i-1];        }    }}int a,b,k;int check(int x){    for(int i=a; i<=b-x+1; i++){        if(s[i+x-1]-s[i-1]<k) return 0;    }    return 1;}int main(){  //  freopen("51.txt","r",stdin);    init();    while(scanf("%d%d%d",&a,&b,&k)!=EOF){        if(s[b]-s[a-1]<k){            printf("-1\n"); continue;        }        int l=1,r=b-a+1;        while(l<=r){            int mid=(l+r)/2;            if(check(mid)){                r=mid-1;            }else {                l=mid+1;            }        }        printf("%d\n",l);    }    return 0;}
0 0
原创粉丝点击