Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Combining Congruences

โญโญโญ advancedโณ 30 min read๐Ÿ“ Lesson 171 of 281

Euler's phi via a sieve, prefix sums for range answers, and CRT: x = a + n*t where t = (b - a) * n^(-1) (mod m).

Two clocks, one answer

phi(k) counts units mod k; the sieve subtracts phi[i]/p at each prime in one linear pass. CRT reuses the inverse: solving x = a (mod n), x = b (mod m) sets x = a + n*t and solves for t modulo m. Composite moduli need the general egcd inverse โ€” same idea, one more step.

โšก Now practice

Ready to Code
Number TrioModular inverses under load, factorial binomials at n = 1e6, and totient prefix sums.
3 challenges ยท ยท ~110 min