lintcode: Fast Power
来源:互联网 发布:js toggle() 编辑:程序博客网 时间:2024/05/16 15:12
Calculate the a^b % b where a, b and n are all 32bit integers.
Example For 231 % 3 = 2
For 1001000 % 1000 = 0
Challenge O(logn)
思路参见我的博客 幂模运算
class Solution {public: /* * @param a, b, n: 32bit integers * @return: An integer */ int fastPower(int a, int b, int n) { // write your code here long long res=1;//用int的话中间会溢出 long long base=a; if(n==0){ return 1%b; } while(n){ int bi=n%2; n/=2; if(bi){ res=(res*base)%b; } base=(base*base)%b; } return res; }};
1 0
- lintcode: Fast Power
- *[Lintcode]Fast Power
- LintCode 150 : Fast Power
- Fast Power--lintcode
- [LintCode] 快速幂 Fast Power
- [刷题]Fast Power
- #140 Fast Power
- Fast Power 解题报告
- 递归----Fast Power Show result
- Lintcode140 Fast Power solution 题解
- 快速幂(Fast Power)
- fast power关于取模运算
- 02分治算法-02fast-power
- lintcode: O(1) Check Power of 2
- *[Lintcode]O(1) Check Power of 2
- 140.Fast Power-快速幂(中等题)
- fast
- FAST
- GDAL应用-gdal_merge.py
- java System.getProperty()用法
- hdu5625 Clarke and chemistry
- 解决vim文件显示乱码问题
- .NET 环境中使用RabbitMQ
- lintcode: Fast Power
- 使用MySQL和Servlet编写Android接口样例
- MySQL INNER JOIN
- DiskLruCache的简单用法 .
- 泰国餐厅网站参考-hsj
- OGR数据模型
- 深入理解Java:注解(Annotation)基本概念
- 矩形嵌套(动态规划)
- Java -Xms -Xmx -Xss -XX:MaxNewSize -XX:MaxPermSize含义记录