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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年05月06日 11时01分57秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
tf keras SimpleRNN源码解析
2019-04-30
MyBatisPlus简单入门(SpringBoot)
2019-04-30
攻防世界web进阶区web2详解
2019-04-30
xss-labs详解(上)1-10
2019-04-30
xss-labs详解(下)11-20
2019-04-30
攻防世界web进阶区ics-04详解
2019-04-30
Linux png转jpg (convert命令)
2019-04-30
vscode git
2019-04-30
CodeForces - 456C Boredom (dp)
2019-04-30
CodeForces - 1042B Vitamins (思维)
2019-04-30
ACM 2013 长沙区域赛 Collision (几何)
2019-04-30
ACM 2014 鞍山区域赛 E - Hatsune Miku (dp)
2019-04-30
反向传播&梯度下降 的直观理解程序(numpy)
2019-04-30
ACM 2017 北京区域赛 J-Pangu and Stones(区间dp)
2019-04-30
java常用类 String面试题
2019-04-30
四线触摸屏原理
2019-04-30
C/C++如何返回一个数组/指针
2019-04-30
腾讯AI语音识别API踩坑记录
2019-04-30
java.net.BindException: 无法指定被请求的地址
2019-05-01