Skip to main content

ProofsPath · 12 min

Modular Arithmetic Essentials for Contests

Important
For:GeneralResearch

Modular Arithmetic Essentials for Contests

Why This Matters

Modular arithmetic is the working language of contest number theory. Almost every olympiad number-theory problem either is a modular-arithmetic problem or reduces to one in the first few steps. The basic claim:

Two integers aa and bb are congruent modulo nn, written ab(modn)a \equiv b \pmod{n}, if nabn \mid a - b. The relation partitions Z\mathbb{Z} into nn equivalence classes, and arithmetic operations descend to those classes in a controlled way.

The technique-level question every contest writer must answer: which operations are sound modulo nn, and which are treacherous? Get this right and Fermat, Euler, Wilson, CRT, and quadratic residues are all natural extensions. Get it wrong and your "proof" silently violates the rules in step 3.

Recognize a modular argument when:

  • The problem asks about divisibility, remainders, or "last digits" of an integer expression.
  • The problem involves periodic or cyclic behavior of integer sequences (powers, Fibonacci, factorials).
  • The problem reduces to showing some integer expression is or is not a perfect square / cube / kk-th power.
  • The problem statement itself contains "mod\bmod" or "\equiv".

This page covers the fundamentals every olympiad student must own cold: the equivalence relation, the four sound operations, the four unsound operations, modular inverses, and linear congruences. The remaining number-theory pages build on this base.

The Equivalence Relation

Theorem

Congruence Modulo n is an Equivalence Relation

Statement

Define ab(modn)a \equiv b \pmod{n} iff nabn \mid a - b. This is a reflexive, symmetric, and transitive relation on Z\mathbb{Z}. The equivalence classes are exactly the nn sets {a+kn:kZ}\{a + kn : k \in \mathbb{Z}\} for a{0,1,,n1}a \in \{0, 1, \ldots, n - 1\}.

Intuition

"Same remainder when divided by nn" is the operational content. The equivalence classes (called residue classes modn\bmod n) form the quotient ring Z/nZ\mathbb{Z} / n \mathbb{Z}, which has nn elements. Arithmetic operations on integers descend to operations on residue classes provided they respect the equivalence.

Proof Sketch

Reflexivity: n0=aan \mid 0 = a - a, so aaa \equiv a.

Symmetry: $n \mid a - b \Leftrightarrow n \mid -(a - b) = b

  • a,so, so a \equiv b \Leftrightarrow b \equiv a$.

Transitivity: nabn \mid a - b and nbcn \mid b - c give n(ab)+(bc)=acn \mid (a - b) + (b - c) = a - c, so aba \equiv b and bcb \equiv c give aca \equiv c.

Equivalence classes: by the division algorithm every integer aa can be written uniquely as a=qn+ra = q n + r with 0r<n0 \le r < n. The class of aa is determined by rr. Hence the nn classes correspond to r{0,1,,n1}r \in \{0, 1, \ldots, n - 1\}.

Why It Matters

The equivalence-relation framing is what makes "modn\bmod n" behave like a number system: you can replace any integer by any other integer in the same class, and arithmetic still works. This freedom is the source of every clean modular-arithmetic argument.

Failure Mode

Equivalence is preserved by addition and multiplication; it is not automatically preserved by exponentiation in the exponent (changing the base mod nn is fine; changing the exponent mod nn generally is not). It is also not preserved by cancellation unless you have coprimality. Both are detailed below.

The Four Sound Operations

If aa(modn)a \equiv a' \pmod{n} and bb(modn)b \equiv b' \pmod{n}, then:

  1. a+ba+b(modn)a + b \equiv a' + b' \pmod{n}
  2. abab(modn)a - b \equiv a' - b' \pmod{n}
  3. abab(modn)a b \equiv a' b' \pmod{n}
  4. ak(a)k(modn)a^k \equiv (a')^k \pmod{n} for any non-negative integer kk (operation 3 iterated)

These four operations descend cleanly because the relation is defined by an additive subgroup (nZn \mathbb{Z}) that is also closed under multiplication.

You can substitute any representative of a residue class for any other when computing sums, differences, products, and powers. This is the freedom that makes modular arithmetic practical: 123456789(mod7)123456789 \pmod 7 becomes 1+2+3+4+5+6+7+8+9?(mod7)1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 \equiv ? \pmod 7 via "replace each digit position by its weight mod7\bmod 7".

The Four Unsound Operations

These do not descend to residue classes, or descend only under additional hypotheses.

Division by bb. ab(modn)a \equiv b \pmod n and ca,cbc \mid a, c \mid b does not imply a/cb/c(modn)a/c \equiv b/c \pmod n. Example: 104(mod6)10 \equiv 4 \pmod 6 but 10/2=5≢2=4/2(mod6)10/2 = 5 \not\equiv 2 = 4/2 \pmod 6. Division is only sound when gcd(c,n)=1\gcd(c, n) = 1 (in which case it is multiplication by c1c^{-1}, see below).

Cancellation. acbc(modn)a c \equiv b c \pmod n does not imply ab(modn)a \equiv b \pmod n unless gcd(c,n)=1\gcd(c, n) = 1. Example: 2303(mod6)2 \cdot 3 \equiv 0 \cdot 3 \pmod 6 but 2≢0(mod6)2 \not\equiv 0 \pmod 6.

Exponentiation in the exponent. kk(modn)k \equiv k' \pmod n does not imply akak(modn)a^k \equiv a^{k'} \pmod n in general. To reduce the exponent you need a separate condition: by Fermat or Euler, the exponent reduces modφ(n)\bmod \, \varphi(n) when gcd(a,n)=1\gcd(a, n) = 1.

Mixed moduli. ab(modn)a \equiv b \pmod n and ab(modm)a \equiv b \pmod m do not in general combine to a useful relation (modnm)\pmod{nm} unless gcd(n,m)=1\gcd(n, m) = 1, in which case CRT applies.

These four unsound operations are the single largest source of errors in olympiad number-theory writeups. Beginners apply them by reflex from ordinary arithmetic and produce statements that look right but are not.

Modular Inverses

Theorem

Existence of Modular Inverses

Statement

aa has a multiplicative inverse modulo nn, i.e., there exists bb with ab1(modn)a b \equiv 1 \pmod n, if and only if gcd(a,n)=1\gcd(a, n) = 1.

Intuition

Bezout's identity: gcd(a,n)=1\gcd(a, n) = 1 iff there exist integers b,kb, k with ab+nk=1a b + n k = 1, i.e., ab1(modn)a b \equiv 1 \pmod n.

When the inverse exists it is unique modulo nn, and "division by aa" modulo nn is well-defined as multiplication by the inverse. When gcd(a,n)>1\gcd(a, n) > 1, division is genuinely not well-defined; you cannot work around this.

Proof Sketch

(\Leftarrow) If gcd(a,n)=1\gcd(a, n) = 1, Bezout gives ab+nk=1a b + n k = 1 for some integers b,kb, k. Then ab1(modn)a b \equiv 1 \pmod n, so bb is the inverse.

(\Rightarrow) If ab1(modn)a b \equiv 1 \pmod n for some bb, then ab1=kna b - 1 = k n for some kk, so abkn=1a b - k n = 1. Any common divisor of aa and nn divides abkn=1a b - k n = 1, hence is 1. So gcd(a,n)=1\gcd(a, n) = 1.

Why It Matters

Modular inverses turn (Z/nZ)(\mathbb{Z}/n\mathbb{Z})^* — the set of residue classes coprime to nn — into an abelian group under multiplication. This group has order φ(n)\varphi(n) (Euler's totient), and Lagrange's theorem on it gives Euler's theorem aφ(n)1(modn)a^{\varphi(n)} \equiv 1 \pmod n.

Inverses are computed via the extended Euclidean algorithm, which runs in O(logn)O(\log n) steps. They are also computed via Fermat's little theorem when nn is prime: a1ap2(modp)a^{-1} \equiv a^{p-2} \pmod p.

Failure Mode

22 has no inverse modulo 44, because gcd(2,4)=2\gcd(2, 4) = 2. Any "2b1(mod4)2 b \equiv 1 \pmod 4" attempt is doomed. In particular, problems that involve 12\frac{1}{2} in modular arithmetic modulo an even number are ill-defined; rephrase to avoid the inverse before applying any technique.

Worked Example: A Linear Congruence

Example

Solve $7x \equiv 3 \pmod{15}$

Problem. Find all integer solutions to 7x3(mod15)7 x \equiv 3 \pmod {15}.

Solution. gcd(7,15)=1\gcd(7, 15) = 1 so 77 has an inverse mod 15. Find it via the extended Euclidean algorithm: $15 = 2 \cdot 7

  • 1,so, so 1 = 15 - 2 \cdot 7,giving, giving 7 \cdot (-2) \equiv 1 \pmod,i.e.,, i.e., 7^ \equiv -2 \equiv 13 \pmod$.

Multiply both sides of 7x37 x \equiv 3 by 1313: x133=399(mod15)x \equiv 13 \cdot 3 = 39 \equiv 9 \pmod{15}.

So the solutions are x{9,24,39,,6,21,}x \in \{9, 24, 39, \ldots, -6, -21, \ldots\}, i.e., x9(mod15)x \equiv 9 \pmod{15}.

Verification: 79=63=415+33(mod15)7 \cdot 9 = 63 = 4 \cdot 15 + 3 \equiv 3 \pmod {15}. ✓

Worked Example: Last Two Digits of a Power

Example

Last two digits of $3^{100}$

Problem. Compute the last two digits of 31003^{100}.

Solution. "Last two digits" means 3100(mod100)3^{100} \pmod{100}.

By the Chinese Remainder Theorem (since 100=425100 = 4 \cdot 25 with gcd(4,25)=1\gcd(4, 25) = 1), it suffices to compute 3100(mod4)3^{100} \pmod 4 and 3100(mod25)3^{100} \pmod{25} then combine.

3100(mod4)3^{100} \pmod 4: 31(mod4)3 \equiv -1 \pmod 4, so 3100(1)100=1(mod4)3^{100} \equiv (-1)^{100} = 1 \pmod 4.

3100(mod25)3^{100} \pmod{25}: gcd(3,25)=1\gcd(3, 25) = 1 and φ(25)=20\varphi(25) = 20, so by Euler's theorem 3201(mod25)3^{20} \equiv 1 \pmod {25}. Hence 3100=(320)51(mod25)3^{100} = (3^{20})^5 \equiv 1 \pmod{25}.

So 31001(mod4)3^{100} \equiv 1 \pmod 4 and 1(mod25)\equiv 1 \pmod{25}. By CRT, 31001(mod100)3^{100} \equiv 1 \pmod{100}, i.e., the last two digits are 01.

This is the canonical "modn\bmod n as a primary tool" technique: factor nn, compute mod each factor (using Euler or Fermat to reduce exponents), recombine with CRT.

Common Mistakes

Watch Out

Dividing in modular arithmetic without coprimality

"80(mod4)8 \equiv 0 \pmod 4, so dividing by 4 gives 20(mod4)2 \equiv 0 \pmod 4" is wrong. Division by aa modulo nn is only well-defined when gcd(a,n)=1\gcd(a, n) = 1. Without coprimality, "divide by aa" must be replaced by "factor aa from both sides and reduce the modulus" (so axay(modka)a x \equiv a y \pmod{ka} becomes xy(modk)x \equiv y \pmod k).

Watch Out

Reducing the exponent without justification

2100(mod7)2^{100} \pmod 7 is not computed by reducing the 100 mod 7. Reducing exponents requires either Fermat (modulus prime and base coprime) or Euler (general modulus, base coprime to modulus). The base reduces modulo nn; the exponent reduces modulo φ(n)\varphi(n) (when applicable).

Watch Out

Confusing mod with divisibility

"a0(modn)a \equiv 0 \pmod n" means nan \mid a. Beginners sometimes treat "modn\bmod n" as a function returning the remainder (the programmer's view) rather than as a relation between integers (the mathematician's view). Both are valid notations; the relation form is more flexible for proof writing.

Watch Out

Mixing moduli without CRT

"a1(mod2)a \equiv 1 \pmod 2 and a2(mod3)a \equiv 2 \pmod 3 so a?(mod6)a \equiv ? \pmod 6" is solved by CRT (specifically a5(mod6)a \equiv 5 \pmod 6), not by averaging or combining the residues directly. gcd(2,3)=1\gcd(2, 3) = 1 is what makes CRT work.

Watch Out

Applying mod after the fact

If you compute a giant integer expression "honestly" and only then take mod, you have done none of the simplification work. Apply mod to each step: replace 123456(mod7)123 \cdot 456 \pmod{7} by (123mod7)(456mod7)=41=4(mod7)(123 \bmod 7) \cdot (456 \bmod 7) = 4 \cdot 1 = 4 \pmod 7. Modular arithmetic is most powerful when you apply it as you go.

Exercises

ExerciseCore

Problem

Find 7222(mod11)7^{222} \pmod{11}.

ExerciseCore

Problem

Find all xx with 0x<300 \le x < 30 satisfying 4x6(mod10)4 x \equiv 6 \pmod{10}.

ExerciseAdvanced

Problem

Show that for all positive integers nn, n7nn^7 - n is divisible by 42.

Cross-Network Links

  • ProofsPath: fermats-little-theorem-applications is the prime-modulus exponent-reduction tool; eulers-theorem-applications generalizes Fermat to composite moduli; chinese-remainder-theorem-applications combines multiple moduli; quadratic-residues-and-legendre-symbol is the structured study of squares mod pp.
  • TheoremPath: game-theory uses modular arithmetic in finite-strategy game analysis; the Nim-sum is a Z/2Z\mathbb{Z}/2\mathbb{Z} vector-space computation.
  • ComputationPath: modular exponentiation by repeated squaring is the practical algorithm for the operations on this page; cryptographic primitives (RSA, Diffie-Hellman, ECC) all live here.

References

See structured references block. Primary entry points: Hardy-Wright Ch 5-6 for the canonical analytic treatment; Burton Elementary Number Theory Ch 4-5 for a textbook-style treatment with worked examples; Engel Problem-Solving Strategies Ch 6 for olympiad-flavored applications; Chen's online text Modern Olympiad Number Theory for the contest synthesis. Gauss's Disquisitiones Arithmeticae (1801) is the foundational primary source.