Quantcast
Channel: 书影 - Entries for the tag leetcode
Viewing all articles
Browse latest Browse all 559

[LeetCode]Super Pow

$
0
0

题目描述:

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 ^ b mod 1337的值。a是正整数,b是一个极大的正整数,以数组形式给出。

解题思路:

快速幂

Python代码:

class Solution(object):
    def superPow(self, a, b):
        """
        :type a: int
        :type b: List[int]
        :rtype: int
        """
        ans, pow = 1, a
        for n in b[::-1]:
            ans = (ans * (pow ** n) % 1337) % 1337
            pow = (pow ** 10) % 1337
        return ans

 


Viewing all articles
Browse latest Browse all 559

Trending Articles