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 ≤ 106; a ≤ b).

Output

In a single line print a single integer — the required minimum l. If there's no solution, print -1.

Examples

Input

2 4 2

Output

3

Input

6 13 1

Output

4

Input

1 4 3

Output

-1

 給出a,b區間,找出一個數 L 滿足 1<=L<=b-a+1, 使得在 [ a , b-L+1 ] 區間內的任意數x在區間

[ x,x+L-1 ]至少有k個數爲素數

題目有點亂,二分枚舉 L 即可

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define rush() int T;cin>>T;while(T--)
#define go(a) while(cin>>a)
#define ms(a,b) memset(a,b,sizeof a)
#define E 1e-8
#define debug(a) cout<<"*"<<a<<"*"<<endl
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> Pair;
const int inf=0x3f3f3f3f;
const int N=1e6+5;

    int n,m,t;
    int i,j,k;
    int num[N];
bool judge(int L)
{
    for(int i=n;i<=m-L+1;i++){
        if(num[i+L-1]-num[i-1]<k) return false;
    }
    return true;
}
int32_t main()
{
    IOS;
    int a,b;
    while(cin>>a>>b>>k){ n=a;m=b;
        int cnt=0; num[1]=0;
        //統計到 i 位置一共有多少素數,由  num 數組記錄
        for(i=2;i<N;i++){
            bool ok=1;
            for(j=2;j*j<=i;j++){
                if(i%j==0){ok=0;break;}
            }
            if(ok) cnt++;
            num[i]=cnt;
        }

        int l=1,r=N,ans=-1;
        while(r>=l){
            int mid=(l+r)/2;
            if(judge(mid)){
                if(mid>=1&&mid<=b-a+1) ans=mid;
                r=mid-1;
            }
            else l=mid+1;
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章