Given the interval [a, B], calculate the minimum value of l so that any segment of [a, B] with an arbitrary length of l contains at least k Prime
Binary l
[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <cstdlib>
# Include <cmath>
# Include <algorithm>
# Include <functional>
# Include <iostream>
Using namespace std;
# Define MAXN (1000000 + 10)
Int a [MAXN], tot = 0, x, y, k;
Bool B [MAXN] = {0 };
Void work ()
{
B [1] = 1;
For (int I = 2; I <= y; I ++)
{
If (! B [I])
{
Tot ++;
A [tot] = I;
}
For (int j = 1; j <= tot; j ++)
{
If (a [j] * I> y) break;
B [a [j] * I] = 1;
If (! I % a [j]) break;
}
}
}
Bool is_ OK (int l)
{
Int tot = 0;
For (int I = x; I <= x L-1; I ++)
If (! B [I]) tot ++;
If (tot <k) return false;
For (int j = x + l; j <= y; j ++)
{
Tot = tot + (! B [j])-(! B [j-l]);
If (tot <k) return false;
}
Return true;
}
Int main ()
{
Scanf ("% d", & x, & y, & k );
Work ();
Int l = k, r = y-x + 1;
If (l> r |! Is_ OK (r ))
{
Printf ("-1 \ n ");
Return 0;
}
For (int I = 1; I <= 60; I ++)
{
If (l = r) break;
Int m = (l + r)> 1;
// If (r-l = 1) m ++;
If (is_ OK (m) r = m;
Else l = m + 1;
}
Printf ("% d \ n", l );
// While (1 );
Return 0;
}