0% found this document useful (0 votes)
4 views167 pages

Number Theory

The document covers various mathematical concepts relevant to computer science, including number theory, combinatorics, and game theory. It discusses the importance of finding patterns and formulas in problem-solving, arithmetic and geometric progressions, logarithms, and modular arithmetic. Additionally, it introduces algorithms for calculating greatest common divisors and emphasizes the significance of mathematical intuition in programming.

Uploaded by

chamala ruthwik
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)
4 views167 pages

Number Theory

The document covers various mathematical concepts relevant to computer science, including number theory, combinatorics, and game theory. It discusses the importance of finding patterns and formulas in problem-solving, arithmetic and geometric progressions, logarithms, and modular arithmetic. Additionally, it introduces algorithms for calculating greatest common divisors and emphasizes the significance of mathematical intuition in programming.

Uploaded by

chamala ruthwik
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/ 167

Mathematics

Tómas Ken Magnússon


Bjarki Ágúst Guðmundsson
Árangursrík forritun og lausn verkefna
School of Computer Science
Reykjavík University
Today we’re going to cover

Basics

Number Theory

Combinatorics

Game Theory

2
Basics
Computer Science ⊂ Mathematics

4
• Usually at least one problem that involves solving mathematically.
• Problems often require mathematical analysis to be solved efficiently.
• Using a bit of math before coding can also shorten and simplify code.

5
Finding patterns and formulas

• Some problems have solutions that form a pattern.

6
Finding patterns and formulas

• Some problems have solutions that form a pattern.


• By finding the pattern, we solve the problem.
• Could be classified as mathematical ad-hoc problem.
• Requires mathematical intuition.

6
Finding patterns and formulas

• Some problems have solutions that form a pattern.


• By finding the pattern, we solve the problem.
• Could be classified as mathematical ad-hoc problem.
• Requires mathematical intuition.
• Useful tricks:
• Solve some small instances by hand.
• See if the solutions form a pattern.

6
Finding patterns and formulas

• Some problems have solutions that form a pattern.


• By finding the pattern, we solve the problem.
• Could be classified as mathematical ad-hoc problem.
• Requires mathematical intuition.
• Useful tricks:
• Solve some small instances by hand.
• See if the solutions form a pattern.
• Does the pattern involve some overlapping subproblem?

6
Finding patterns and formulas

• Some problems have solutions that form a pattern.


• By finding the pattern, we solve the problem.
• Could be classified as mathematical ad-hoc problem.
• Requires mathematical intuition.
• Useful tricks:
• Solve some small instances by hand.
• See if the solutions form a pattern.
• Does the pattern involve some overlapping subproblem?
We might need to use DP.

6
Finding patterns and formulas

• Some problems have solutions that form a pattern.


• By finding the pattern, we solve the problem.
• Could be classified as mathematical ad-hoc problem.
• Requires mathematical intuition.
• Useful tricks:
• Solve some small instances by hand.
• See if the solutions form a pattern.
• Does the pattern involve some overlapping subproblem?
We might need to use DP.
• Knowing reoccurring identities and sequences can be helpful.

6
Arithmetic progression

• Often we see a pattern like

2, 5, 8, 11, 14, 17, 20, . . .

7
Arithmetic progression

• Often we see a pattern like

2, 5, 8, 11, 14, 17, 20, . . .

• This is called a arithmetic progression.

an = an−1 + c

7
Arithmetic progression

• Depending on the situation we may want to get the n-th element

an = a1 + (n − 1)c

• Or the sum over a finite portion of the progression

n(a1 + an )
Sn =
2

8
Arithmetic progression

• Depending on the situation we may want to get the n-th element

an = a1 + (n − 1)c

• Or the sum over a finite portion of the progression

n(a1 + an )
Sn =
2

• Remember this one?


n(n + 1)
1 + 2 + 3 + 4 + 5 + ... + n =
2

8
Geometric progression

• Other types of pattern we often see are geometric progressions

1, 2, 4, 8, 16, 32, 64, 128, . . .

9
Geometric progression

• Other types of pattern we often see are geometric progressions

1, 2, 4, 8, 16, 32, 64, 128, . . .

• More generally

a, ar , ar 2 , ar 3 , ar 4 , ar 5 , ar 6 , . . .

an = ar n−1

9
Geometric progression

• Sum over a finite portion


n
X a(1 − r n )
ar i =
(1 − r )
i=0

10
Geometric progression

• Sum over a finite portion


n
X a(1 − r n )
ar i =
(1 − r )
i=0

• Or from the m-th element to the n-th


n
X a(r m − r n+1 )
ar i =
(1 − r )
i=m

10
Little bit about logarithm

• Sometimes doing computation in logarithm can be an efficient


alternative.

11
Little bit about logarithm

• Sometimes doing computation in logarithm can be an efficient


alternative.
• In both C++(<cmath>) and Java(java.lang.Math) we have the
natural logarithm
double log(double x);

11
Little bit about logarithm

• Sometimes doing computation in logarithm can be an efficient


alternative.
• In both C++(<cmath>) and Java(java.lang.Math) we have the
natural logarithm
double log(double x);
and logarithm in base 10
double log10(double x);

11
Little bit about logarithm

• Sometimes doing computation in logarithm can be an efficient


alternative.
• In both C++(<cmath>) and Java(java.lang.Math) we have the
natural logarithm
double log(double x);
and logarithm in base 10
double log10(double x);
• And also the exponential
double exp(double x);

11
Example

• For example, what is the first power of 17 that has k digits in base
b?

12
Example

• For example, what is the first power of 17 that has k digits in base
b?
• Naive solution: Iterate over powers of 17 and count the number of
digits.

12
Example

• For example, what is the first power of 17 that has k digits in base
b?
• Naive solution: Iterate over powers of 17 and count the number of
digits.
• But the powers of 17 grow exponentially!

1716 > 264

• What if k = 500 (∼ 1.7 · 10615 ), or something larger?

12
Example

• For example, what is the first power of 17 that has k digits in base
b?
• Naive solution: Iterate over powers of 17 and count the number of
digits.
• But the powers of 17 grow exponentially!

1716 > 264

• What if k = 500 (∼ 1.7 · 10615 ), or something larger?


• Impossible to work with the numbers in a normal fashion.
• Why not log?

12
Example

• Remember, we can calculate the length of a number n in base b


with blogb (n)c + 1.

13
Example

• Remember, we can calculate the length of a number n in base b


with blogb (n)c + 1.
• But how do we do this with only ln or log10 ?

13
Example

• Remember, we can calculate the length of a number n in base b


with blogb (n)c + 1.
• But how do we do this with only ln or log10 ?
• Change base!
logd (a) ln(a)
logb (a) = =
logd (b) ln(b)
• Now we can at least count the length without converting bases

13
Example

• We still have to iterate over the powers of 17, but we can do that in
log
ln(17x−1 · 17) = ln(17x−1 ) + ln(17)

14
Example

• We still have to iterate over the powers of 17, but we can do that in
log
ln(17x−1 · 17) = ln(17x−1 ) + ln(17)

• More generally
logb (xy ) = logb (x) + logb (y )
• For division
x
logb ( ) = logb (x) − logb (y )
y

14
Example

• We can simplify this even more.


• The solution to our problem is in mathematical terms, finding the x
for
logb (17x ) = k − 1

15
Example

• We can simplify this even more.


• The solution to our problem is in mathematical terms, finding the x
for
logb (17x ) = k − 1

• One more handy identity

logb (ac ) = c · logb (a)

15
Example

• We can simplify this even more.


• The solution to our problem is in mathematical terms, finding the x
for
logb (17x ) = k − 1

• One more handy identity

logb (ac ) = c · logb (a)

• Using this identity and the ones we’ve covered, we get


 
ln(10)
x = (k − 1) ·
ln(17)

15
Base conversion

• Speaking of bases.

16
Base conversion

• Speaking of bases.
• What if we actually need to use base conversion?

16
Base conversion

• Speaking of bases.
• What if we actually need to use base conversion?
• Simple algorithm
vector<int> toBase(int base, int val) {
vector<int> res;
while(val) {
res.push_back(val % base);
val /= base;
}
return val;
• Starts from the 0-th digit, and calculates the multiple of each power.

16
Working with doubles

• Comparing doubles, sounds like a bad idea.

17
Working with doubles

• Comparing doubles, sounds like a bad idea.


• What else can we do if we are working with real numbers?

17
Working with doubles

• Comparing doubles, sounds like a bad idea.


• What else can we do if we are working with real numbers?
• We compare them to a certain degree of precision.

17
Working with doubles

• Comparing doubles, sounds like a bad idea.


• What else can we do if we are working with real numbers?
• We compare them to a certain degree of precision.
• Two numbers are deemed equal of their difference is less than some
small epsilon.

const double EPS = 1e-9;

if (abs(a - b) < EPS) {


...
}

17
Working with doubles

• Less than operator:


if (a < b - EPS) {
...
}
• Less than or equal:
if (a < b + EPS) {
...
}
• The rest of the operators follow.

18
Working with doubles

• This allows us to use comparison based algorithms.

19
Working with doubles

• This allows us to use comparison based algorithms.


• For example std::set<double>.
struct cmp {
bool operator(){double a, double b}{
return a < b - EPS;
}
};

set<double, cmp> doubleSet();


• Other STL containers can be used in similar fashion.

19
Number Theory
Modular arithmetic

• Problem statements often end with the sentence

“... and output the answer modulo n.”

21
Modular arithmetic

• Problem statements often end with the sentence

“... and output the answer modulo n.”

• This implies that we can do all the computation with integers


modulo n.

21
Modular arithmetic

• Problem statements often end with the sentence

“... and output the answer modulo n.”

• This implies that we can do all the computation with integers


modulo n.
• The integers, modulo some n form a structure called a ring.

21
Modular arithmetic

• Problem statements often end with the sentence

“... and output the answer modulo n.”

• This implies that we can do all the computation with integers


modulo n.
• The integers, modulo some n form a structure called a ring.
• Special rules apply, also loads of interesting properties.

21
Modular arithmetic

Some of the allowed operations:

• Addition and subtraction modulo n

(a mod n) + (b mod n) = (a + b mod n)


(a mod n) − (b mod n) = (a − b mod n)

• Multiplication

(a mod n)(b mod n) = (ab mod n)

• Exponentiation
(a mod n)b = (ab mod n)
• Note: We are only working with integers.

22
Modular arithmetic

• What about division?

23
Modular arithmetic

• What about division? NO!

23
Modular arithmetic

• What about division? NO!


• We could end up with a fraction!
• Division with k equals multiplication with the multiplicative inverse
of k.

23
Modular arithmetic

• What about division? NO!


• We could end up with a fraction!
• Division with k equals multiplication with the multiplicative inverse
of k.
• The multiplicative inverse of an integer a, is the element a−1 such
that
a · a−1 = 1 (mod n)

23
Modular arithmetic

• What about logarithm?

24
Modular arithmetic

• What about logarithm? YES!


• But difficult.

24
Modular arithmetic

• What about logarithm? YES!


• But difficult.
• Basis for some cryptography such as elliptic curve, Diffie-Hellmann.
• Google “Discrete Logarithm” if you want to know more.

24
Modular arithmetic

• Prime number is a positive integer greater than 1 that has no


positive divisor other than 1 and itself.
• Greatest Common Divisor of two integers a and b is the largest
number that divides both a and b.
• Least Common Multiple of two integers a and b is the smallest
integer that both a and b divide.
• Prime factor of an positive integer is a prime number that divides it.
• Prime factorization is the decomposition of an integer into its prime
factors. By the fundamental theorem of arithmetics, every integer
greater than 1 has a unique prime factorization.

25
Extended Euclidean algorithm

• The Euclidean algorithm is a recursive algorithm that computes the


GCD of two numbers.
int gcd(int a, int b){
return b == 0 ? a : gcd(b, a % b);
}
• Runs in O(log2 N).

26
Extended Euclidean algorithm

• The Euclidean algorithm is a recursive algorithm that computes the


GCD of two numbers.
int gcd(int a, int b){
return b == 0 ? a : gcd(b, a % b);
}
• Runs in O(log2 N).
• Notice that this can also compute LCM
a·b
lcm(a, b) =
gcd(a, b)

• See Wikipedia to see how it works and for proofs.

26
Extended Euclidean algorithm

• Reversing the steps of the Euclidean algorithm we get the Bézout’s


identity
gcd(a, b) = ax + by
which simply states that there always exist x and y such that the
equation above holds.

27
Extended Euclidean algorithm

• Reversing the steps of the Euclidean algorithm we get the Bézout’s


identity
gcd(a, b) = ax + by
which simply states that there always exist x and y such that the
equation above holds.
• The extended Euclidean algorithm computes the GCD and the
coefficients x and y .
• Each iteration it add up how much of b we subtracted from a and
vice versa.

27
Extended Euclidean algorithm

int egcd(int a, int b, int& x, int& y) {


if (b == 0) {
x = 1;
y = 0;
return a;
} else {
int d = egcd(b, a % b, x, y);
x -= a / b * y;
swap(x, y);
return d;
}
}

28
Applications

• Essential step in the RSA algorithm.


• Essential step in many factorization algorithms.
• Can be generalized to other algebraic structures.
• Fundamental tool for proofs in number theory.
• Many other algorithms for GCD

29
Primality testing

• How do we determine if a number n is a prime?

30
Primality testing

• How do we determine if a number n is a prime?


• Naive method: Iterate over all 1 < i < n and check it i | n.
• O(N)

30
Primality testing

• How do we determine if a number n is a prime?


• Naive method: Iterate over all 1 < i < n and check it i | n.
• O(N)

• Better: If n is not a prime, it has a divisor ≤ n.

• Iterate up to n instead.

• O( N)

30
Primality testing

• How do we determine if a number n is a prime?


• Naive method: Iterate over all 1 < i < n and check it i | n.
• O(N)

• Better: If n is not a prime, it has a divisor ≤ n.

• Iterate up to n instead.

• O( N)

• Even better: If n is not a prime, it has a prime divisor ≤ n

• Iterate over the prime numbers up to n.

• There are ∼ N/ ln(N) primes less N, therefore O( N/ log N).

30
Primality testing

• Trial division is a deterministic primality test.


• Many algorithms that are probabilistic or randomized.
• Fermat test; uses Fermat’s little theorem.
• Probabilistic algorithms that can only prove that a number is
composite such as Miller-Rabin.
• AKS primality test, the one that proved that primality testing is in P.

31
Prime sieves

• If we want to generate primes, using a primality test is very


inefficient.

32
Prime sieves

• If we want to generate primes, using a primality test is very


inefficient.
• Instead, our preferred method of prime generation is the sieve of
Eratosthenes.

32
Prime sieves

• If we want to generate primes, using a primality test is very


inefficient.
• Instead, our preferred method of prime generation is the sieve of
Eratosthenes.

• For all numbers from 2 to n:

32
Prime sieves

• If we want to generate primes, using a primality test is very


inefficient.
• Instead, our preferred method of prime generation is the sieve of
Eratosthenes.

• For all numbers from 2 to n:
• If the number is not marked, iterate over every multiple of the
number up to n and mark them.

32
Prime sieves

• If we want to generate primes, using a primality test is very


inefficient.
• Instead, our preferred method of prime generation is the sieve of
Eratosthenes.

• For all numbers from 2 to n:
• If the number is not marked, iterate over every multiple of the
number up to n and mark them.
• The unmarked numbers are those that are not a multiple of any
smaller number.

• O( N log log N)

32
Prime sieves

• If we want to generate primes, using a primality test is very


inefficient.
• Instead, our preferred method of prime generation is the sieve of
Eratosthenes.

• For all numbers from 2 to n:
• If the number is not marked, iterate over every multiple of the
number up to n and mark them.
• The unmarked numbers are those that are not a multiple of any
smaller number.

• O( N log log N)

2 3 4 5 Primes:
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves

• If we want to generate primes, using a primality test is very


inefficient.
• Instead, our preferred method of prime generation is the sieve of
Eratosthenes.

• For all numbers from 2 to n:
• If the number is not marked, iterate over every multiple of the
number up to n and mark them.
• The unmarked numbers are those that are not a multiple of any
smaller number.

• O( N log log N)

2 3 4 5 Primes:
6 7 8 9 10 2,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves

• If we want to generate primes, using a primality test is very


inefficient.
• Instead, our preferred method of prime generation is the sieve of
Eratosthenes.

• For all numbers from 2 to n:
• If the number is not marked, iterate over every multiple of the
number up to n and mark them.
• The unmarked numbers are those that are not a multiple of any
smaller number.

• O( N log log N)

2 3 4 5 Primes:
6 7 8 9 10 2,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves

• If we want to generate primes, using a primality test is very


inefficient.
• Instead, our preferred method of prime generation is the sieve of
Eratosthenes.

• For all numbers from 2 to n:
• If the number is not marked, iterate over every multiple of the
number up to n and mark them.
• The unmarked numbers are those that are not a multiple of any
smaller number.

• O( N log log N)

2 3 4 5 Primes:
6 7 8 9 10 2, 3,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves

• If we want to generate primes, using a primality test is very


inefficient.
• Instead, our preferred method of prime generation is the sieve of
Eratosthenes.

• For all numbers from 2 to n:
• If the number is not marked, iterate over every multiple of the
number up to n and mark them.
• The unmarked numbers are those that are not a multiple of any
smaller number.

• O( N log log N)

2 3 4 5 Primes:
6 7 8 9 10 2, 3,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves

• If we want to generate primes, using a primality test is very


inefficient.
• Instead, our preferred method of prime generation is the sieve of
Eratosthenes.

• For all numbers from 2 to n:
• If the number is not marked, iterate over every multiple of the
number up to n and mark them.
• The unmarked numbers are those that are not a multiple of any
smaller number.

• O( N log log N)

2 3 4 5 Primes:
6 7 8 9 10 2, 3, 5,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves

• If we want to generate primes, using a primality test is very


inefficient.
• Instead, our preferred method of prime generation is the sieve of
Eratosthenes.

• For all numbers from 2 to n:
• If the number is not marked, iterate over every multiple of the
number up to n and mark them.
• The unmarked numbers are those that are not a multiple of any
smaller number.

• O( N log log N)

2 3 4 5 Primes:
6 7 8 9 10 2, 3, 5,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves

• If we want to generate primes, using a primality test is very


inefficient.
• Instead, our preferred method of prime generation is the sieve of
Eratosthenes.

• For all numbers from 2 to n:
• If the number is not marked, iterate over every multiple of the
number up to n and mark them.
• The unmarked numbers are those that are not a multiple of any
smaller number.

• O( N log log N)

2 3 4 5 Primes:
6 7 8 9 10 2, 3, 5,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves

• If we want to generate primes, using a primality test is very


inefficient.
• Instead, our preferred method of prime generation is the sieve of
Eratosthenes.

• For all numbers from 2 to n:
• If the number is not marked, iterate over every multiple of the
number up to n and mark them.
• The unmarked numbers are those that are not a multiple of any
smaller number.

• O( N log log N)

2 3 4 5 Primes:
6 7 8 9 10 2, 3, 5, 7,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves

• If we want to generate primes, using a primality test is very


inefficient.
• Instead, our preferred method of prime generation is the sieve of
Eratosthenes.

• For all numbers from 2 to n:
• If the number is not marked, iterate over every multiple of the
number up to n and mark them.
• The unmarked numbers are those that are not a multiple of any
smaller number.

• O( N log log N)

2 3 4 5 Primes:
6 7 8 9 10 2, 3, 5, 7, 11,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves

• If we want to generate primes, using a primality test is very


inefficient.
• Instead, our preferred method of prime generation is the sieve of
Eratosthenes.

• For all numbers from 2 to n:
• If the number is not marked, iterate over every multiple of the
number up to n and mark them.
• The unmarked numbers are those that are not a multiple of any
smaller number.

• O( N log log N)

2 3 4 5 Primes:
6 7 8 9 10 2, 3, 5, 7, 11, 13,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves

• If we want to generate primes, using a primality test is very


inefficient.
• Instead, our preferred method of prime generation is the sieve of
Eratosthenes.

• For all numbers from 2 to n:
• If the number is not marked, iterate over every multiple of the
number up to n and mark them.
• The unmarked numbers are those that are not a multiple of any
smaller number.

• O( N log log N)

2 3 4 5 Primes:
6 7 8 9 10 2, 3, 5, 7, 11, 13, 17,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves

• If we want to generate primes, using a primality test is very


inefficient.
• Instead, our preferred method of prime generation is the sieve of
Eratosthenes.

• For all numbers from 2 to n:
• If the number is not marked, iterate over every multiple of the
number up to n and mark them.
• The unmarked numbers are those that are not a multiple of any
smaller number.

• O( N log log N)

2 3 4 5 Primes:
6 7 8 9 10 2, 3, 5, 7, 11, 13, 17, 19,
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Prime sieves

• If we want to generate primes, using a primality test is very


inefficient.
• Instead, our preferred method of prime generation is the sieve of
Eratosthenes.

• For all numbers from 2 to n:
• If the number is not marked, iterate over every multiple of the
number up to n and mark them.
• The unmarked numbers are those that are not a multiple of any
smaller number.

• O( N log log N)

2 3 4 5 Primes:
6 7 8 9 10 2, 3, 5, 7, 11, 13, 17, 19, 23
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
32
Sieve of Eratosthenes

vector<int> eratosthenes(int n){


bool *isMarked = new bool[n+1];
memset(isMarked, 0, n+1);
vector<int> primes;
int i = 2;
for(; i*i <= n; ++i)
if (!isMarked[i]) {
primes.push_back(i);
for(int j = i; j <= n; j += i)
isMarked[j] = true;
}
for (; i <= n; i++)
if (!isMarked[i])
primes.push_back(i);
return primes;
}
33
Integer factorization

The fundamental theorem of arithmetic states that

• Every integer greater than 1 is a unique multiple of primes.

34
Integer factorization

The fundamental theorem of arithmetic states that

• Every integer greater than 1 is a unique multiple of primes.

n = p1e1 p2e2 p3e3 · · · pkek

34
Integer factorization

The fundamental theorem of arithmetic states that

• Every integer greater than 1 is a unique multiple of primes.

n = p1e1 p2e2 p3e3 · · · pkek

We can therefore store integers as lists of their prime powers.

34
Integer factorization

The fundamental theorem of arithmetic states that

• Every integer greater than 1 is a unique multiple of primes.

n = p1e1 p2e2 p3e3 · · · pkek

We can therefore store integers as lists of their prime powers.


To factor an integer n:


• Use the sieve of Eratosthenes to generate all the primes up n
• Iterate over all the primes generated and check if they divide n, and
determine the largest power that divides n.

34
map<int, int> factor(int N) {
vector<int> primes;
primes = eratosthenes(static_cast<int>(sqrt(N+1)));
map<int, int> factors;
for(int i = 0; i < primes.size(); ++i){
int prime = primes[i], power = 0;
while(N % prime == 0){
power++;
N /= prime;
}
if(power > 0){
factors[prime] = power;
}
}
if (N > 1) {
factors[N] = 1;
}
return factors;
}

35
Integer factorization

The prime factors can be quite useful.

36
Integer factorization

The prime factors can be quite useful.

• The number of divisors


k
Y
σ0 (n) = (e1 + 1)
i=1

36
Integer factorization

The prime factors can be quite useful.

• The number of divisors


k
Y
σ0 (n) = (e1 + 1)
i=1

• The sum of all divisors in x-th power


k
Y (p (ei +1)x − 1)
σm (n) =
(pi − 1)
i=1

36
Integer factorization

• The Euler’s totient function


k
Y
φ(n) = n · (1 − p)
i=1

37
Integer factorization

• The Euler’s totient function


k
Y
φ(n) = n · (1 − p)
i=1

• Euler’s theorem, if a and n are coprime

aφ(n) = 1 (mod n)

Fermat’s theorem is a special case when n is a prime.

37
Combinatorics
Combinatorics

Combinatorics is study of countable discrete structures.

39
Combinatorics

Combinatorics is study of countable discrete structures.

Generic enumeration problem: We are given an infinite sequence of sets


A1 , A2 , . . . An , . . . which contain objects satisfying a set of properties.
Determine
an := |An |
for general n.

39
Basic counting

• Factorial
n! = 1 · 2 · 3 · · · n
• Binomial coefficient  
n n!
=
k k!(n − k)!

40
Basic counting

• Factorial
n! = 1 · 2 · 3 · · · n
• Binomial coefficient  
n n!
=
k k!(n − k)!
Number of ways to choose k objects from a set of n objects,
ignoring order.

40
Basic counting

Properties

•    
n n
=
k n−k
•    
n n
= =1
0 n
•      
n n−1 n−1
= +
k k −1 k

41
Basic counting

Pascal triangle!

0
0
1 1
0 1
2 2 2
0 1 2
3 3 3 3
0 1 2 3
4 4 4 4 4
0 1 2 3 4
5 5 5 5 5 5
0 1 2 3 4 5
6 6 6 6 6 6 6
0 1 2 3 4 5 6
7 7 7 7 7 7 7 7
0 1 2 3 4 5 6 7
8 8 8 8 8 8 8 8 8
0 1 2 3 4 5 6 7 8
9 9 9 9 9 9 9 9 9 9
0 1 2 3 4 5 6 7 8 9
10 10 10 10 10 10 10 10 10 10 10
0 1 2 3 4 5 6 7 8 9 10

42
Example

How many rectangles can be formed on a m × n grid?

43
Example

How many rectangles can be formed on a m × n grid?

• A rectangle needs 4 edges, 2 vertical and 2 horizontal.

43
Example

How many rectangles can be formed on a m × n grid?

• A rectangle needs 4 edges, 2 vertical and 2 horizontal.

• 2 vertical

43
Example

How many rectangles can be formed on a m × n grid?

• A rectangle needs 4 edges, 2 vertical and 2 horizontal.

• 2 vertical
• 2 horizontal

43
Example

How many rectangles can be formed on a m × n grid?

• A rectangle needs 4 edges, 2 vertical and 2 horizontal.

• 2 vertical
• 2 horizontal

43
Example

How many rectangles can be formed on a m × n grid?


• A rectangle needs 4 edges, 2 vertical and 2 horizontal.

• 2 vertical
• 2 horizontal
• Number of ways we can choose 2 vertical lines
 
n
2

43
Example

How many rectangles can be formed on a m × n grid?


• A rectangle needs 4 edges, 2 vertical and 2 horizontal.

• 2 vertical
• 2 horizontal
• Number of ways we can choose 2 horizontal lines
 
m
2

43
Example

How many rectangles can be formed on a m × n grid?


• A rectangle needs 4 edges, 2 vertical and 2 horizontal.

• 2 vertical
• 2 horizontal
• Total number of ways we can form a rectangle
  
n m n!m!
=
2 2 (n − 2)!(m − 2)!2!2!
n(n − 1)m(m − 1)
=
4

43
Multinomial

What if we have many objects with the same value?

44
Multinomial

What if we have many objects with the same value?

• Number of permutations on n objects, where ni is the number of


objects with the i-th value.(Multinomial)
 
n n!
=
n1 , n2 , . . . , nk n1 !n2 ! · · · nk !

44
Multinomial

What if we have many objects with the same value?

• Number of permutations on n objects, where ni is the number of


objects with the i-th value.(Multinomial)
 
n n!
=
n1 , n2 , . . . , nk n1 !n2 ! · · · nk !

• Number of way to choose k objects from a set of n objects with,


where each value can be chosen more than once.
 
n+k −1 (n + k − 1)!
=
k k!(n − 1)!

44
Example

How many different ways can we divide k identical balls into n boxes?

45
Example

How many different ways can we divide k identical balls into n boxes?

• Same as number of nonnegative solutions to

x1 + x2 + . . . + xn = k

45
Example

How many different ways can we divide k identical balls into n boxes?

• Same as number of nonnegative solutions to

x1 + x2 + . . . + xn = k

• Let’s imagine we have a bit string consisting only of 1 of length


n+k −1
1| 1 1 1 1{z1 1 . . . 1}
n+k−1

45
Example

• Choose n − 1 bits to be swapped for 0

1...101...10...01...1

46
Example

• Choose n − 1 bits to be swapped for 0

|1 .{z
. . 1} 0 |1 .{z
. . 1} 0 . . . 0 |1 .{z
. . 1}
x1 x2 xn

• Then total number of 1 will be k, each 1 representing an each


element, and separated into n groups

46
Example

• Choose n − 1 bits to be swapped for 0

|1 .{z
. . 1} 0 |1 .{z
. . 1} 0 . . . 0 |1 .{z
. . 1}
x1 x2 xn

• Then total number of 1 will be k, each 1 representing an each


element, and separated into n groups
• Number of ways to choose the bits to swap
   
n+k −1 n+k −1
=
n−1 k

46
Example

How many different lattice paths are there from (0, 0) to (n, m)?
(n, m)

(0, 0)

47
Example

How many different lattice paths are there from (0, 0) to (n, m)?
(n, m)

• There is 1 path to (0, 0)

1
(0, 0)
1 1

47
Example

How many different lattice paths are there from (0, 0) to (n, m)?
(n, m)

• There is 1 path to (0, 0)


• There is 1 path to (1, 0) and (0, 1)

1
(0, 0)
1 1

47
Example

How many different lattice paths are there from (0, 0) to (n, m)?
(n, m)

• There is 1 path to (0, 0)


• There is 1 path to (1, 0) and (0, 1)
• Paths to (1, 1) is the sum of number
of paths to (0, 1) and (1, 0).
1 2
(0, 0)
1 1

47
Example

How many different lattice paths are there from (0, 0) to (n, m)?
(n, m)
• There is 1 path to (0, 0)
• There is 1 path to (1, 0) and (0, 1)

1 • Paths to (1, 1) is the sum of number


of paths to (0, 1) and (1, 0).
1 3
• Number of paths to (i, j) is the sum
1 2 3 of the number of paths to (i − 1, j)
(0, 0) and (i, j − 1).
1 1 1 1

47
Example

How many different lattice paths are there from (0, 0) to (n, m)?
(n, m)
1 6 21 56 126 252 462 • There is 1 path to (0, 0)
1 5 15 35 70 126 210 • There is 1 path to (1, 0) and (0, 1)

1 4 10 20 35 56 84 • Paths to (1, 1) is the sum of number


of paths to (0, 1) and (1, 0).
1 3 6 10 15 21 28
• Number of paths to (i, j) is
1 2 3 4 5 6 7 i + j 
(0, 0)
1 1 1 1 1 1 1 i

47
Catalan

What if we are not allowed to cross the main diagonal?

(n, m)

(0, 0)

48
Catalan

What if we are not allowed to cross the main diagonal?


• The number of paths from (0, 0) to
(n, m)
(n, m)
 
1 2n
Cn =
n+1 n

(0, 0)

48
Catalan

What if we are not allowed to cross the main diagonal?


• The number of paths from (0, 0) to
(n, m)
(n, m)
 
1 2n
Cn =
n+1 n

• Cn are known as Catalan numbers.


• Many problems involve solutions given
(0, 0)
by the Catalan numbers.

48
Catalan

• Number of different ways n + 1 factors can be completely


parenthesized.

((ab)c)d (a(bc))d (ab)(cd) a((bc)d) a(b(cd))

49
Catalan

• Number of different ways n + 1 factors can be completely


parenthesized.

((ab)c)d (a(bc))d (ab)(cd) a((bc)d) a(b(cd))

• Number of stack sortable permutations of length n.

49
Catalan

• Number of different ways n + 1 factors can be completely


parenthesized.

((ab)c)d (a(bc))d (ab)(cd) a((bc)d) a(b(cd))

• Number of stack sortable permutations of length n.


• Number of different triangulations convex polygon with n + 2 sides

49
Catalan

• Number of different ways n + 1 factors can be completely


parenthesized.

((ab)c)d (a(bc))d (ab)(cd) a((bc)d) a(b(cd))

• Number of stack sortable permutations of length n.


• Number of different triangulations convex polygon with n + 2 sides

• Number of full binary trees with n + 1 leaves.

49
Catalan

• Number of different ways n + 1 factors can be completely


parenthesized.

((ab)c)d (a(bc))d (ab)(cd) a((bc)d) a(b(cd))

• Number of stack sortable permutations of length n.


• Number of different triangulations convex polygon with n + 2 sides

• Number of full binary trees with n + 1 leaves.


• And aloot more.

49
Fibonacci

The Fibonacci sequence is defined recursively as

f1 = 1
f2 = 1
fn = fn−1 + fn−2

50
Fibonacci

The Fibonacci sequence is defined recursively as

f1 = 1
f2 = 1
fn = fn−1 + fn−2

Already covered how to calculate fn in O(N) time with dynamic


programming.

50
Fibonacci

The Fibonacci sequence is defined recursively as

f1 = 1
f2 = 1
fn = fn−1 + fn−2

Already covered how to calculate fn in O(N) time with dynamic


programming.
But we can do even better.

50
Fibonacci as matrix

The Fibonacci sequence can be represented by a vectors


! ! !
fn+2 1 1 fn+1
=
fn+1 1 0 fn

51
Fibonacci as matrix

The Fibonacci sequence can be represented by a vectors


! ! !
fn+2 1 1 fn+1
=
fn+1 1 0 fn

Or simply as a matrix
!n !
1 1 fn+1 fn
=
1 0 fn fn−1

51
Fibonacci as matrix

The Fibonacci sequence can be represented by a vectors


! ! !
fn+2 1 1 fn+1
=
fn+1 1 0 fn

Or simply as a matrix
!n !
1 1 fn+1 fn
=
1 0 fn fn−1

Using fast exponentiaton, we can calculate fn in O(log N) time.

51
Fibonacci as matrix

Any linear recurrence

an = c1 an−1 + c2 an−2 . . . ck an−k

can be expressed in the same way


    
an+1 c1 c2 ... ck an
 an   1 0 ... 0   an−1 
    
 . =. ..  . 
 .  .  . 
 .  . .  . 
an−k 0 0 ... 0 an−k−1

52
Fibonacci as matrix

Any linear recurrence

an = c1 an−1 + c2 an−2 . . . ck an−k

can be expressed in the same way


    
an+1 c1 c2 ... ck an
 an   1 0 ... 0   an−1 
    
 . =. ..  . 
 .  .  . 
 .  . .  . 
an−k 0 0 ... 0 an−k−1

With a recurrence relation defined as a linear function of the k preceding


terms the running time will be O(k 3 log N).

52
Game Theory
Game theory

Game theory is the study of strategic decision making, but in competitive


programming we are mostly interested in combinatorial games.

54
Game theory

Game theory is the study of strategic decision making, but in competitive


programming we are mostly interested in combinatorial games.
Example:

• There is a pile of k matches.


• Player can remove 1, 2 or 3 from the pile and alternate on moves.
• The player who removes the last match wins.
• There are two players, and the first player starts.
• Assuming that both players play perfectly, who wins?

54
Example

We can analyse these types of games with backward induction.

55
Example

We can analyse these types of games with backward induction.


We call a state N-position if it is a winning state for the next player to
move, and P-position if it is a winning position for the previous player.

• All terminal positions are P-positions.


• If every reachable state from the current one is a N-position then
the current state is a P-position.
• If at least one P-position can be reached from the current one, then
the current state is a N-position.
• A position is a P-position if all reachable states form the current one
are N position.

55
Example

Let’s analyse our previous game.

56
Example

Let’s analyse our previous game.

• The terminal position is a P-position.

0 1 2 3 4 5 6 7 8 9 10 11 12 ...
P

56
Example

Let’s analyse our previous game.

• The terminal position is a P-position.


• The positions reachable from the terminal positions are N-positions.

0 1 2 3 4 5 6 7 8 9 10 11 12 ...
P N N N

56
Example

Let’s analyse our previous game.

• The terminal position is a P-position.


• The positions reachable from the terminal positions are N-positions.
• Position 4 can only reach N-positions, therefore a P position.

0 1 2 3 4 5 6 7 8 9 10 11 12 ...
P N N N P

56
Example

Let’s analyse our previous game.

• The terminal position is a P-position.


• The positions reachable from the terminal positions are N-positions.
• Position 4 can only reach N-positions, therefore a P position.
• The next 3 positions can reach the P-position 4, therefore they are
N-positions.

0 1 2 3 4 5 6 7 8 9 10 11 12 ...
P N N N P N N N

56
Example

Let’s analyse our previous game.

• The terminal position is a P-position.


• The positions reachable from the terminal positions are N-positions.
• Position 4 can only reach N-positions, therefore a P position.
• The next 3 positions can reach the P-position 4, therefore they are
N-positions.
• And so on.

0 1 2 3 4 5 6 7 8 9 10 11 12 ...
P N N N P N N N P N N N P ...

56
Game theory

We can see a clear pattern of the N and P positions in the previous


game. – Easy to prove that a position is P if x ≡ 0 (mod 4).

57
Game theory

We can see a clear pattern of the N and P positions in the previous


game. – Easy to prove that a position is P if x ≡ 0 (mod 4).

• Many games can be analyzed this way.


• Not only one dimensional games.

57
Game theory

We can see a clear pattern of the N and P positions in the previous


game. – Easy to prove that a position is P if x ≡ 0 (mod 4).

• Many games can be analyzed this way.


• Not only one dimensional games.
• What if there are n piles instead of 1?

57
Game theory

We can see a clear pattern of the N and P positions in the previous


game. – Easy to prove that a position is P if x ≡ 0 (mod 4).

• Many games can be analyzed this way.


• Not only one dimensional games.
• What if there are n piles instead of 1?
• What if we can remove 1, 3 or 4?

57
The game called Nim

• There are n piles, each containing xi chips.


• Player can remove from exactly one pile, and can remove any
number of chips.
• The player who removes the last match wins.
• There are two players, and the first player starts and they alternate
on moves.
• Assuming that both players play perfectly, who wins?

58
The game called Nim

Nim can be analyzed with N and P position.

59
The game called Nim

Nim can be analyzed with N and P position.

• Not trivial to generalize over n piles.

59
The game called Nim

Nim can be analyzed with N and P position.

• Not trivial to generalize over n piles.


• Luckily someone has already done that for us.

59
The game called Nim

Nim can be analyzed with N and P position.

• Not trivial to generalize over n piles.


• Luckily someone has already done that for us.

Buton’s theorem states that a position (x1 , x2 , . . . , xn ) in Nim is a


P-position if and only if the xor over the number of chips is 0.

59
The game called Nim

Nim can be analyzed with N and P position.

• Not trivial to generalize over n piles.


• Luckily someone has already done that for us.

Buton’s theorem states that a position (x1 , x2 , . . . , xn ) in Nim is a


P-position if and only if the xor over the number of chips is 0.
This theorem extends to other sums of combinatorial games!

59

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy