『NYIST』第八届河南省ACM竞赛训练赛[正式赛一] E题

E - E
Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

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 ≤ 106a ≤ 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
 
    
 
    
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
#define maxn 1000005
int p[maxn]= {0}; //前i位有sum[i]个素数
int d[maxn];//是素数标记为1,否则标记为0
int a,b,k;
void shaifa()
{
    int i,j;
    d[0]=d[1]=1;
    for(i=2; i<maxn; i++)
    {
        p[i]=p[i-1];
        if(!d[i])
        {
            p[i]++;
            for(j=2*i; j<maxn; j+=i)//或者:for(j=1;i*j<maxn;j++){d[i*j]=1;}
            {
                d[j]=1;
            }
        }
    }
}
bool check(int x)//传入二分查找的中间值
{
    for(int i=a; i<=b-x+1; i++)
        if(p[i+x-1]-p[i-1]<k)//此区间的素数小于k
            return 0;
    return 1;//满足条件返回1
}
int main()
{
    shaifa();
    while(~scanf("%d%d%d",&a,&b,&k))
    {
        if(p[b]-p[a-1]<k)//取极限,如果不满足条件
        {
            printf("-1\n");
            return 0;
        }
        int left=1;//左边界
        int right=b-a+1;//右边界
        int num;
        while(left<=right)//二分查找
        {
            int mid=(left+right)/2;//中间值
            if(check(mid))//满足条件
            {
                num=mid;
                right=mid-1;//缩小范围继续查找
            }
            else
                left=mid+1;//改变左边界
        }
        printf("%d\n",num);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值