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
Example 4:
Input: a = 2147483647, b = [2,0,0]
Output: 1198
Constraints:
1 <= a <= 231 - 1
1 <= b.length <= 2000
0 <= b[i] <= 9
b
doesn't contain leading zeros.
题意:计算 a b a^b ab 对 1337 1337 1337 取模, a a a 是一个正整数, b b b 是一个非常大的正整数且会以数组形式给出。
解法 Python
这一题必须用到数论的知识才能够解决。不过好在,我们有无限精度的Python,使用其中的快速幂函数 pow
:
class Solution:
def superPow(self, a: int, b: List[int]) -> int:
return pow(a, int(''.join([str(i) for i in b])), 1337)
代码的执行效率如下:
执行用时:48 ms, 在所有 Python3 提交中击败了99.58% 的用户
内存消耗:13.6 MB, 在所有 Python3 提交中击败了61.30% 的用户