-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
问题
你的任务是计算 ab 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出。
示例 1:
输入: a = 2, b = [3]
输出: 8
示例 2:
输入: a = 2, b = [1,0]
输出: 1024
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/super-pow
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
求解
弄清楚三个问题:
1.如何处理用数组表示的指数?
2.是如何得到求模之后的结果?
3.是如何高效进行幂运算?
作者:labuladong
链接:https://leetcode-cn.com/problems/super-pow/solution/you-qian-ru-shen-kuai-su-mi-suan-fa-xiang-jie-by-l/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class Solution {
private:
const int mnum = 1337;
int mypow(int a,int b){
int res=1;
a %= mnum;
for(int i=0;i<b;++i){
res *= a;
res %= mnum;
}
return res;
}
public:
int superPow(int a, vector<int>& b) {
if(b.empty())return 1;
int last = b.back();
int part1 = mypow(a,last);
b.pop_back();
int part2 = mypow(superPow(a,b),10);
return (part1*part2)%mnum;
}
};
Metadata
Metadata
Assignees
Labels
No labels