0% found this document useful (0 votes)
33 views26 pages

NT 4

The document provides an overview of number theory, focusing on concepts such as divisibility, the Euclidean algorithm, prime numbers, and the fundamental theorem of arithmetic. It includes definitions, theorems, and algorithms related to these topics, along with examples and implementations in code. The document emphasizes the importance of understanding these concepts intuitively rather than just theoretically.

Uploaded by

srag20062017
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views26 pages

NT 4

The document provides an overview of number theory, focusing on concepts such as divisibility, the Euclidean algorithm, prime numbers, and the fundamental theorem of arithmetic. It includes definitions, theorems, and algorithms related to these topics, along with examples and implementations in code. The document emphasizes the importance of understanding these concepts intuitively rather than just theoretically.

Uploaded by

srag20062017
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Number Theory

Nicola

Number theory is a branch of discrete mathematics, which I

consider its heart. Number theory is mainly meant with integer

numbers, sometimes rationals. It studies aspects like divisibility,

modular arithmetic, prime numbers, and related stu which we will

soon get to know.

1 Divisibily
We are not really interested in the theoretical aspect, so it'll be

enough to cover the basics.

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

b = ak . We denote this as a | b, which is read  a divides b.

1
Example 1
Using the denition of divisibility a | b ⇐⇒ b = ak show the

following basic properties of divisibility:

1) if a is any integer, show that

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

3) if a|b and a|c show that

a|b+c

a|b·c
a | sb + tc
4) if a|b and b|c show that a|c

5) if a|b and b|a show that |a| = |b|

2
It doesn't really matter proving these formally( though it is

simply done by converting divisibility notion into an equation and

back to another divisibiliy relation); what really matters is to un-

derstand and internalize these idea intuitively. for the rst two 1

is a divisor of all integers, and 0 is a multiple of them all.

then, if b is a multiple of a, then surely bc will be a multiple.

formally

a | b ⇐⇒ b = ak =⇒ bc = a(ck) ⇐⇒ a | bc
next, similar to linear equations, you can multiply or divide (only

integers) on both sides of a divisibility relation

a | b ⇐⇒ b = ak ⇐⇒ bc = (ac)k ⇐⇒ ac | bc
in group (3), the three relations basically tell you that you can sum

and multiply multiples of a as you wish, they will remain multiples.


in (4) a multiple of a multiple of a is also a multiple of a. and in (5),

if a is a multiple of b, and b is a multiple of a, then they are equal

(the absolute value sign is only used for purposes of generalization,

since we also have negative integers)

a | b =⇒ b = ak, b | a =⇒ a = bm = (ak)m = a(km) =⇒ km = 1


now since k and m are integers, then either both are 1 or both are
−1, that means either a = b or a = −b, which can be compressed

into |a| = |b|.

3
There are some other properties but these cover most of it. Now

let's move to a new concept.

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

gcd(a, b) = gcd(a − kb, b)

For convenience let's dene d = gcd(a, b) and g = gcd(a−kb, b).

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

g | a − kb, g | b =⇒ g | (a − kb) + k(b) =⇒ g | a

and similarlyg ≤ d. combining this with the result that d ≤ g , we


conclude that d = g , as desired.

Notice that k can be positive or negative, it doesn't matter, so

we can add or subtract any multiple of b to a, yet conserving the

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).

This is so useful, why? because we know something about r , it

is between 0 and b − 1. (less than b) If we neglect the signs of a, b,

since the gcd is not aected by them. And say that without loss of

generality (WLOG), a ≥ b, then we got a smaller pair of integers

(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

r is always guaranteed to be non-negative, by the theorem 1 itself).

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

of any pair of integers, by transitioning to smaller and smaller pairs,

down to zero, while conserving the value of gcd(x, y) on every tran-


sition.

Implementation 1 Euclidean algorithm


https://ideone.com/30Jloq

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-

self as shown above, to do some modications (maybe to track

the pairs or whatever). However, if calculating the GCD is all

you need, then simply use the built-in function.

Example 2
employing the euclidean algorithm, calculate the GCD of the

following pairs: (4, 8) (35, 86) (18, 12)

The algorithm would run in this order

(4, 8) =⇒ (8, 4) =⇒ (4, 0)

(35, 86) =⇒ (86, 35) =⇒ (35, 16) =⇒ (16, 3) =⇒ (3, 1) =⇒ (1, 0)


(18, 12) =⇒ (12, 6) =⇒ (6, 0)
So as you can see sometimes the algorithm takes many steps

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).

To show this, look at the greater integer, a, if b ≤ a2 then surely


r < b ≤ a2 . Otherwise if b > a2 , then r = a − b < a2 . So both ways,
a will decrease at least by half, which can happen at most log2 a
times. same for b.

7
3 Fundamental theorem of arithmetic
Denition 3
A positive integer p is called a prime number if and only if its

only divisors are 1 and p itself.

Based on the denition, here's a code that checks whether the



input integer is prime or not in O( n).

Implementation 2 Primality check


https://ideone.com/wmMm4T

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

where pi represents a prime number, and ei ≥ 0 is an exponent.


This representation is called the prime factorization of a.

I won't bother you with a proof to this, I suppose you are al-

ready convinced this is true. You may google it if interested.

Note
Perfect squares (integers of the form x2 , that have an integer

square root), have even exponents for all primes. Similarly,

prefect cubes have exponents divisible by 3 for all primes.

Now let's discuss the sieve algorithm, that generates the list of

all primes up to n. And also provides a primality check array as a

by-product. It is faster than O(n log n)

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

their prime factorizations, then

a | b ⇐⇒ ni ≤ mi

for all i. In other words, for b to be divisible by a, it must have


a greater power for each prime in the factorization.

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

are equal, and an integer has only one unique factorization.


Y Y Y Y
pm
i
i
= b = ak = pni i plii = pni i+li =⇒ mi = ni + li

and since li ≥ 0, ni ≤ mi. The proof of the other side of the


then

implication is similar, dene k to be the integer whose factorization


Q li
pi has li = mi − ni which is non-negative since ni ≤ mi (the
prime factorization is valid), and then we have ak = b meaning

that a | b as desired.

11
As a summary, if a | b, then b is splitting his prime exponents

between a and k , so each of them will have smaller exponents. And

if a has smaller exponents, then you can add some exponents so as


to complement them to those of b. Adding exponents means that

a is being multiplied by something to yield b, therefore it divides


b.

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

pn1 1+1 − 1 p2n2+1 − 1 pn3 3+1 − 1 Y pni+1 − 1


s(a) = ( )( )( )... = ( i )
p1 − 1 p2 − 1 p3 − 1 pi − 1

if we would like to construct a divisor of a, then for each prime


it should have exponents ≤ ni. So if we consider a prime pi ,

then we can have its exponent in the divisor to be any of these

0, 1, 2, 3...ni − 1, ni, so we have ni + 1 options for the prime pi. And

by the product law we get the formula.

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 .

nally, let's discuss the sum. Let p1


a, be any prime divisor of
n1 m1
imagine that a = p1 ×b. Any divisor of a is of the form d = p1 ×e

where m1 ≤ n1 and e | b (e is a divisor of b), and any integer of


m1
the form p1 × e where m1 ≤ n1 and e | b is also a divisor of a.

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).

Now with the same argument, we can take a prime divisor

of b to calculate s(b) (which is basically any prime divisor of a


except p1). After we do this to all primes of a, we get s(a) =
n +1 n +1 n +1 n +1
p1 1 −1 p2 2 −1 p3 3 −1 Q pi i −1
( p1 −1 )( p2 −1 )( p3 −1 )... = ( )
pi −1 .

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

their prime factorizations, then

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

it divides both of a, b. And to be the greatest, it must have the

greatest exponent possible, which is min(a, b). Same logic with

lcm(a, b). And now let's take one nal result.

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

(well, there is, but it requires calling a library... it is generally not

used), so we prefer dening the following line of code to calculate

lcm(a, b).

Implementation 4 LCM line


#define lcm(a,b) a/__gcd(a,b)*b

Notice that we rst divide by the GCD, since otherwise multi-

plying a × b rst is more likely to cause overow for large numbers.

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

that have the same remainder when divided by 5.

Generally, if we write integers row by row in a n-column table,

then each coulmn will have integers with the same remainder when

divided by n, each column corresponding to one of the n dierent

remainders (residues). Each column is called a residue class or an

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).

This is basically the same thing, n | a−b means that a and

b have the same remainder, so they will be in the same column

(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).

The next example will show the manipulation of modular arith-

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

It is given that n|a−c and n | b − d. We can get

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..

(45 + 90 = 135 degrees is the same as 405 + 810 = 1215 ≡ 135


degrees)

This fact is so useful in CP, since most of the combinatorics

problems ask for output modulo something (usually 109 + 7 or

998244353). So whenever you see this, never take the modulo after

calculating the answer, since it will denitely cause an overow as

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

the nal answer at all.

Notice that you can't take the mod for example, on exponents

(if b ≡ c (mod n) then ab ̸≡ ac (mod n)), the exponent just de-

notes how many times multiplication is performed, and the mod

is only taken on integers that are subject to arithmetic operators

(addition,multiplication,subtraction and division). However, you

can mod thea in this example.


https://codeforces.com/problemset/problem/1920/C

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

normal arithmetic, so it is known as the "thing" that when mul-

tiplied by a gives the whole one. The same denition is used in

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

we know that gcd(ab, n) = 1 since ab ≡ 1 (mod n) means that


ab = kn + 1, and gcd(kn + 1, n) = 1 by theorem 1. and if ab has
nothing in common with n, then so does a alone.

So, when you see a fraction under mod, do not imagine a piece

of pie divided into equal parts, rather imagine the denition itself,

as the inverse of the same integer changes with dierent modulos.

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-

tiplied by 2. So you can work with modular arithmetic completely

as in normal arithmetic including division, do not worry at all.

Example 4
Find the inverse of the following integers under mod.

2 (mod 3), 4 (mod 7), 5 (mod 8), 4 (mod 9)

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

p, consider the set S = {1, 2, 3, ..., p − 2, p − 1} and the set


T = {1a, 2a, 3a, ...(p − 2)a, (p − 1)a} (In other words, T =
{xa : x ∈ S}). Then, taking all the elements modulo p, we
get:

S=T

We know that S contains all the possible residues mod p, we


just want to show T does. Assume there are two dierent elements

in T that are equal mod p, let them ia, ja, then

ia ≡ ja (mod p) ⇐⇒ a(i − j) ≡ 0 (mod p) ⇐⇒ p | a(i − j)

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

of p in this range is 0, therefore i = j which is a contradiction. This

means that all of the ia are dierent mod p, and since there are

p − 1 distinct residues in T , that are all between 1 and p − 1, then


the elements in the set are 1, 2, 3, ...p−2, p−1, which is equal to S .

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)

The theorem says that we need a not divisible by p, so when

we invoke theorem 4, we don't have to worry. The two theorems

have the same conditions on p and a.

Anyways, since in theorem 4, S=T mod p, then the product

of there elements is equal also under mod. (we used theorem 4 to

establish the congruence relation (S ≡ T (mod p)), and we chose

to take the product and not the sum for example, because we want

to get ap−1 somehow (the power comes from multiplication))

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.

This is extremely useful, we will be using FLT to calculate in-

verses when the modulo is a prime. Just imagine the theorem this

way

ap−1 = a × ap−2 ≡ 1 (mod p)


This is crystal clear, it is literally saying that ap−2 is the inverse of

a mod p.

The only thing remaining is how to calculate the power e-

ciently in C++, since as I've already mentioned, most combina-

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

it 25 times and square it the same way.

Just as a note, if the exponent is odd, we can't half it and square

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.

Implementation 5 Fast power


https://ideone.com/v3AsVp

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;
}

This code works in O(log y), since on each call it is halved.

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

You might also like