Super Pow — LeetCode 372 Python Solution
MediumMathDivide and Conquer
- Problem
- #372
- Pattern
- Math and Number Theory
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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
- Input
- a = 2, b = [3]
- Output
- 8
Python solution
Python
class Solution:
def superPow(self, a: int, b: List[int]) -> int:
mod = 1337
ans = 1
for e in b[::-1]:
ans = ans * pow(a, e, mod) % mod
a = pow(a, 10, mod)
return ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n) or O(1) |
| Space | O(1) auxiliary |
Pattern: Math and Number Theory
Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 372. Super Pow is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.
The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 372. Super Pow?
- LeetCode 372. Super Pow is rated Medium on LeetCode.
- What topics does LeetCode 372. Super Pow cover?
- LeetCode 372. Super Pow is tagged Math and Divide and Conquer on LeetCode.