{x}
blog image

Super Pow

Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.

 

Example 1:

Input: a = 2, b = [3]
Output: 8

Example 2:

Input: a = 2, b = [1,0]
Output: 1024

Example 3:

Input: a = 1, b = [4,3,3,8,5,2]
Output: 1

 

Constraints:

  • 1 <= a <= 231 - 1
  • 1 <= b.length <= 2000
  • 0 <= b[i] <= 9
  • b does not contain leading zeros.

Solution Explanation for Super Pow

The problem asks to compute (ab) mod 1337, where 'a' is a positive integer and 'b' is a large integer represented as an array of digits. A naive approach of calculating ab directly would lead to integer overflow, even with large integer libraries. The solution leverages modular arithmetic and exponentiation by squaring to efficiently solve this problem.

Core Idea:

The solution employs the following key ideas:

  1. Modular Exponentiation: Instead of calculating ab directly, we compute (ab) mod 1337 at each step. This prevents integer overflow by keeping the intermediate results within a manageable range.

  2. Exponentiation by Squaring: This technique significantly reduces the number of multiplications needed to compute ab. It's based on the observation that:

    • a2k = (ak)2
    • a2k+1 = a * (ak)2
  3. Iterating through digits of b: We process the digits of 'b' from right to left (least significant to most significant). For each digit, we compute the contribution of that digit to the final result using modular exponentiation.

  4. Updating 'a': After processing each digit, we update 'a' to a10 mod 1337. This is because the next digit represents a power of 10 greater than the current one.

Algorithm:

  1. Initialize ans to 1.
  2. Iterate through the digits of b from right to left.
  3. For each digit e:
    • Update ans to (ans * ae mod 1337) mod 1337. This incorporates the contribution of the current digit.
    • Update a to a10 mod 1337. This prepares 'a' for the next digit (higher power of 10).
  4. Return ans.

Time Complexity Analysis:

  • The main loop iterates through the digits of b, which has a length of O(log10 b).

  • The qpow (or equivalent modular exponentiation function) takes O(log n) time, where n is the exponent. In our case, n is either a single digit (0-9) or 10, both of which are constants. Therefore, the time complexity of qpow is O(1) in the context of this algorithm.

  • Overall, the time complexity of the superPow function is dominated by the loop iterating through the digits of b. Hence, the time complexity is O(log10 b). This is efficient, as it avoids the exponential time complexity of a naive approach.

Space Complexity Analysis:

The space complexity is O(1) because we only use a constant number of variables to store intermediate results.

Code Examples (Python, Java, C++, Go, TypeScript):

The provided code snippets in multiple languages demonstrate the algorithm efficiently. Each language's implementation will have minor syntactic differences but follow the same core algorithmic steps. Key functions like qpow (or its equivalent) are responsible for modular exponentiation using the exponentiation-by-squaring approach. The main loop processes the digits of b as described above.