LeetCode C++ 372. Super Pow【Math】中等
发布日期:2021-07-01 02:57:11 浏览次数:3 分类:技术文章

本文共 811 字,大约阅读时间需要 2 分钟。

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% 的用户

转载地址:https://memcpy0.blog.csdn.net/article/details/110154274 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode C++ 面试题 04.05. Legal Binary Search Tree LCCI【Binary Search Tree/DFS】中等
下一篇:LeetCode C++ 剑指 Offer 16. 数值的整数次方【Math/Recursion】中等

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年05月06日 11时01分57秒