Math Fundamentals18 sections · 814 units
Open in Course

Problem - Power Modulo

Fast exponentiation with mod

Compute xn mod mx^n \bmod m where x,n,mx, n, m can be up to 10910^9.

For example, 210 mod 1000=1024 mod 1000=242^{10} \bmod 1000 = 1024 \bmod 1000 = 24.

You'll use exponentiation by squaring, but take the modulo after every multiplication to keep intermediate results small.