Math Fundamentals18 sections · 814 units
Open in Course

Modular Exponentiation

Computing large powers

In competitive programming, you often need to compute xnmodmx^n \bmod m where nn is huge (like 10910^9).

Naive exponentiation would overflow. Even 210002^{1000} has 300+ digits. But using modular arithmetic, you can keep numbers small while computing the correct result modulo mm.

The trick: (a×b)modm=((amodm)×(bmodm))modm(a \times b) \bmod m = ((a \bmod m) \times (b \bmod m)) \bmod m. Apply this after every multiplication to prevent overflow.