NT 4
NT 4
Nicola
1 Divisibily
We are not really interested in the theoretical aspect, so it'll be
Denition 1
We say b ∈ Z is divisible by a ∈ Z (or b is a multiple of a or a
is a divisor of b) if and only if there exists a k ∈ Z such that
1
Example 1
Using the denition of divisibility a | b ⇐⇒ b = ak show the
1|a
a|0
2) if a|b and c is any integer show that
a | bc
ac | bc
also show that ac | bc =⇒ a | b
a|b+c
a|b·c
a | sb + tc
4) if a|b and b|c show that a|c
2
It doesn't really matter proving these formally( though it is
derstand and internalize these idea intuitively. for the rst two 1
formally
a | b ⇐⇒ b = ak =⇒ bc = a(ck) ⇐⇒ a | bc
next, similar to linear equations, you can multiply or divide (only
a | b ⇐⇒ b = ak ⇐⇒ bc = (ac)k ⇐⇒ ac | bc
in group (3), the three relations basically tell you that you can sum
3
There are some other properties but these cover most of it. Now
Theorem 1
[Euclid's division lemma] for any integer a and positive integer
b, there exist unique integers q and r such that 0 ≤ r < b and
a = qb + r. (q is called quotient, r the remainder of division)
Denition 2
The greatest common divisor (GCD) of two integers a, b, de-
noted as gcd(a, b), is the greatest integer d such that d | a and
d | b.
Theorem 2
for any integers a, b, k
d | a, d | b =⇒ d | (a) − k(b)
so now d divides both a−kb and b, therefore it is a common divisor
of them. but it can't be greater than g since it is their greatest
common divisor. We write d ≤ g .
4
Now using the same argument
value of GCD.
https://codeforces.com/problemset/problem/1325/A
https://codeforces.com/problemset/problem/1458/A
2 Euclidean Algorithm
Remember theorem 1, we can rewrite this as r = a − qb. Then
using theorem 1, we have gcd(a, b) = gcd(a − qb, b) = gcd(r, b).
since the gcd is not aected by them. And say that without loss of
(r, b) that has the same GCD. (a+b >> b+r, gcd(a, b) = gcd(b, r).
If we redo this, we'll reach some point where one of the two param-
eters of the GCD is zero, and stop (we can't go below zero, since
5
So when we reach the point with gcd(x, 0), we know that gcd(x, 0) =
x, implying by the chain of equalities that gcd(a, b) = x.
long story short, we have an algorithm that can calculate the GCD
ll GCD(ll a,ll b)
{
if(a<b){swap(a,b);}
if(b==0){return a;}
return GCD(b,a%b);
}
Note
There is actually a built-in function
__gcd(a,b)
6
but sometimes you might have to implement the function your-
Example 2
employing the euclidean algorithm, calculate the GCD of the
even for relatively small numbers. Its use lies in that it's super fast
for huge numbers, it is even faster than O(log n), could be seen as
O(1).
7
3 Fundamental theorem of arithmetic
Denition 3
A positive integer p is called a prime number if and only if its
bool is_prime(ll x)
{
if(x<2){return false;}
for(ll i=2;i*i<=x;i++){
if(x%i==0){
return false;
}
}
return true;
}
8
Theorem 3
[Fundamental theorem of arithmetic] Every positive integer a
has a unique representation of the form
Y
pei i
I won't bother you with a proof to this, I suppose you are al-
Note
Perfect squares (integers of the form x2 , that have an integer
Now let's discuss the sieve algorithm, that generates the list of
9
Implementation 3 Sieve
https://ideone.com/gHTQ0w
vector<ll>primes;
vector<bool>is_prime;
void sieve(int n)
{
primes.clear();
is_prime.clear();
is_prime.assign(n+1,true);
is_prime[0]=is_prime[1]=false;
for(ll i=2;i<=n;i++){
if(is_prime[i]){
primes.push_back(i);
for(ll j=i*i;j<=n;j+=i){
is_prime[j]=false;
}
}
}
}
https://codeforces.com/problemset/problem/1062/B
10
Theorem 4
pni i , b = pm
Q Q i
Let a= i be two positive integers written in
a | b ⇐⇒ ni ≤ mi
Note
This is the very most important theorem in NT. You have to
internalize it very well.
plii .
Q
We can write b = ka, write the factorization of k= We
know that the factorizations of b and ak are the same, since they
that a | b as desired.
11
As a summary, if a | b, then b is splitting his prime exponents
Theorem 5
Q ni
Let a = pi be an integer in its prime factorization. Let
d(a), p(a) and s(a) denote the number, product, and sum of
all divisors of a respectively. Then:
Y
d(a) = (n1 + 1)(n2 + 1)(n3 + 1)... = ni + 1
d(a)
p(a) = a 2
12
now for the product, note that divisors come in pairs (if d is a
a
divisor of a, the
d is also a divisor), so we can pair the divisors so
that the product of each pair is a. Therefore since there are d(n)
d(a)
a
divisors, then we have
2 pairs, and s(a) = a 2 .
This means that the sum of all divisors of a is equal to the sum
n1
of divisors of p1 times the sum of divisors of b. Which is equal to
n +1
2 3 n1 p1 1 −1
(1+p1 +p1 +p1 +...p1 ) times s(b). This is equal to ( p1−1 )×s(b).
13
Denition 4
The least common multiple (LCM) of two integers a, b, denoted
as lcm(a, b), is the least integer l such that a | l and b | l.
Theorem 6
pni i , b pm
Q Q i
Let a= = i be two positive integers written in
min(ni ,mi )
Y
gcd(a, b) = pi
max(ni ,mi )
Y
lcm(a, b) = pi
14
Briey speaking, by theorem 3, for every prime pi, gcd(a, b)
must have an exponent smaller than ni and smaller than mi since
Theorem 7
Let a, b be two positive integers, then
a × b = gcd(a, b) × lcm(a, b)
We will show that the two sides have the same factorization.
Take any prime pi. on LHS, its exponent is ni + mi, on the RHS
it is min(ni, mi) + max(ni, mi), which are equal (rst+second el-
ement=smaller+greater element), hence implying equality.
15
There is no built-in function for calculating LCM of two integers
lcm(a, b).
16
4 Modular arithmetic
17
Look at these, these are the numbers from −10 to 19, arranged
in a table of ve columns, each column here consists of the integers
then each coulmn will have integers with the same remainder when
equivalence class.
Denition 5
Let a, b ∈ Z, n ∈ Z+. We say that a is congruent to b modulo
n if and only if n | a − b, and we denote this as a ≡ b (mod n).
(residue class), or using the new term -congruent- they will be con-
18
gruent.
The same idea is found in the clock, assume it's 8 o'clock, after
24 hours or any multiple of 24 hours it will be also 8 o'clock (we
work mod 24), and the angles system where adding any multiple
of360 degrees brings the angle back to the initial direction (works
mod 360).
metic.
Example 3
Leta, b, c, d ∈ Z, n ∈ Z+, with a ≡ c (mod n) and b ≡ d
(mod n), show that
a + b (mod n) = c + d mod n
a − b (mod n) = c − d mod n
a × b (mod n) = c × d mod n
n | (a − c) + (b − d) = (a + b) − (c + d) ⇐⇒ a + b ≡ c + d mod n
n | (a − c) − (b − d) = (a − b) − (c − d) ⇐⇒ a − b ≡ c − d mod n
n | (a − c)b + (b − d)c = ab − cd ⇐⇒ ab ≡ cd mod n
19
This means basically, that you can perform arithmetic opera-
tions as you wish, while replacing any integer with any other that
belongs to the same residue class, without altering the nal output.
In other words, any integer represents its class, and all integers be-
longing to the same class are treated as the same integers under
the mod. Just like with angles and clock, we can add, subtract..
998244353). So whenever you see this, never take the modulo after
the answer is too large. Instead, you should take the modulo at
every step, while not worrying about it at all, as it will not aect
Notice that you can't take the mod for example, on exponents
20
If you wonder, how do we perform division under mod, then let
me tell you that you're right, division and fractions do not make
any sense under mod, but there will be a denition for this.
1
Imagine
a , this is called the multiplicative inverse of a in the
modular arithmetic.
Denition 6
The inverse of a residue a ∈ Z modulo n ∈ Z+ is the residue b
that satises a × b ≡ 1 (mod n).
Note
An integer a has an inverse b modulo n if and only if
gcd(a, n) = 1
So, when you see a fraction under mod, do not imagine a piece
of pie divided into equal parts, rather imagine the denition itself,
21
Anyways, all of this relates, even in normal arithmetic we follow
1
the same dinition, I mean
2 is not the portion you get when you
slice a pie in the middle, it is the number that yields 1 when mul-
Example 4
Find the inverse of the following integers under mod.
2, 2, 5, 7 respectively.
Now let's move to our last two theorems for this chapter
Theorem 8
Let p a any integer that is not divisible by
be any prime and
S=T
22
Since it is given that a is not divisible by p, then (i−j) must. But i
and j are between 0 and p−1 so −p < i−j < p. The only multiple
means that all of the ia are dierent mod p, and since there are
We will use this in the last theorem next, fermat's little theorem.
Theorem 9
[FLT] Let p be any prime number, and a an integer that is not
(
divisible by p. Then, a p − 1) ≡ 1 (mod p)
to take the product and not the sum for example, because we want
p−1
Y p−1
Y
i≡ ia (mod p) ⇐⇒ (p − 1)! ≡ (p − 1)!ap−1 (mod p)
i=1 i=1
23
⇐⇒ (p − 1)!(ap−1 − 1) ≡ 0 (mod p)
now since (p − 1)! has nothing in common with p, we can take
it out; what remains is ap−1 − 1 ≡ 0 (mod p) ⇐⇒ ap−1 ≡ 1
(mod p), completing the proof.
verses when the modulo is a prime. Just imagine the theorem this
way
a mod p.
torics problems ask for answers mod 109 + 7 and 998244353, which
are quite big as exponents.
The idea is that, for calculating x100 for example, we don't have
to multiply x a hundred times, we can multiply it 50 times, then
100
multiply it by itself (x = (x50)2 = x50 × x50). You know what
else, we don't even have to multiply x fty times, we can multiply
24
back, but there are a million ways to bypass this problem, for ex-
ample imagine x2y+1 = x2y ×x, just calculate for the even exponent
2y and multiply by x.
ll fast_pow(ll x,ll y)
{
if(y==0){return 1;}
ll res=fast_pow(x,y/2);
res=(res*res)%mod;
if(y&1){
res=(res*x)%mod;
}
return res;
}
25
Practice problems
https://codeforces.com/problemset/problem/1370/A
https://codeforces.com/problemset/problem/1845/A
https://codeforces.com/problemset/problem/1535/B
https://codeforces.com/problemset/problem/678/C
https://codeforces.com/problemset/problem/1872/C
https://codeforces.com/problemset/problem/1396/A
https://codeforces.com/problemset/problem/1787/B
https://codeforces.com/problemset/problem/1454/D
https://codeforces.com/problemset/problem/1902/C
https://codeforces.com/problemset/problem/1370/C
https://codeforces.com/problemset/problem/1487/D
26