Skip to content

Chapter 7: Mathematics

Overview

Math is the secret weapon that turns many "impossible" problems into three lines of code. This chapter covers everything from the bread-and-butter (modular arithmetic, primes, combinatorics) through probability and game theory, up to the heavy artillery (FFT/NTT, Gaussian elimination, XOR basis). You don't need all of it at once — number theory and combinatorics alone cover most math-tagged problems on Codeforces — but when a problem does need FFT or Sprague-Grundy, nothing else will work.

Coming from: Chapter 6 — Advanced Graph Algorithms — advanced graph decompositions, flows, and tree techniques
Going to: Chapter 8 — Geometry — computational geometry primitives and convex hull algorithms

Topics at a Glance

#TopicDifficultyKey Trigger Phrase
01Math FundamentalsBeginnerGCD, fast exponentiation, "mod 10⁹+7"
02Number TheoryIntermediate"Primes," "divisors," "factorization," "sieve"
03Extended Euclidean & CRTIntermediate"Modular inverse" or "solve system of congruences"
04Combinatorics BasicsIntermediate"Count arrangements" or "C(n, k) mod p"
05Catalan NumbersIntermediate"Balanced parentheses," "binary trees," "non-crossing partitions"
06Inclusion-ExclusionIntermediate"Count elements in A∪B∪C" or "at least one forbidden"
07Game Theory — Nim & Sprague-GrundyIntermediate"Two players, both play optimally, who wins?"
08Probability and Expected ValueIntermediate"Expected number of steps" or "probability of event"
09Matrix ExponentiationIntermediate"Linear recurrence" + huge n (10⁹+)
10Gaussian EliminationAdvanced"Solve linear system" or "XOR equations"
11Linear Basis (XOR Basis)Advanced"Maximum XOR subset" or "XOR span"
12FFT & NTTAdvanced"Polynomial multiplication" or "count pairs with sum = k"

Grouped by skill

The same topics, regrouped so it is clear what kind of reasoning each one trains:

  • Modular arithmetic and inverses — Math Fundamentals (01), Extended Euclidean & CRT (03).
  • Counting — Combinatorics Basics (04), Catalan Numbers (05), Inclusion-Exclusion (06).
  • Number-theoretic structure — Number Theory (02).
  • Recurrences and transforms — Matrix Exponentiation (09), FFT & NTT (12).
  • Linear algebra — Gaussian Elimination (10), Linear Basis / XOR (11).
  • Games and probability — Game Theory & Nim (07), Probability and Expected Value (08).

If You Only Read 3 Files

Prerequisite, not optional: Math Fundamentals is assumed throughout. Do not skip it — modular arithmetic, fast exponentiation, and GCD show up in every other file in this chapter.

  1. Number Theory — because primes, sieves, and divisor functions appear in a huge fraction of math problems. The Sieve of Eratosthenes alone solves dozens of problem types.
  2. Combinatorics Basics — because "count the ways" is one of the most common problem types on Codeforces, and you need to compute C(n, k) mod p fluently. Stars and Bars, PIE, and binomial coefficients are essential.
  3. Game Theory — Nim & Sprague-Grundy — because game theory problems look terrifying until you learn Sprague-Grundy theory, and then they become formulaic. XOR of Grundy values determines the winner.

Cross-Chapter Connections


File Listing

FileTopicLevel
math-fundamentalsMath Fundamentals
number-theoryNumber Theory⭐⭐
combinatorics-basicsCombinatorics Basics⭐⭐
catalan-numbersCatalan Numbers⭐⭐
inclusion-exclusionInclusion-Exclusion Principle⭐⭐
extended-euclid-crtExtended Euclidean & CRT⭐⭐
matrix-exponentiationMatrix Exponentiation⭐⭐
game-theory-nimGame Theory — Nim & Sprague-Grundy⭐⭐
probabilityProbability and Expected Value⭐⭐
fft-nttFFT & NTT⭐⭐⭐
gaussian-eliminationGaussian Elimination⭐⭐⭐
linear-basis-xorLinear Basis (XOR Basis)⭐⭐⭐

Suggested Reading Order

  1. math-fundamentals — modular arithmetic, GCD/LCM, fast exponentiation
  2. number-theory — primes, sieves, divisors, multiplicative functions
  3. combinatorics-basics — binomial coefficients, counting principles
  4. catalan-numbers — the ubiquitous counting sequence
  5. inclusion-exclusion — count unions of overlapping sets
  6. extended-euclid-crt — modular inverses and Chinese Remainder Theorem
  7. probability — linearity of expectation and expected value DP
  8. game-theory-nim — Sprague-Grundy theory for combinatorial games
  9. matrix-exponentiation — compute linear recurrences for huge n
  10. fft-ntt — O(n log n) polynomial multiplication
  11. gaussian-elimination — solve linear systems over reals and GF(2)
  12. linear-basis-xor — XOR subset queries via basis vectors

Built for the climb from Codeforces 1100 to 2100.