[leetcode] 372. Super Pow 解题报告
来源:互联网 发布:linux alias 多条命令 编辑:程序博客网 时间:2024/05/16 18:40
题目链接: 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
思路: 需要用到的数学知识
1. a^b % 1337 = (a%1337)^b % 1337
2. xy % 1337 = ((x%1337) * (y%1337)) % 1337, 其中xy是一个数字如:45, 98等等
其中第一个公式可以用来削减a的值, 第二个公式可以将数组一位位的计算, 比如 12345^678, 首先12345可以先除余1337, 设结果为X, 则原式就可以化为:
X^678 = ((X^670 % 1337) * (X^8 % 1337)) % 1337 = (pow((X^670 % 1337), 10) * (X^8 % 1337)) % 1337
在上面我用了一个pow来化简表示 X^670 = pow(X^670, 10), 当然不是库函数里面pow, 因为会超出界限, 因此我们需要自己在写一个pow来一个个的边乘边除余.
代码如下:
class Solution {public: int superPow(int a, int k) { if(k ==0) return 1; int ans = 1; for(int i = 1; i <= k; i++) ans = (ans*a) %1337; return ans; } int superPow(int a, vector<int>& b) { if(b.empty()) return 1; a = a%1337; int lastBit = b.back(); b.pop_back(); return (superPow(superPow(a, b), 10) * superPow(a, lastBit))%1337; }};参考: https://discuss.leetcode.com/topic/50489/c-clean-and-short-solution/2
1 1
- [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
- 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
- AngularUI Router
- VS2013调用armadillo使用KNN算法实现0-9数字分类
- 实战Apache+Tomcat集群和负载均衡
- iOS开发(OC)——根据url播放音频
- PCH文件的加载与宏定义
- [leetcode] 372. Super Pow 解题报告
- BlockingQueue
- TFS性能测试
- 小技巧,logo使用<img>嵌入
- POJ 1742 Coins
- C++合成默认构造函数的真相
- Android标准架构实例分析之编写最简单的hello驱动
- scala spark hbase 操作案例
- (原创软件)CnCrypt 密码生成工具,提取自CnCrypt的一个独立版本