Greatest Common Divisor (GCD) is the largest number which is a divisor of all given numbers. We can compute GCD(a,b)
in O(log(min(a,b)))
.
The two implementations are available. Binary GCD is a little bit more efficient than standard one by using only binary operations.
- GCD and LCM | CodeChef
- Greatest Common Divisor - AOJ ALDS1B
- Disjoint Set of Common Divisors - AtCoder ABC142D
Least Common Multiple (LCM) is the smallest number which is a multiple of all given numbers. We can also compute LCM(a,b)
in O(log(min(a,b)))
.
Binary exponentiation is a way to calculate the n
-th power of a
in O(log(n))
.
The algorithm requires only O(log(n))
multiplications.
Binary Exponentiation | C++ code
Prime Factorization or Integer Factorization is a way to break a number into a set of prime numbers. All numbers in the set multiply together to result in the original number.
- Trivial | C++ code in
O(sqrt(N))
- Prime Factorization - AOJ NTL1A
- Factorization - AtCoder ABC110D
- Disjoint Set of Common Divisors - AtCoder ABC142D
Binomial coefficients (n,k)
are the number of ways to pick a set of k
elements up from n
different elements.
The order of elements picked up is not taken into account.
- Binomial Coefficients using Pascal's Triangle | C++ code
- Precomputation in
O(N**2)
- Calculation in
O(1)
- Precomputation in
- Binomial Coefficients in Modulo | C++ code
- Precomputation in
O(N)
- Calculation in
O(1)
- Precomputation in
Number of combinations with repetition | Combination - Wikipedia
- 多重ループ - AtCoder ABC021D
- 経路 - AtCoder ABC034C
- いろはちゃんとマス目 - AtCoder ABC042D
- Maximum Average Sets - AtCoder ABC057D
- Factorization - AtCoder ABC110D
- E - Cell Distance
- D - Bouquet
- E - Roaming
- E - Colorful Blocks
Extended Euclidean Algorithm is a way to compute a integer solution (x,y)
of ax + by = gcd(a,b)
.
We can compute the solution in O(log(min(a,b)))
.
- Extended Euclidean Algorithm (iterative solution) | C++ code
- Extended Euclidean Algorithm (recursive solution) | C++ code
A Modular Multiplicative Inverse of an integer a
is an integer x
such that the product ax
is congruent to 1 in modulus m
.