Editorial for An Exponentiation Problem

Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Ninjaclasher

There were multiple ways to solve this.

In Java or Python, there exists a standard library method that we could use to solve this exact problem.

In Python, this is:

pow(base, exponent, modulo)

In Java, this is:

BigInteger.modPow(exponent, modulo);

If you are not using the aforementioned languages, such as C/C++, you needed to code a power function. However, notice that M \le 10^9, meaning that simply multiplying M times is too slow, and will TLE. We can use a method called Exponentiation by squaring to reduce the time complexity to a sub-linear one instead of a linear one.

Time Complexity: \mathcal{O}(\log M)


There are no comments at the moment.