Description
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 <= 2^31 - 1
1 <= b.length <= 2000
0 <= b[i] <= 9
b does not contain leading zeros.
Solution
Solved after help.
Because:
7
1234567
=
7
1234560
∗
7
7
=
7
123456
∗
10
∗
7
7
7^{1234567}=7^{1234560}*7^{7}=7^{123456*10}* 7^{7}
71234567=71234560∗77=7123456∗10∗77
So we can start from left, and every time after getting the number, get the power of 10
of it.
Time complexity:
o
(
n
)
o(n)
o(n), where n
is the length of b
Space complexity:
o
(
1
)
o(1)
o(1)
Code
class Solution:
def superPow(self, a: int, b: List[int]) -> int:
res = 1
mod_val = 1337
for i in range(len(b)):
res = res ** 10 * (a ** b[i])
res %= mod_val
return res