LeetCode Super Pow(快速求幂算法)
来源:互联网 发布:苹果垃圾桶 知乎 编辑:程序博客网 时间:2024/04/30 06:38
题意:给出底数a,指数b(很大的数,用数组形式给出,每个元素为1位),求其对1337的模
思路:在遍历数据过程中,用ans(n)表示上一次计算结果,则ans(n+1) = pow(ans(n), 10, 1337) * pow(a, b[n+1], 1337) % 1337
其中pow(a, b, mod)为快速求幂算法的实现
具体代码如下:耗时(9ms)
public class Solution{ private int pow(int a, int b, int mod) { int ret = 1; int x = a % mod; while (b != 0) { if ((b & 1) == 1) { ret = ret * x % mod; } x = x * x % mod; b >>= 1; } return ret; } public int superPow(int a, int[] b) { int ans = 1; int mod = 1337; for (int i = 0; i < b.length; i++) { ans = pow(ans, 10, mod) * pow(a, b[i], mod) % mod; } return ans; }}
第二种解法 (使用欧拉函数和快速求幂)
(1) Firstly, if a
has both divisor 7 and 191, that's a % 1337 == 0
, answer is 0.
(2) Then if a
has neither divisor 7 nor 191, that's a and 1337 are coprime, so ab % 1337 = ab % phi(1337) % 1337 = ab % 1140 % 1337.
(3) Finally, a
could has either divisor 7 or 191, that's similar.
Let it be 7 for example.
Let a = 7nx
and let b = 1140p + q, where 0 < q <= 1140
then:
ab % 1337
= ((7nx)b) % 1337
= (7nbxb) % 1337
= ( (7nb % 1337) * (xb % 1337) ) % 1337
= ( (71140np + nq % 1337) * (x1140p + q % 1337) ) % 1337
now note x and 1337 are coprime, so
= ( (71140np + nq % 1337) * (xq % 1337) ) % 1337
= ( 7 * (71140np + nq - 1 % 191) * (xq % 1337) ) % 1337
note 7 and 191 are coprime too, and 1140 is a multiple of 190, where 190 = phi(191). What's more we should assure that q != 0, if b % 1140== 0, then let b = 1140. so
= ( 7 * (7nq - 1 % 191) * (xq % 1337) ) % 1337
= ( (7nq % 1337) * (xq % 1337) ) % 1337
= (7nqxq) % 1337
= ((7nx)q) % 1337
= (aq) % 1337
public class Solution{ private int pow(int a, int b, int mod) { int ret = 1; int x = a % mod; while (b != 0) { if ((b & 1) == 1) { ret = ret * x % mod; } x = x * x % mod; b >>= 1; } return ret; } public int superPow(int a, int[] b) { int mod = 1337; int q = 0; for (int num : b) { q = (q * 10 + num) % mod; } if (q == 0) q = 1140; return pow(a, q, mod); }}
- LeetCode Super Pow(快速求幂算法)
- leetcode 372. Super Pow 快速幂实现
- Leetcode 372. Super Pow 快速幂计算 解题报告
- LeetCode 372. Super Pow (快速幂取模)
- 快速求幂 POW优化
- LeetCode 50. Pow(x, n),求幂算法
- leetcode.372. Super Pow
- LeetCode 372 Super Pow
- LeetCode 372. Super Pow
- LeetCode Super Pow详解
- [LEETCODE] 372. super pow
- leetcode Super Pow
- LeetCode--372. Super Pow
- LeetCode: Super Pow
- leetcode 372. Super Pow
- Leetcode 372. Super Pow
- [leetcode] 372. Super Pow
- Leetcode: 372.Super Pow
- iOS 如何实现顶部图片的在滑动时的伸缩
- Ubuntu下多个版本OpenCV管理(Multiple Opencv version)
- 跟我一起学习MySQL技术内幕(第五版):(第四章学习日记1)
- JavaScript中的一些常用事件
- PHP学习-DAY2-PHP基础2
- LeetCode Super Pow(快速求幂算法)
- jQuery 操作属性
- MyEclipse8.5集成Tomcat7时的启动错误:Exception in thread “main” java.lang.NoClassDefFoundError org/apache/com
- 帝都
- POJ 2758 Checking the Text
- 游标属性
- 泛型(三)之泛型接口与方法
- forlinx ok210 uboot 分析
- 环形队列