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
分析

根据公式 ① ② ab mod 1337 可以优化为 (a mod 1337)b mod 1337 ,所以每次计算底数的时候,计算之前和之后,都进行mod,预防结果超过整数范围。
根据公式 ③ ,可以根据题意对指数进行分解,本题的进制为10.
根据公式 ④,对计算过程进行分解,每一步都是计算
,由于ai知道,所以每步的计算其实是计算底数。
代码
int mod = ;
int superPow(int a, vector<int>& b) {
int answer = ,n = b.size();
if( n == )
return ;
for(int i = n - ;i >= ; i--)
{
if( b[i] > )
answer = answer * Inpow(a,b[i]) % mod;
a = Inpow(a,);
}
return answer;
} int Inpow(int base,int exp){
int result = ;
base = base%mod; for(int i = exp; i > ;i = i >> )
{
if( i & == )
result = result * base %mod;
base = base * base % mod;
} return result % mod; }
372. Super Pow的更多相关文章
- leetcode 50. Pow(x, n) 、372. Super Pow
50. Pow(x, n) 372. Super Pow https://www.cnblogs.com/grandyang/p/5651982.html https://www.jianshu.co ...
- 372 Super Pow 超级次方
你的任务是计算 ab 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出.示例 1:a = 2b = [3]结果: 8示例 2:a = 2b = [1,0]结果: 102 ...
- LeetCode——372. Super Pow
题目链接:https://leetcode.com/problems/super-pow/description/ Your task is to calculate ab mod 1337 wher ...
- 【LeetCode】372. Super Pow 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/super-po ...
- Leetcode 372. Super Pow
使用公式 c = ab => c mod d = [a mod d * b mod d] mod d 所以a^423 mod d = (a^100)^4 * (a ^10)^2 * a^3 ...
- 372. Super Pow.txt
▶ 指数取模运算 ab % m ▶ 参考维基 https://en.wikipedia.org/wiki/Modular_exponentiation,给了几种计算方法:暴力计算法,保存中间结果法(分 ...
- [LeetCode] Super Pow 超级次方
Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large posi ...
- [Swift]LeetCode372. 超级次方 | Super Pow
Your task is to calculate ab mod 1337 where a is a positive integer and bis an extremely large posit ...
- leetcode Super Pow
题目描述: superPow(int a, int[] b),b是一个int数组,每个元素都是正的个位数,组合起来表示一个正整数,例如b=[1,2,3]表示123,求解a^b mod 1337. 思路 ...
随机推荐
- JSP和Servlet的中文乱码处理
JSP和Servlet的中文乱码处理 前几天学习了JSP和Servlet中有关中文乱码的一些问题,写成了博客,今天进行更新一下.应该是可以解决日常的乱码问题了.现在作以下总结希望对需要的人有所帮助.我 ...
- POJ Minimum Cut
Minimum Cut Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 9302 Accepted: 3902 Case ...
- jquery使用案例
表单验证 Dom实现表单验证 通过在form标签的submit上绑定一个onclick事件,用户点击事,触发这个事件,执行Checkvalid()函数进行对表单中的元素值验证,验证通过之后,继续让su ...
- Javascript的原型链图
90%的前端或者js程序员或者老师们对Javascript懂得不比这个多 给手机看的 但是这个图里的所有褐色单向箭头链就是Javascript的原型链(颜色标注对理解js原型链很关键) 这图中的各个_ ...
- ZOJ3944 People Counting ZOJ3939 The Lucky Week (模拟)
ZOJ3944 People Counting ZOJ3939 The Lucky Week 1.PeopleConting 题意:照片上有很多个人,用矩阵里的字符表示.一个人如下: .O. /|\ ...
- 【IDEA 2016】intellij idea tomcat jsp 热部署
刚开始用IDEA,落伍的我,只是觉得IDEA好看.可以换界面.想法如此的low. 真是不太会用啊,弄好了tomcat.程序启动竟然改动一下就要重启,JSP页面也一样. IDEA可以配置热部署,打开to ...
- linux 命令 之判断表达式
摘自http://www.comptechdoc.org/os/linux/usersguide/linux_ugshellpro.html Tests There is a function pro ...
- vs2015全新nuget使用全析
随着vs2015正式版本的发布,实在按捺不住,安装体验了一把,感触最大的除了可以调试lamda以外莫过于对nuget的升级. 先来一张全景: 怎么样,风格完全不同了吧.可以显示预发行版本了,右上角那个 ...
- C#小方法PadLeft 和 PadRight
1.在 C# 中可以对字符串使用 PadLeft 和 PadRight 进行轻松地补位. PadLeft(int totalWidth, char paddingChar) //在字符串左边用 pad ...
- flex实验总结
1.父元素 .box{ display:flex; flex-direction: column;//铺满垂直排列 flex-direction: column-reverse;//铺满垂直反向排列 ...