PrevNext

Euclidean Algorithm

Resources
cp-algo

Suppose we have non-negative integers aa and bb. The original Euclidean Algorithm computes gcd(a,b)\gcd(a,b) and looks like this:

def euclid(a: int, b: int) -> int:
assert (
int(a) > 0 and int(b) > 0
), "Arguments must be positive, non-zero numeric values."
while b > 0:
k = a // b
# subtract multiples of one equation from the other.
a -= b * k
a, b = b, a
return a

Extended Euclidean Algorithm

Resources
cp-algo
Wikipedia

The extended Euclidean algorithm computes integers xx and yy such that

ax+by=gcd(a,b).ax+by=\gcd(a,b).

We can slightly modify the version of the Euclidean algorithm given above to return more information!

def extend_euclid(a: int, b: int) -> list[int]:
assert (
int(a) > 0 and int(b) > 0
), "Arguments must be positive, non-zero numeric values."
# we know that 1 * a + 0 * b = a and 0 * a + 1 * b = b.
x_arr = [1, 0, int(a)]
y_arr = [0, 1, int(b)]
q = -1

Recursive Version

def euclid(a: int, b: int) -> int:
"""Recursive Euclidean GCD."""
return a if b == 0 else euclid(b, a % b)

becomes

def extend_euclid(a: int, b: int) -> list[int]:
if not b:
return [1, 0]
p = extend_euclid(b, a % b)
return [p[1], p[0] - (a // b) * p[1]]

The pair will equal the first two returned elements of the array in the iterative version. Looking at this version, we can prove by induction that when aa and bb are distinct positive integers, the returned pair (x,y)(x,y) will satisfy xb2gcd(a,b)|x|\le \frac{b}{2\gcd(a,b)} and ya2gcd(a,b)|y|\le \frac{a}{2\gcd(a,b)}. Furthermore, there can only exist one pair that satisfies these conditions!

Note that if we do all calculations in 64-bit integers, we won't wind up with overflow issues even when a,ba,b are quite large (say, 260\approx 2^{60}).

Application - Modular Inverse

Resources
cp-algo

Focus Problem – try your best to solve this problem before continuing!

Additionally, it seems that when multiplication / division is involved in this problem, n2<LLONG_MAXn^2 < \texttt{LLONG\_MAX}.

Solution

Hint

Solution

Application - Chinese Remainder Theorem

The Chinese Remainder Theorem (CRT) states that there is a unique solution to a system of simultaneous modular congruences with pairwise coprime moduli. Mathematically, suppose we are given pairwise coprime positive integers m1,m2,,mkm_1,m_2,\cdots,m_k, ai[0,mi)a_i\in [0, m_i), and the system

xa1(modm1)xa2(modm2)xa3(modm3)xak(modmk)(1)\begin{gathered} x \equiv a_1 \pmod{m_1} \\ x \equiv a_2 \pmod{m_2} \\ x \equiv a_3 \pmod{m_3} \\ \vdots \\ x \equiv a_k \pmod{m_k} \end{gathered} \tag{1}

If we let

M=m1m2m3mkM=m_1\cdot m_2 \cdot m_3 \cdots m_k

then there is exactly one solution for xx modulo MM.

It suffices to solve only the case k=2k=2, because for greater kk we can just combine pairs of modular congruences until only one remains.

xa1(modm1)xa2(modm2)(2)\begin{gathered} x \equiv a_1 \pmod{m_1} \\ x \equiv a_2 \pmod{m_2} \end{gathered} \tag{2}
M=m1m2M=m_1 \cdot m_2

Focus Problem – try your best to solve this problem before continuing!

Solution

Hint

Solution

Application - Linear Diophantine Equation

Resources
Brilliant

In-depth explanation

cp-algo

A Linear Diophantine Equation is an equation of the form

ax+by=cax + by = c

where aa, bb and cc are known constants, and we want to find integer values of xx and yy satisfying the equation. Each number in the equation has to be an integer.

We can solve a Linear Diophantine Equation using the extended Euclidean algorithm, which calculates values xgx_g and ygy_g that satisfy the equation:

axg+byg=gcd(a,b).ax_g + by_g = \gcd(a, b).

A Linear Diophantine Equation can be solved if cc is divisible by gcd(a,b)\gcd(a, b), and otherwise it cannot be solved. This observation is derived from Bézout's Identity.

Let's calculate the value of xx and yy assuming g=gcd(a,b)g = \gcd(a, b) divides cc. Since gcg | c we can multiply the Bézout equation through by c/gc / g, resulting in:

a(xgcg)+b(ygcg)=gcg=c.a \left(x_g \cdot \frac{c}{g}\right) + b \left(y_g \cdot \frac{c}{g}\right) = g \cdot \frac{c}{g} = c.

So one solution to ax+by=cax + by = c is x0=xgcg, y0=ygcgx_0 = x_g \cdot \frac{c}{g}, \ y_0 = y_g \cdot \frac{c}{g}.

A solution to the Diophantine equation is never unique, because we can form an infinite number of solutions if we know one solution.

Theorem: If (x0,y0)(x_0, y_0) is an integer solution of the Diophantine Equation ax+by=cax + by = c, then all integer solutions to the equation are of the form

(x0+kbgcd(a,b),y0kagcd(a,b))\left(x_0 + k \frac{b}{\gcd(a, b)}, y_0 - k \frac{a}{\gcd(a, b)}\right)

where kk is any integer.

Proof: We have

a(x0+kbgcd(a,b))+b(y0kagcd(a,b))=ax0+by0+abkgcd(a,b)abkgcd(a,b)=ax0+by0=c,\begin{aligned} a\left(x_0 + k \frac{b}{\gcd(a, b)}\right) + b\left(y_0 - k \frac{a}{\gcd(a, b)}\right) &= ax_0 + by_0 + \frac{abk}{\gcd(a, b)} - \frac{abk}{\gcd(a, b)} \\ &= ax_0 + by_0 \\ &= c, \end{aligned}

here note that the two kk-terms cancel each other out, so no matter what integer kk we choose, our equation results back in cc.

But we still need to check that we haven't missed any other solutions. So suppose (x,y)(x, y) is some solution to the equation — any solution at all. Since it agrees with (x0,y0)(x_0, y_0) on the right-hand side, we can subtract one from the other:

ax+by=ax0+by0a(xx0)=b(yy0)agcd(a,b)(xx0)=bgcd(a,b)(yy0).\begin{aligned} ax + by &= ax_0 + by_0 \\ a(x - x_0) &= -b(y - y_0) \\ \frac{a}{\gcd(a, b)}(x - x_0) &= -\frac{b}{\gcd(a, b)}(y - y_0). \end{aligned}

Here, note that the numbers agcd(a,b)\frac{a}{\gcd(a, b)} and bgcd(a,b)\frac{b}{\gcd(a, b)} are coprime (i.e., they have no factors left in common except 11). That means bgcd(a,b)\frac{b}{\gcd(a, b)} has no choice but to divide evenly into xx0x - x_0. In other words, xx0x - x_0 must be some whole multiple of bgcd(a,b)\frac{b}{\gcd(a, b)}; we call that multiple kk. Once we have fixed the value of kk, the equation above tells us yy0y - y_0 has to equal kagcd(a,b)-k \frac{a}{\gcd(a, b)}, proving the theorem.

Problems

This section is not complete.

Any help would be appreciated! Just submit a Pull Request on GitHub.

TODO: more problems

StatusSourceProblem NameDifficultyTags
ACMedium
Show TagsModular Arithmetic
KattisMedium
Show TagsModular Arithmetic

Module Progress:

PrevNext