Math Fundamentals18 sections · 814 units
Open in Course

Problem - Power Modulo

Fast exponentiation with mod

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

For example, 210mod1000=1024mod1000=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.