![]() |
VOOZH | about |
In competitive programming, we often need to do a lot of big number calculations fast. Binary exponentiation is like a super shortcut for doing powers and can make programs faster. This article will show you how to use this powerful trick to enhance your coding skills.
Binary Exponentiation or Exponentiation by squaring is the process of calculating a number raised to the power another number (AB) in Logarithmic time of the exponent or power, which speeds up the execution time of the program.
Whenever we need to calculate (AB), we can simple calculate the result by taking the result as 1 and multiplying A for exactly B times. The time complexity for this approach is O(B) and will fail when values of B in order of 108 or greater. This is when we can use Binary exponentiation because it can calculate the result in O(log(B)) time complexity, so we can easily calculate the results for larger values of B in order of 1018 or less.
When we are calculating (AB), we can have 3 possible positive values of B:
Examples:
2 12 = (2) 2 * 6
= (4) 6
= (4) 2 * 3
= (16) 3
= 16 * (16) 2
= 16 * (256) 1
4096
4096
Even though, both the iterative and recursive approach have the same time complexity, the iterative approach is still faster because there are no overheads for recursive calls.
We can compute Nth Fibonacci Number by simply running a loop till N and in every iteration i, we calculate the ith Fibonacci number using (i-1)th and (i-2)th iteration. But this approach runs in linear time complexity, that is O(N). But, if we are concerned with simply the Nth Fibonacci number and not every number before it, then we can compute it in O(logN) by using matrix exponentiation, where we build a 2⨯2 matrix to transition from (i-2)th and (i-1)th Fibonacci number to (i-1)th and ith Fibonacci number.
Click here to expand your knowledge about Matrix Exponentiation
It is hardly to see any problem which is all about to compute AB, but Binary Exponentiation comes in handy when our answer becomes too large to be stored as an integer, that is greater than INT_MAX. There are many problems which asks us to count the number of ways to do something and as the number of ways are too large to be stored in an Integer variable, the question asks us to print the answer modulo 1000000007 or modulo 998244353. Since, it is proved here, that
(A * B) mod M = ((A mod M) * (B mod M)) mod M
Therefore, Binary Exponentiation is very useful in computing AB mod M.
46480318
Let's suppose, we are given a Permutation P and a Sequence S and we need to apply P to S for a large number of times (say K), then we can easily compute the final sequence by using Binary Exponentiation.
Examples:
P = [2, 3, 4, 1], S = [2, 1, 3, 4]
After applying permutation P to Sequence S once,
S' = [1, 3, 4, 2]
Explanation:
S'[1] = S[P[1]] = S[2] = 1
S'[2] = S[P[2]] = S[3] = 3
S'[3] = S[P[3]] = S[4] = 4
S'[4] = S[P[4]] = S[1] = 2After applying permutation P to Sequence S twice,
S'' = [3, 4, 2, 1]
Explanation:
S''[1] = S'[P[1]] = S'[2] = S[P[2]] = S[3] = 3
S''[2] = S'[P[2]] = S'[3] = S[P[3]] = S[4] = 4
S''[3] = S'[P[3]] = S'[4] = S[P[4]] = S[1] = 2
S''[4] = S'[P[4]] = S'[1] = S[P[1]] = S[2] = 1
Observations: If we observe carefully, in the above example instead of applying permutation P to S twice, if we apply permutation P in itself (P') and then apply it on S once, we will get the same result.
P = [2, 3, 4, 1], S = [2, 1, 3, 4]
After applying permutation P to itself once,
P' = [3, 4, 1, 2]
Explanation:
P'[1] = P[P[1]] = P[2] = 3
P'[2] = P[P[2]] = P[3] = 4
P'[3] = P[P[3]] = P[4] = 1
P'[4] = P[P[4]] = P[1] = 2Now, applying permutation P' to S,
S''[1] = S[P'[1]] = S[3] = 3
S''[2] = S[P'[2]] = S[4] = 4
S''[3] = S[P'[3]] = S[1] = 2
S''[4] = S[P'[4]] = S[2] = 1
Therefore, it is clear that applying a permutation P to a sequence S for N times is equal to applying permutation P' to sequence S for N/2 times and we can simply solve this using Binary Exponentiation:
New Sequence = 3 4 2 1
Complexity Analysis:
If we have 2 very large number A and B and we need no compute (A * B) mod M but (A * B) in 64-bit integer, then we can use the concept of binary exponentiation to compute the product of these 2 numbers by adding A to the answer B times.
When we are calculating (A * B), we can have 3 possible positive values of B:
Case 1: If B = 0, whatever be the value of A, our result will be 0.
Case 2: If B is an even number, then instead of calculating (A * B) mod M , we can calculate (((A * 2) mod M)* B/2) mod M and the result will be same.
Case 3: If B is an odd number, then instead of calculating (A * B), we can calculate (A + A * (B-1)) mod M, which is same as case 2.
We can recursively follow the above steps to get our result.
Examples:
(25 * 10) mod 60 = ((25 * 2) mod 60 * 5) mod 60
= ((50 mod 60) * 5) mod 60
= (50 + (50 mod 60) * 4) mod 60
= (50 + (100 mod 60) * 2) mod 60
= (50 + (40 mod 60) * 2) mod 60
= (50 + (80 mod 60)) mod 60
= (50 + 20) mod 60
= 10
Therefore, we can compute (A * B) mod M using Binary Exponentiation:
49
Complexity Analysis:
Easy Level Problems on Binary Exponentiation:
Problem | Problem Link |
|---|---|
Medium Level Problems on Binary Exponentiation:
Problem | Problem Link |
|---|---|
Left Most Divisor | |
Rahul and the Lift |
Hard Level Problems on Binary Exponentiation:
Problem | Problem Link |
|---|---|
Related Article: