Algorithm


Problem Name: 372. Super Pow

Problem Link: https://leetcode.com/problems/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.

 

Code Examples

#1 Code Example with Javascript Programming

Code - Javascript Programming


const superPow = function(a, b) {
   const base = 1337
   function powmod(a, k) {
     a %= base
     let res = 1
     for(let i = 0; i  <  k; i++) res = res * a % base
     return res
   }
   if(b.length === 0) return 1
   const last = b.pop()
   return powmod(superPow(a, b), 10) * powmod(a, last) % base
}; 
Copy The Code & Try With Live Editor

Input

x
+
cmd
a = 2, b = [3]

Output

x
+
cmd
8

#2 Code Example with Python Programming

Code - Python Programming


class Solution:
    def superPow(self, a, b):
        return pow(a, int(''.join(map(str, b))), 1337)
Copy The Code & Try With Live Editor

Input

x
+
cmd
a = 2, b = [3]

Output

x
+
cmd
8
Advertisements

Demonstration


Previous
#371 Leetcode Sum of Two Integers Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#373 Leetcode Find K Pairs with Smallest Sums Solution in C, C++, Java, JavaScript, Python, C# Leetcode