Binary exponentiation is an efficient algorithm for computing in time. Instead of multiplying by itself times, it repeatedly squares the base and halves the exponent. This works because every exponent can be represented in binary, so only the powers corresponding to 1 bits need to be multiplied into the answer. Since the exponent is divided by at every step, the number of operations grows logarithmically rather than linearly.
For example,
Hence,
Thus, binary exponentiation only multiplies the powers corresponding to the set (1) bits in the binary representation of the exponent.
Implementation
long long binpow(long long a, long long n) {
long long res = 1;
while (n > 0) {
if (n & 1) res *= a;
a *= a;
n >>= 1;
}
return res;
}For modular exponentiation, apply % mod after every multiplication to keep values bounded:
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
a %= mod;
while (n > 0) {
if (n & 1) res = (res * a) % mod;
a = (a * a) % mod;
n >>= 1;
}
return res;
}