问题描述(来源于LeetCode)

你的任务是计算 a^b 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出。

代码实现

1
2
3
4
5
6
7
8
9
class Solution:
def superPow(self, a: int, b: List[int]) -> int:
mod = 1337
a_mod = a % mod
powers = [pow(a_mod, i, mod) for i in range(10)]
res = 1
for digit in b:
res = (pow(res, 10, mod) * powers[digit]) % mod
return res

复杂度分析

  • 时间复杂度:O(NLogN)O(NLogN)
  • 空间复杂度:O(N)O(N)