CF--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 aa + 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 lintegers xx + 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即一個區間,求一個最小的len,使得對所有的x,x,...,x+len-1這個區間內至少k個素數。

其中要滿足

a<=x<=b

a<=x-len+1<=b

二分len,利用前綴和優化。

#define ll long long
const ll mod=1e9+7;
ll n,m;
const ll N = 2000000+999;
ll prime[N] = {0},num_prime = 0;  //prime存放着小於N的素數
int isNotPrime[N] = {1, 1};        // isNotPrime[i]如果i不是素數,則爲1
int Prime()
{
    for(ll  i = 2 ; i < N ; i ++)
    {
        if(! isNotPrime[i])
            prime[num_prime ++]=i;
        //無論是否爲素數都會下來
        for(ll  j = 0 ; j < num_prime && i * prime[j] <  N ; j ++)
        {
            isNotPrime[i * prime[j]] = 1;
            if( !(i % prime[j] ) )  //遇到i最小的素數因子
                //關鍵處1
                break;
        }
    }
    return 0;
}

預處理

Prime();
    for(int i=1; i<=1000006; i++)
    {
        if(!isNotPrime[i])
        {
            s[i]=s[i-1]+1;
        }
        else
        {
            s[i]=s[i-1];
        }
    }

 二分

二分寫法1  這個版本是求出符合答案最小的那個!

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod=1e9+7;
ll n,m;
const ll N = 2000000+999;
ll prime[N] = {0},num_prime = 0;  //prime存放着小於N的素數
int isNotPrime[N] = {1, 1};        // isNotPrime[i]如果i不是素數,則爲1
int Prime()
{
    for(ll  i = 2 ; i < N ; i ++)
    {
        if(! isNotPrime[i])
            prime[num_prime ++]=i;
        //無論是否爲素數都會下來
        for(ll  j = 0 ; j < num_prime && i * prime[j] <  N ; j ++)
        {
            isNotPrime[i * prime[j]] = 1;
            if( !(i % prime[j] ) )  //遇到i最小的素數因子
                //關鍵處1
                break;
        }
    }
    return 0;
}
int s[N];
int a,b,k;
bool check(int len)
{
    for(int i=a; i<=b-len+1; i++)
    {
        if(s[i+len-1]-s[i-1]<k)
            return false;
    }
    return true;
}
int main()
{
    Prime();
    for(int i=1; i<=1000006; i++)
    {
        if(!isNotPrime[i])
        {
            s[i]=s[i-1]+1;
        }
        else
        {
            s[i]=s[i-1];
        }
    }
    scanf("%d %d %d",&a,&b,&k);
    int l=1;
    int r=b-a+1;
    int ans=999999;
    while(l<r)
    {
        int mid=(l+r)/2;//長度
        if(check(mid))
        {
            r=mid;
        }
        else
        {
            l=mid+1;
        }
    }
    printf("%d\n",check(l)?l:-1);
}

要是求符合題意最大的  (>=x最大的一個數):

while(l<r)
    {
        int mid=(l+r+1)/2;//長度
        if(a[mid]<=x)
        {
            l=mid;
        }
        else
        {
            r=mid-1;
        }
    }
    return a[l];

二分寫法2

這個方法要確保題目一定有答案的

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod=1e9+7;
ll n,m;
const ll N = 2000000+999;
ll prime[N] = {0},num_prime = 0;  //prime存放着小於N的素數
int isNotPrime[N] = {1, 1};        // isNotPrime[i]如果i不是素數,則爲1
int Prime()
{
    for(ll  i = 2 ; i < N ; i ++)
    {
        if(! isNotPrime[i])
            prime[num_prime ++]=i;
        //無論是否爲素數都會下來
        for(ll  j = 0 ; j < num_prime && i * prime[j] <  N ; j ++)
        {
            isNotPrime[i * prime[j]] = 1;
            if( !(i % prime[j] ) )  //遇到i最小的素數因子
                //關鍵處1
                break;
        }
    }
    return 0;
}
int s[N];
int a,b,k;
bool check(int len)
{
    for(int i=a; i<=b-len+1; i++)
    {
        if(s[i+len-1]-s[i-1]<k)
            return false;
    }
    return true;
}
int main()
{
    Prime();
    for(int i=1; i<=1000006; i++)
    {
        if(!isNotPrime[i])
        {
            s[i]=s[i-1]+1;
        }
        else
        {
            s[i]=s[i-1];
        }
    }
    scanf("%d %d %d",&a,&b,&k);
    int l=1;
    int r=b-a+1;
    int ans=9999999;
    while(l<=r)
    {
        int mid=(l+r)>>1;//長度
        if(check(mid))
        {
            r=mid-1;
            ans=min(ans,mid);
        }
        else
        {
            l=mid+1;
        }
    }
    printf("%d\n",ans==9999999?-1:ans);
}

 

 

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