Leetcode 372. 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.

Example1:

a = 2
b = [3]

Result: 8

Example2:

a = 2
b = [1,0]

Result: 1024

 

使用公式 c = ab  =>  c mod d = [a mod d * b mod d] mod d

所以a^423 mod d = (a^100)^4 * (a ^10)^2 * a^3

 

 1 class Solution(object):
 2     def superPow(self, a, b):
 3         """
 4         :type a: int
 5         :type b: List[int]
 6         :rtype: int
 7         """
 8         ans = 1
 9         mod = 1337
10         for i in b[::-1]:
11             ans = ans * a ** i % mod
12             a = a ** 10 % mod
13         return ans % mod

 



posted @ 2016-12-13 13:59  lettuan  阅读(92)  评论(0编辑  收藏  举报