Skip to content

Modular Arithmetic Hot Sheet

Use this page when one fixed modulus is in play and you need the fastest route back to safe powmod, inverse, or nCk mod prime helpers.

Do Not Use When

  • the real task is factorization, divisors, or gcd structure rather than residues
  • the modulus is composite or changes per query and you have not separated those cases
  • the problem needs a slower proof page more than a retrieval sheet

Choose By Signal

Core Invariants

  • modular division is legal only when the inverse exists
  • the repo mod_inv helper uses Fermat, so its contract is “fixed prime modulus”
  • factorial / inverse-factorial tables are only the clean route when max_n < MOD and many queries justify the precompute

Main Traps

  • using Fermat under a composite modulus
  • forgetting to normalize after subtraction
  • assuming n! is invertible when n >= MOD
  • multiplying large values before % MOD without thinking about overflow
  • treating quadratic-residue extraction like a plain inverse / power helper problem

Exact Starters In This Repo

Reopen Paths