LeetCode 372. Super Pow
来源:互联网 发布:英孚网络课程网址 编辑:程序博客网 时间:2024/05/16 15:24
原题网址:https://leetcode.com/problems/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 = 2b = [3]Result: 8
Example2:
a = 2b = [1,0]Result: 1024
方法:分析模的特点,若a % 1337 = n * 1337 + m,b % 1337 = p * 1337 + q,则a*b % 1337 = m * q % 1337
public class Solution { private int[] mod; private int mod(int a, int p) { if (mod[p] > 0) return mod[p]; if (p == 0) { mod[p] = a % 1337; return mod[p]; } int m = 1; for(int i = 0; i < 10; i++) { m *= mod(a, p - 1); m %= 1337; } mod[p] = m; return mod[p]; } public int superPow(int a, int[] b) { if (a % 191 == 0 && a % 7 == 0) return 0; mod = new int[b.length]; int m = 1; for(int i = 0; i < b.length; i++) { for(int j = 1; j <= b[i]; j++) { m *= mod(a, b.length - 1 - i); m %= 1337; } } return m; }}
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
- C语言-预编译
- Android项目目录结构res和assests目录的不同:
- 最小费最大流,拆点法(海军上将 uva 1658)
- Haproxy+keepalived实现高可用负载均衡
- CSS让图片垂直居中的几种技巧
- LeetCode 372. Super Pow
- C语言 可变参数函数总结
- C++list的用法
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- Leetcode Recover Binary Search Tree
- 新的开始
- 四层和七层负载均衡的区别
- 推荐系统2——JDBC连接
- 利用TortoiseGit(小乌龟)将项目上传至GitHub网站