How many primes is in the interval ?
Prime number is an integer that is larger than 1 and is only divisible by 1 and itself.
Check if an integer is a prime: Only need to check if is divisible by from to .
const int maxn = (int) 1e7;
bool sieve[maxn+1];
void eratoSieve()
{
memset(sieve,true,sizeof(sieve));
sieve[0] = false;
sieve[1] = false;
for (int i=2; i*i<=maxn; i++)
if (sieve[i])
{
for (int j=i*2; j<=maxn; j+=i)
sieve[j] = false;
}
}
How to evaluate quickly?
If ,
If ,
$ 2^n = ( 2^{\frac{n}{2}} )^2 \times 2 $
ll modpow(ll x, ll n, ll m)
{
if (n == 0)
return 1 % m;
ll u = modpow(x, n / 2, m);
u = (u * u) % m;
if (n % 2 == 1)
u = (u * x) % m;
return u;
}
Given two integer point , how many integer point is on the segement ? ()
The equation of the line segment is and .
We can find , then the equation will become and .
With each from to , we have an integer point on the segment.
How to evaluate quickly?
If is prime, .
If is not divisible by
Often associated with Dynamic programming.
Some tutorials about Probability and Combinatorics
Tutorial 1
Tutorial 2
Thanks Bud and Yi Rong for taking the notes!