Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Modular Inverses

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

For a prime modulus, a^(p-2) is the inverse of a โ€” binary exponentiation makes each inverse O(log p).

Dividing mod p

Fermat: a^(p-1) = 1 (mod p) for a not divisible by p, so a^(p-2) is the multiplicative inverse. One binary exponentiation per inverse, or a linear sweep inv[i] = -(p/i) * inv[p mod i] for ALL inverses at once โ€” the trick behind every factorial table.