这篇文章是程序自动发表的,详情可以见
这里
这是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
思路 这道题显然不能直接计算,可以用欧拉定理 对任意正整数a,正整数m,(a,m) = 1,ƒ(m) 为比m小的与m互质的(注意不一定是质数)正整数的个数, 则 aƒ(m) ≡ 1 (mod m) 。 再利用性质: a ≡ b (mod m) ,c ≡ d (mod m) ,则ac ≡ bd (mod m)
证明就不写了,打数学符号太累了(T^T),给个传送门吧--> 欧拉定理
show me the code
class Solution(object): def superPow(self, a, b): """ :type a: int :type b: List[int] :rtype: int """ m = 1337 if a % m == 0: return 0 sum = 0 for i in b: sum = 10 * sum i phi = 0 for i in range(1,m): if (i % 7 != 0) and (i % 191 != 0): phi = 1 sum = sum % phi return (a**sum) % 1337 # if m a prime num ,use the small law of Fermat # else use the law of Euler