Meet 1 - Blog
Created | |
---|---|
Tags |
Number Theory is the Queen of Mathematics. - Carl Friedrich Gauss
Introduction
Modular Arithmetic was debveloped by Carl Friedrich gauss in 1801. Since then, it has been used extensively to study properties of numbers and classes of numbers
To mention its several applications in Computer Science, Modular Arithmetic allows us to easily create groups, rings and fields which are fundamental building blocks of most modern public-key cryptosystems.
In this and the next few scheduled meets, we are, however, going to talk about applications of number theory and modular arithmetic with respect to competitive programming.
Divisibility
Before directly talking about the topic of Modular Arithmetic let us first revise the concepts on divisibility and the ideas revolving and .
- for some unique where
Greatest Common Divisor
Problem: Given two numbers and find the greatest common divisor of both.
This brings us to one of the oldest algorithms known to mankind. Euclidean algorithm to find of two numbers.
From our knowledge of divisibility let us formulate the following:
Then, .
// Make sure that a > b in here.
int gcd(int a, int b)
{
if (b == 0) return a;
return gcd(a, a%b);
}
Extended GCD Algorithm
Given any we can always represent the following. To prove this one can use a basic proof by contradiction.
Problem: Find these coefficients.
int gcd(int a, int b, int& x, int& y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
int x1, y1;
int d = gcd(b, a % b, x1, y1);
x = y1, y = x1 - y1 * (a / b);
return d;
}
Terminology and Notation
is defined as , where is the largest integer that doesn't exceed (This should always produce an integer between and inclusive).
In mathematical terms, if such that .
Also, if we have two expressions and then,
In C/C++ %
is the modulus operator.
For example 5 % 3 = 2
, 10 % 2 = 0
, -3 % 2 = -1
and 5 % -3 = 1
.
Also, x % 0
is not defined for obvious reasons.
Precedence order:
%
/
*
+
-
Therefore, a + b % m
is not equal to (a + b) % m
.
Modular Arithmetic
Basic Properties
Let's talk about some basic properties of the modular operation.
-
-
- If and then
1. Addition
Proof
Let,
and
Thus, , and on the other hane we have
2. Subtraction
Proof
Let,
and
Thus, , and on the other hane we have
3. Multiplication
Proof
Let,
and
Thus, , and on the other hane we have
Example
You can apply modulus operator to any ugly looking equation as many times as you want and the final result would not get affected.
Well this clearly is equivalent to the following.
4. Division
This is slightly more complicated, and requires a concept called the "modular multiplicative inverse".
The modular multiplicative inverse of a number is the number s.t. . You may notice that this is similar to the concept of a reciprocal, but here we don't want a fraction; we want an integer, specifically an integer between 0 and inclusive.
We should note that the modular inverse does not always exist. For example, let . By checking all possible values modulo is should become clear that we cannot find satisfying the above equation. It can be proven that the modular inverse exists if and only if are relatively prime, .
But how do you actually find such a number? Bruteforcing all numbers to a prime number close to a billion will usually cause you to exceed the time limit. There are two faster ways to calculate the inverse: the extended GCD algorithm, and Fermat's little theorem.
-
Fermat's Result : , where is prime.
- Extended GCD algorithm is explained below.
Further Properties
-
- has a unique solution modulo if and only if
- and thus will have solutions, if and only if
- and has a unique solution modulo , if and only if
Try to prove these on your own!
Theorems and Methods
Chinese Remainder Theorem
Let, and then, the solution for shall be
where .
Fermat's Theorem
According to fermat's Theorem we have the following which holds true if and only if .
Proof Outline
We have .
From the equation above the Fermat's theorem, called formally as Fermat's Little Theorem in contrast with his last theorem, can be easily derived and hence proven.
Wilson's Theorem
Proof Outline
We know that
Backstory: Probably Leibnitz was the first person to find this shit but he never published. I am not quite sure who finally wrote the formal proof of this theorem. Probably, Euler.
More Theorems
- If are two primes such that and then,
- has a solution if and only if and is an odd prime.
Binary Exponentiation
Binary exponentiation (also known as exponentiation by squaring) is a trick which allows to calculate an using only multiplications (instead of multiplications required by the naive approach).
It also has important applications in many tasks unrelated to arithmetic, since it can be used with any operations that have the property of associativity:
Raising to the power of is expressed naively as multiplication by done times: . However, this approach is not practical for large a or n.
The idea of binary exponentiation is, that we split the work using the binary representation of the exponent.
Let's write n in base 2, for example:
Since the number n has exactly digits in base 2, we only need to perform multiplications, if we know the powers .
So we only need to know a fast way to compute those. Luckily this is very easy, since an element in the sequence is just the square of the previous element.
So to get the final answer for , we only need to multiply three of them (skipping because the corresponding bit in n is not set): = 6561⋅81⋅3 = 1594323
The final complexity of this algorithm is O(logn): we have to compute logn powers of a, and then have to do at most logn multiplications to get the final answer from them.
The following recursive approach expresses the same idea:
Implementation
// binpow(a, b) => a^b
long long binpow(long long a, long long b)
{
if (b == 0)
return 1;
long long res = binpow(a, b / 2);
if (b % 2)
return res * res * a;
else
return res * res;
}
long long binpow(long long a, long long b) {
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
Now the Coolest Question!
Problem: Find the fibonacci number.
where we have the matrix as