Skip to main content

πŸ”’ Math

Math problems reward recognizing the structure: modular arithmetic, GCD, primality, combinatorics, or simple geometry. The right formula usually beats any data-structure cleverness.

This category contains 25 problems. Use the patterns below to recognize what's being asked, then jump to the problem list at the bottom.


🧠 Key Patterns​

  • Modular Arithmetic β€” (a + b) % m, modular inverse via Fermat.
  • GCD / LCM β€” Euclidean algorithm. lcm = a / gcd * b.
  • Prime Sieve β€” Sieve of Eratosthenes for primes up to nn in O(nlog⁑log⁑n)O(n \log \log n).
  • Fast Exponentiation β€” Power in O(log⁑n)O(\log n) via repeated squaring.
  • Combinatorics β€” nCrnC_r, Pascal's triangle, stars and bars.
  • Geometry β€” Cross product for orientation, distance formula, line intersection.

⚠️ Common Pitfalls​

  • Integer overflow on multiplication β€” promote to long early.
  • Negative modulo: (-7) % 3 is -1 in C#, not 2. Add m and re-mod.

πŸ“š Study Resources​

πŸ“Ί Videos​

πŸ“– Books​

  • Concrete Mathematics β€” Graham, Knuth, Patashnik β€” The DP-of-math book
  • Competitive Programming Handbook β€” Laaksonen β€” Ch. 21

🌐 Articles & References​


πŸ’» All Math Problems​