Math-372-Super Pow
2018-01-30 12:16
323 查看
Description:
Your task is to calculate abab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
Example2:
Best Solution:
Your task is to calculate abab 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
Best Solution:
public class Solution { private static final int M = 1337; public int normalPow(int a, int b) { int result = 1; while (b != 0) { if (b % 2 != 0) result = result * a % M; a = a * a % M; b /= 2; } return result; } public int superPow(int a, int[] b) { a %= M; int result = 1; for (int i = b.length - 1; i >= 0; i--) { result = result * normalPow(a, b[i]) % M; a = normalPow(a, 10); } return result; } }
相关文章推荐
- [leetcode-372]Super Pow(java)
- LeetCode 372 Super Pow
- 372. Super Pow(LeetCode)
- LeetCode 372. Super Pow (快速幂取模)
- [java][leetcode 372]Super Pow
- LeetCode 第 372 题 (Super Pow)
- LeetCode 第 372 题 (Super Pow)
- Leetcode 372. Super Pow (Medium) (cpp)
- Leetcode (372) Super Pow
- LeetCode 372 Super Pow
- math.pow 和 Math.sqrt函数
- Super Pow
- Python中numpy.clip();numpy.fabs()的用法;以及math.pow()的说明
- Math.pow
- JS中的Math.pow(a,b)方法
- 372. Super Pow
- 与Math.pow 相反的函数使用介绍
- Javascript四舍五入(Math.round()与Math.pow())
- java基础(八)Math/代码块/继承成员方法指南的关系/继承中成员变量之间的关系/方法的重写/继承中构造方法之间的关系/this和super的区别
- Javascript四舍五入(Math.round()与Math.pow())