CodeForces - 237C Primes on Interval(二分+尺取)

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\n",r);
       else puts("-1");
    }
    return 0;
}
				
时间: 2024-10-28 23:49:08

CodeForces - 237C Primes on Interval(二分+尺取)的相关文章

CodeForces 237C Primes on Interval

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).

Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has got a robot which is situated on an infinite Cartesian plane, i

Primes on Interval(二分 + 素数打表)

Primes on Interval Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 237C Appoint description:  System Crawler  (2016-04-26) Description You've decided to carry out a survey in the theory

hdu 6231 -- K-th Number(二分+尺取)

题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an array B by a parameter K as following rules: Initially, the array B is empty. Consider each interval in array A. If the length of this interval is le

【二分+尺取】HDU 6119 小小粉丝度度熊

http://acm.hdu.edu.cn/showproblem.php?pid=6119 [思路] 首先通过处理交叉的可以处理成不交叉的 然后二分查找答案 如何判断一个长度是否可行? 双指针O(n)扫一次就可以,要处理区间和问题,所以先求前缀和 时间复杂度O(nlogn) [AC] 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #inclu

UVALive - 2678 二分/尺取

题意:求最小的长度L满足该长度上的元素和大于等于S 最近dp做多了总有一种能用dp解决一切的错觉 二分长度解决 #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<string> #include<vector> #include&

POJ3061 Subsequence 尺取or二分

Description A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive elements o

Codeforces Round #116 (Div. 2, ACM-ICPC Rules) E. Cubes (尺取)

题目链接:http://codeforces.com/problemset/problem/180/E 给你n个数,每个数代表一种颜色,给你1到m的m种颜色.最多可以删k个数,问你最长连续相同颜色的序列的长度是多少. 将相同颜色的下标存到对应颜色的容器中,比如ans[a[i]].push_back(i)就是将下标为i颜色为a[i]的数存到ans[a[i]]容器中. 对于每种颜色序列,尺取一下 在差距小于k的情况下取能取到的最大长度. 1 //#pragma comment(linker, "/S

POJ-3061 Subsequence 二分或尺取

题面 题意:给你一个长度为n(n<100000)的数组,让你找到一个最短的连续子序列,使得子序列的和>=m  (m<1e9) 题解: 1 显然我们我们可以二分答案,然后利用前缀和判断是否可行,这样是O(nlgn)的   注意没有答案 ans输出0 1 #include<cstdio> 2 #include<cstdlib> 3 #include<iostream> 4 #include<cstring> 5 using namespace