Now we go deeper into modular arithmetic: inverses (the modular version of division), Fermat's little theorem, Euler's theorem, Wilson's theorem, and fast exponentiation. These are the workhorses of competition number theory β they let you compute huge powers instantly and solve congruences like a pro.
The inverse of $a$ modulo $m$ is a number $a^{-1}$ such that:
\[ a \cdot a^{-1} \equiv 1 \pmod{m} \]
The inverse exists if and only if gcd(a, m) = 1 (a and m are coprime).
When it exists, it's unique mod m.
\(Example: 3 \\times 5 = 15 \\equiv 1 (mod 7), so the inverse of 3 mod 7 is 5.\)
Method 1: Brute force (for small m) β \(try a \\times 1, a \\times 2, ... until you get 1 mod m.\)
Method 2: Extended Euclidean Algorithm β find integers x, y such that ax + my = 1. Then x mod m is the inverse.
Method 3: Fermat's Little Theorem (when m is prime) β see below.
If $p$ is prime and $a$ is not divisible by $p$:
\[ a^{p-1} \equiv 1 \pmod{p} \]
Equivalently: $ a^p \equiv a \pmod{p} $ for any integer a.
Application to inverses: If p is prime and p β€ a, then $ a^{-1} \equiv a^{p-2} \pmod{p} $.
Why: multiply both sides of $ a^{p-1} \equiv 1 $ by $ a^{-1} $ to get $ a^{p-2} \equiv a^{-1} $.
Application to exponents: $ a^k \equiv a^{k \bmod (p-1)} \pmod{p} $ (when a not divisible by p). Reduces huge exponents fast!
Ο(n) counts how many integers from 1 to n are coprime to n.
Formula: If $ n = p_1^{a_1} p_2^{a_2} ... p_k^{a_k} $:
\[ \varphi(n) = n \left(1 - \frac{1}{p_1}\right)\left(1 - \frac{1}{p_2}\right)...\left(1 - \frac{1}{p_k}\right) \]
Examples:
If gcd(a, n) = 1:
\[ a^{\varphi(n)} \equiv 1 \pmod{n} \]
This is the generalization of Fermat's Little Theorem to composite moduli.
FLT is the special case where n is prime (Ο(p) = p-1).
Application: $ a^k \equiv a^{k \bmod \varphi(n)} \pmod{n} $ when gcd(a,n) = 1. Reduces exponents modulo Ο(n).
An integer $p > 1$ is prime if and only if:
\[ (p-1)! \equiv -1 \pmod{p} \]
\(Example: p = 5. (5-1)! = 4! = 24 \\equiv 24 - 25 = -1 (mod 5).\) β
Useful for theoretical proofs and some competition problems about factorials mod primes.
Compute $ a^n \pmod{m} $ in O(log n) multiplications instead of O(n).
Idea: Break the exponent into powers of 2.
Example: $ a^{13} = a^8 \times a^4 \times a^1 $ (since 13 = 8 + 4 + 1 = \(1101_{2})\)
Algorithm:
If moduli are pairwise coprime, a system of congruences has a unique solution modulo their product.
We'll cover this in detail in the CRT module.
7 is prime. Use Fermat's Little Theorem: $ a^{p-1} \equiv 1 \pmod{p} $
Step 1: p = 7, so p-1 = 6.
$ 2^6 \equiv 1 \pmod{7} $
Step 2: Reduce exponent 100 mod 6.
100 Γ· 6 = 16 remainder 4
\(100 \\equiv 4 (mod 6)\)
Step 3: $ 2^{100} \equiv 2^4 \pmod{7} $
$ 2^4 = 16 $
16 mod 7 = 16 - 14 = 2
Step 4: Verify with cycle method.
\((2^{1}=2, 2^{2}=4, 2^{3}=8 \\equiv\) 1 mod 7. Wait\) β cycle length is 3, not 6!
\(100 mod 3 = 1, so 2^{1}^{0}^{0} \\equiv 2^{1} = 2 mod 7. Same answer\) β
(FLT gives an upper bound on cycle length; the actual cycle can be shorter.)
Answer: 2
Step 1: Prime factorization of 36.
\(36 = 2^{2} \\times 3^{2}\)
Step 2: Apply Euler's totient formula.
\[ \varphi(n) = n \left(1 - \frac{1}{p_1}\right)\left(1 - \frac{1}{p_2}\right)... \]
\[ \varphi(36) = 36 \left(1 - \frac{1}{2}\right)\left(1 - \frac{1}{3}\right) \]
$ = 36 \times \frac{1}{2} \times \frac{2}{3} $
$ = 36 \times \frac{1}{3} = 12 $
Check: Numbers 1-36 coprime to 36 (not divisible by 2 or 3):
1, 5, 7, 11, 13, 17, 19, 23, 25, 29, 31, 35 = 12 numbers. β
Answer: 12
11 is prime. Use Fermat's Little Theorem: inverse of a mod p = $ a^{p-2} \pmod{p} $
Method 1: FLT
Inverse of 4 mod 11 = $ 4^{11-2} = 4^9 \pmod{11} $
\((4^{2} = 16 \\equiv\) 5 mod 11\)
\((4^{4} = (4^{2})^{2} \\equiv 5^{2} = 25 \\equiv\) 3 mod 11\)
\((4^{8} = (4^{4})^{2} \\equiv 3^{2}\) = 9 mod 11\)
\((4^{9} = 4^{8} \\times 4^{1} \\equiv 9 \\times 4 = 36 \\equiv\) 3 mod 11\)
Inverse = 3
Method 2: Brute force (verify)
\(4 \\times 1=4, 4 \\times 2=8, 4 \\times 3=12 \\equiv 1 mod 11\) β
Check: \(4 \\times 3 = 12 \\equiv 1 (mod 11).\) β
Answer: 3