美文网首页
「算法」372. 超级次方

「算法」372. 超级次方

作者: MrLiuYS | 来源:发表于2021-12-05 10:23 被阅读0次

<div class="image-package"><img src="https://img.haomeiwen.com/i1648392/6a130d84adc668bc.jpg" contenteditable="false" img-data="{"format":"jpeg","size":207264,"height":900,"width":1600}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><blockquote><p>你的任务是计算 ab 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出。

示例 1:

输入:a = 2, b = [3]
输出:8
示例 2:

输入:a = 2, b = [1,0]
输出:1024
示例 3:

输入:a = 1, b = [4,3,3,8,5,2]
输出:1
示例 4:

输入:a = 2147483647, b = [2,0,0]
输出:1198

提示:

1 <= a <= 231 - 1
1 <= b.length <= 2000
0 <= b[i] <= 9
b 不含前导 0

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/super-pow
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。</p></blockquote><h1 id="nu9ff">题解</h1><div class="image-package"><img src="https://img.haomeiwen.com/i1648392/d54a224451c92389.jpg" contenteditable="false" img-data="{"format":"jpeg","size":67200,"height":832,"width":1304}" class="uploaded-img" style="min-height:200px;min-width:200px;" width="auto" height="auto"/>
</div><h2 id="xai6y">Swift</h2><blockquote><p>class Solution {
let mod = 1337

func superPow(_ a: Int, _ b: [Int]) -> Int {
var a = a
var ans = 1

for index in (0 ..< b.count).reversed() {
ans = Int(ans * pow(a, b[index]) % mod)
a = pow(a, 10)
}

return ans
}

func pow(_ x: Int, _ n: Int) -> Int {
var x = x
var n = n
var res = 1
while n != 0 {
if n % 2 != 0 {
res = (res * x % mod)
}
x = (x * x % mod)
n /= 2
}
return res
}
}

print(Solution().superPow(2147483647, [2, 0, 0]))
</p></blockquote><p>
</p><p>
</p><p>
</p>

相关文章

  • 「算法」372. 超级次方

    你的任务是计算 ab 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出。示例 1:...

  • 如何高效进行模幂运算

    读完本文,你可以去力扣拿下如下题目: 372.超级次方[https://leetcode-cn.com/probl...

  • Leetcode【372、1131】

    问题描述:【Math】372. Super Pow 解题思路: 快速幂算法。计算 a^b mod 1337,a 是...

  • 超级次方

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/super-...

  • 幂次方算法pow

    自定义的递归算法,代码要求的时间与空间复杂度较高,不适合嵌入式设备进行快速计算。代码由C++实现。 迭代算法,复杂...

  • LZW压缩算法

    参考链接:超级简单的数据压缩算法—LZW算法压缩算法——lzw算法实现LZW算法 LZW 压缩算法正确图解

  • [Math_Medium] 372. Super Pow 201

    原题:372. Super Pow Your task is to calculate ab mod 1337 w...

  • 如何养成一个好习惯

    你相信吗?你就是你习惯的产物。 这里边有一个特别心灵鸡汤的算法,1.01的365次方,和0.99的365次方差多少...

  • RSA-Pycrypto

    RSA 密码算法与签名RSA是一种公钥密码算法,RSA的密文是对代码明文的数字的 E 次方求mod N 的结果。也...

  • 2019-06-19

    一致性hash算法 首先,我们将hash算法的值域映射成一个具有232 次方个桶的空间中,即0~(232)-1的数...

网友评论

      本文标题:「算法」372. 超级次方

      本文链接:https://www.haomeiwen.com/subject/kgeixrtx.html