[LEETCODE] 372. super pow
来源:互联网 发布:php ftp上传文件 编辑:程序博客网 时间:2024/04/30 00:39
【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)%c=(a%c)*(b%c)
和 把 a^b 转化成 (a ^ (b/2))^2
不过要注意b 是奇数的情况,就是这时就要写成 b/2 和 b - b/2。
class Solution {public: int superPow(int a, vector<int>& b) { a = a % 1337; long long res = 1; for (int i = 0; i < b.size(); i++) { res = (pow(res, 10) % 1337) * (pow(a, b[i]) % 1337) % 1337; } return res; } int pow(int a, int n) { if (n == 0) return 1; if (n == 1) return a % 1337; else return pow(a, n / 2) * pow(a, n - n / 2) % 1337; }};
0 0
- leetcode.372. Super Pow
- LeetCode 372. Super Pow
- [LEETCODE] 372. super pow
- LeetCode--372. Super Pow
- leetcode 372. Super Pow
- Leetcode 372. Super Pow
- [leetcode] 372. Super Pow
- Leetcode: 372.Super Pow
- 372. Super Pow(LeetCode)
- LeetCode 372. Super Pow
- [LeetCode]372. Super Pow
- [leetcode]372. Super Pow
- leetcode 372. Super Pow
- LeetCode—372. Super Pow
- Leetcode 372. Super Pow[medium]
- [leetcode] 372. Super Pow 解题报告
- 【leetcode】372. Super Pow【M】【40】
- leetcode 336. Palindrome Pairs 372. Super Pow
- 坑爹的Oracle安装
- Windows下怎么练习shell脚本
- web重点2
- C语言中的关键字
- 致力于打造最详细的Requests使用(不定期补充)
- [LEETCODE] 372. super pow
- 点点滴滴
- macOS 中更改默认应用的方法
- 名人生卒年
- leetcode 12 Integer to Roman(整型数转换为罗马数字)
- java学习之路 之 基本语法-程序流程控制-(if-else)语句练习题
- 深入理解HTTP Session
- 石人眼 :执政党的目标不就是分给农民土地吗?
- Matlab bsxfun函数