• CodeForces


    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.

    Example

    Input
    2 4 2
    Output
    3
    Input
    6 13 1
    Output
    4
    Input
    1 4 3
    Output
    -1

    题意:找到最小的l,使[a,b]中所有长度为l的区间都能找到k个素数,如果找不到,输出"-1"
    分析: 答案是线性相关的,所以我们可以二分答案,然后再通过尺取法判断该长度是否满足条件
    代码如下:
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    const int MAXN=2e6+10;
    int a,b,k;
    int flag[MAXN];
    int flag2;
    int que[MAXN];
    bool check(int len)
    {
     //   puts("c");
        int num,len1,l,r,now;
        int flag3;
        flag3=1;
        l=0,r=0;
        now=0;
        while(1)
        {
             while(r-l<len)
             {
              que[r++]=a+r-1;
              if(flag[a+r-1]){
              now++;
              }
              if(a+r-1==b)break;
             }
            if(r-l==len)
            {
              if(now<k)
              flag3=0;
            //  cout<<"    "<<que[l]<<endl;
              if(flag[que[l]]){
              now--;
              }
              l++;
            }
            if(flag3==0||a+r-1==b)
            break;
        }
        if(flag3==1)return true;
        return false;
    }
    int main()
    {
        flag[1]=0;
        flag[2]=1;
        for(int i=3;i<=1000100;i++)
        {
           flag2=1;
           for(int j=2;j*j<=i;j++)
           {
               if(i%j==0)
               {
                   flag2=0;
                   break;
               }
           }
           flag[i]=flag2;
        }
        while(scanf("%d%d%d",&a,&b,&k)!=EOF)
        {
            int l,r,mid;
            l=0; r=b-a+1;
            while(l+1<r)
            {
              mid=(l+r)/2;
              if(check(mid))
              r=mid;
              else
              l=mid;
            }
           if(check(r))printf("%d
    ",r);
           else puts("-1");
        }
        return 0;
    }


  • 相关阅读:
    WPF、UWP以及其他类型项目的csproj文件的迁移(SDK-Style)
    文书生成笔录预设保存按钮Mq中间转传服务
    卷宗添加争议焦点数据制造脚本(卷宗部分)
    案件信息同步之后,文书系统案件名称显示不正确问题
    DISTINCT----mysql移除返回的重复值
    Nginx系列教程(6)Nginx location 匹配规则详细解说
    转载 chrony 详解
    dmesg 时间转换
    axios---get和post用法详解
    通过递归来封装sessionStorage---set/get/delete
  • 原文地址:https://www.cnblogs.com/a249189046/p/8094309.html
Copyright © 2020-2023  润新知