In competitive programming, we often need to calculate large powers like xy, where y can be as large as 109 or more.
- Using a simple loop takes O(y) time, which is inefficient.
- Binary Exponentiation (also called Fast Exponentiation or Exponentiation by Squaring) reduces this to O(logy) by using the binary representation of the exponent.

Binary Exponentiation
Binary exponentiation efficiently computes xy in O(logy) instead of naive O(y).
- Reduces the number of multiplications by exploiting the binary form of the exponent.
- Works by squaring the base and multiplying the result only when the corresponding bit in y is set to 1
you can read about the iterative implementation here: Iterative function for pow(x, y) and about recurcive implementation here Write program to calculate pow(b, e).
Use Cases of Binary Exponentiation in Competitive Programming
1. Fast Computation of Nth Fibonacci Number
Computing the Nth Fibonacci number using a simple loop takes O(N) time. However, if we only need the Nth number, we can do it in O(log N) using matrix exponentiation:
- Construct a 2×2 matrix representing the Fibonacci transition.
- Raise it to the power N using binary exponentiation.
- Multiply with the base vector to get the Nth Fibonacci number.
This reduces time complexity from O(N) to O(log N).
2. Compute Power Modulo M
To compute xy mod M efficiently when y is very large, binary exponentiation is used.
- Directly calculating xy can exceed integer limits, but by representing y in binary, we can multiply the result only for bits sets.
- This reduces the number of multiplications to O(logB) and keeps numbers small, preventing overflow.
You can read more about it here: Modular Exponentiation (Power in Modular Arithmetic).
3. Apply Permutation of a given Sequence large number of times
Applying a permutation P to a sequence S multiple times can be optimized using binary exponentiation.
- Instead of applying P repeatedly K times, we treat it like exponentiation (Pk).
- If K is odd, we apply the permutation to the sequence, and at each step, we square the permutation itself (P=P∘P) to represent applying it twice.
- Then, we divide K by 2 and continue the process.
This approach significantly reduces the time complexity from O(N⋅K) to O(N logK). You can read more about it here: Apply Given Permutation on Array K times.
4. Compute Product of 2 very Large Numbers Modulo M
When computing the product of two very large numbers x and y modulo M, direct multiplication may overflow a 64-bit integer. Using the binary exponentiation approach, we can compute it safely by repeatedly adding and doubling:
- Case 1: If B=0, the result is 0.
- Case 2: If B is even, compute ((A×2)mod M)×(B/2)mod M
- Case 3: If B is odd, compute A+(A×(B−1))mod M, which reduces to Case 2.
By recursively applying these steps, we can compute (A×B)mod M efficiently without overflow.
You can read more about it here: Multiply Large Integers Under Large Modulo
Practice Problems
Easy
- Padovan Sequence
- Count ways to express N as sum of 1, 3, 4
- Modified Fibonacci
- Geometric Progression
- Carol Numbers
Medium
Hard
Related Article: