732. Recursive Exponentiation (Fast Power, Modulo 10^9+7)

EasyRecursionRecursion

Given two non-negative integers x and n, compute x^n (x raised to the power n) modulo 10^9 + 7, using fast exponentiation by squaring implemented recursively. By convention, x^0 = 1 for all x (including x = 0). The recursive idea: if n is even, x^n = (x^(n/2))^2; if n is odd, x^n = x * x^(n-1). Use modular arithmetic throughout to keep values bounded.

Input: Two non-negative integers x and n separated by a comma.

Output: Return an integer equal to (x^n) mod (10^9 + 7).

Examples

Example 1
Input: 2, 10
Output: 1024
Explanation: 2^10 = 1024.
Example 2
Input: 3, 0
Output: 1
Explanation: Anything to the 0 power is 1.
Example 3
Input: 5, 3
Output: 125
Explanation: 5*5*5 = 125.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →