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 and are congruent modulo , written , if . The relation partitions into 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 , 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 / -th power.
- The problem statement itself contains "" or "".
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
Congruence Modulo n is an Equivalence Relation
Statement
Define iff . This is a reflexive, symmetric, and transitive relation on . The equivalence classes are exactly the sets for .
Intuition
"Same remainder when divided by " is the operational content. The equivalence classes (called residue classes ) form the quotient ring , which has elements. Arithmetic operations on integers descend to operations on residue classes provided they respect the equivalence.
Proof Sketch
Reflexivity: , so .
Symmetry: $n \mid a - b \Leftrightarrow n \mid -(a - b) = b
- aa \equiv b \Leftrightarrow b \equiv a$.
Transitivity: and give , so and give .
Equivalence classes: by the division algorithm every integer can be written uniquely as with . The class of is determined by . Hence the classes correspond to .
Why It Matters
The equivalence-relation framing is what makes "" 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 is fine; changing the exponent mod generally is not). It is also not preserved by cancellation unless you have coprimality. Both are detailed below.
The Four Sound Operations
If and , then:
- for any non-negative integer (operation 3 iterated)
These four operations descend cleanly because the relation is defined by an additive subgroup () 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: becomes via "replace each digit position by its weight ".
The Four Unsound Operations
These do not descend to residue classes, or descend only under additional hypotheses.
Division by . and does not imply . Example: but . Division is only sound when (in which case it is multiplication by , see below).
Cancellation. does not imply unless . Example: but .
Exponentiation in the exponent. does not imply in general. To reduce the exponent you need a separate condition: by Fermat or Euler, the exponent reduces when .
Mixed moduli. and do not in general combine to a useful relation unless , 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
Existence of Modular Inverses
Statement
has a multiplicative inverse modulo , i.e., there exists with , if and only if .
Intuition
Bezout's identity: iff there exist integers with , i.e., .
When the inverse exists it is unique modulo , and "division by " modulo is well-defined as multiplication by the inverse. When , division is genuinely not well-defined; you cannot work around this.
Proof Sketch
() If , Bezout gives for some integers . Then , so is the inverse.
() If for some , then for some , so . Any common divisor of and divides , hence is 1. So .
Why It Matters
Modular inverses turn — the set of residue classes coprime to — into an abelian group under multiplication. This group has order (Euler's totient), and Lagrange's theorem on it gives Euler's theorem .
Inverses are computed via the extended Euclidean algorithm, which runs in steps. They are also computed via Fermat's little theorem when is prime: .
Failure Mode
has no inverse modulo , because . Any "" attempt is doomed. In particular, problems that involve in modular arithmetic modulo an even number are ill-defined; rephrase to avoid the inverse before applying any technique.
Worked Example: A Linear Congruence
Solve $7x \equiv 3 \pmod{15}$
Problem. Find all integer solutions to .
Solution. so has an inverse mod 15. Find it via the extended Euclidean algorithm: $15 = 2 \cdot 7
- 11 = 15 - 2 \cdot 77 \cdot (-2) \equiv 1 \pmod7^ \equiv -2 \equiv 13 \pmod$.
Multiply both sides of by : .
So the solutions are , i.e., .
Verification: . ✓
Worked Example: Last Two Digits of a Power
Last two digits of $3^{100}$
Problem. Compute the last two digits of .
Solution. "Last two digits" means .
By the Chinese Remainder Theorem (since with ), it suffices to compute and then combine.
: , so .
: and , so by Euler's theorem . Hence .
So and . By CRT, , i.e., the last two digits are 01.
This is the canonical " as a primary tool" technique: factor , compute mod each factor (using Euler or Fermat to reduce exponents), recombine with CRT.
Common Mistakes
Dividing in modular arithmetic without coprimality
", so dividing by 4 gives " is wrong. Division by modulo is only well-defined when . Without coprimality, "divide by " must be replaced by "factor from both sides and reduce the modulus" (so becomes ).
Reducing the exponent without justification
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 ; the exponent reduces modulo (when applicable).
Confusing mod with divisibility
"" means . Beginners sometimes treat "" 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.
Mixing moduli without CRT
" and so " is solved by CRT (specifically ), not by averaging or combining the residues directly. is what makes CRT work.
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 by . Modular arithmetic is most powerful when you apply it as you go.
Exercises
Problem
Find .
Problem
Find all with satisfying .
Problem
Show that for all positive integers , 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 .
- TheoremPath: game-theory uses modular arithmetic in finite-strategy game analysis; the Nim-sum is a 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.