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 lintegers 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
Given a, b is an interval, find a minimum len, so that for all x, x,...,x+len-1, there are at least k prime numbers in this interval.
Among them to be satisfied
a<=x<=b
a<=x-len+1<=b
Dichotomous len, using prefix and optimization.
#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 holds primes less than N int isNotPrime[N] = {1, 1}; // isNotPrime[i] If I is not a prime, it is 1 int Prime() { for(ll i = 2 ; i < N ; i ++) { if(! isNotPrime[i]) prime[num_prime ++]=i; //Whether it's a prime or not, it's going to come down. for(ll j = 0 ; j < num_prime && i * prime[j] < N ; j ++) { isNotPrime[i * prime[j]] = 1; if( !(i % prime[j] ) ) //Encountering the Minimum Prime Factor of i //Key point 1 break; } } return 0; }
Preprocessing
Prime(); for(int i=1; i<=1000006; i++) { if(!isNotPrime[i]) { s[i]=s[i-1]+1; } else { s[i]=s[i-1]; } }
Two points
Dichotomy 1. This version is to find the one with the smallest answer.
#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 holds primes less than N int isNotPrime[N] = {1, 1}; // isNotPrime[i] If I is not a prime, it is 1 int Prime() { for(ll i = 2 ; i < N ; i ++) { if(! isNotPrime[i]) prime[num_prime ++]=i; //Whether it's a prime or not, it's going to come down. for(ll j = 0 ; j < num_prime && i * prime[j] < N ; j ++) { isNotPrime[i * prime[j]] = 1; if( !(i % prime[j] ) ) //Encountering the Minimum Prime Factor of i //Key point 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;//length if(check(mid)) { r=mid; } else { l=mid+1; } } printf("%d\n",check(l)?l:-1); }
If the maximum number (>= x) is required:
while(l<r) { int mid=(l+r+1)/2;//length if(a[mid]<=x) { l=mid; } else { r=mid-1; } } return a[l];
Dichotomy 2
This method ensures that there must be an answer to the question.
#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 holds primes less than N int isNotPrime[N] = {1, 1}; // isNotPrime[i] If I is not a prime, it is 1 int Prime() { for(ll i = 2 ; i < N ; i ++) { if(! isNotPrime[i]) prime[num_prime ++]=i; //Whether it's a prime or not, it's going to come down. for(ll j = 0 ; j < num_prime && i * prime[j] < N ; j ++) { isNotPrime[i * prime[j]] = 1; if( !(i % prime[j] ) ) //Encountering the Minimum Prime Factor of i //Key point 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;//length if(check(mid)) { r=mid-1; ans=min(ans,mid); } else { l=mid+1; } } printf("%d\n",ans==9999999?-1:ans); }