DEV Community

Abhishek Chaudhary
Abhishek Chaudhary

Posted on

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:

class Solution:
    def pow(self, a, b, n):
        curr = 1
        for i in range(b):
            curr = ((curr % n) * (a % n)) % n
        return curr

    def superPow(self, a: int, b: List[int]) -> int:
        n = 1337
        if len(b) > 0:
            return (self.pow(a, b[-1], n) * self.pow(self.superPow(a, b[:-1]), 10, n)) % n
        return 1
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
thomasbnt profile image
Thomas Bnt ☕

Hello ! Don't hesitate to put colors on your codeblock like this example for have to have a better understanding of your code 😎

console.log('Hello world!');
Enter fullscreen mode Exit fullscreen mode

Example of how to add colors and syntax in codeblocks