快速幂—LintCode
来源:互联网 发布:js获取classname值 编辑:程序博客网 时间:2024/06/06 03:42
描述:
计算a^n % b,其中a,b和n都是32位的整数。
样例:
例如 2^31 % 3 = 2
例如 100^1000 % 1000 = 0
思路:
二分求幂。
ac代码:
class Solution {public: /* * @param a, b, n: 32bit integers * @return: An integer */ int mod(long long a,long long b,long long n) { // write your code here if(b==1) return 0; if(n==0) return 1; if(n==1) return a%b; if(n%2) return mod(a%b,b,n-1)*a%b; else return mod(((a%b)*(a%b)%b),b,n/2); } int fastPower(int a, int b, int n) { mod(a,b,n); }};
阅读全文
0 0
- 快速幂—LintCode
- lintcode刷题——快速幂
- LintCode-快速幂
- Lintcode快速幂
- lintcode-快速幂-125
- LintCode 快速幂
- Lintcode 快速幂
- lintcode之快速幂
- lintcode ----快速幂
- LintCode 140-快速幂
- LintCode :快速幂
- lintcode(140)快速幂
- 快速幂-LintCode
- [LintCode]140.快速幂
- [LintCode] 快速幂 Fast Power
- LintCode-剑指Offer-(140)快速幂
- LintCode刷题链表排序——归并和快速排序
- lintcode——子树
- 几行代码快速集成二维码扫描库
- Spring学习---Spring MVC起步
- 错误提示:The method parseInt(String) is undefined for the type Loginsample
- Lua中的元表与元方法学习总结
- pb11.5调用系统打印机 Function ulong ShellExecute(ulong hwnd,ref string lpOperation,ref string lpFile,ref st
- 快速幂—LintCode
- opencv Python学习笔记(三)调色板加跟随鼠标绘图
- Linux free -m 详细说明
- STM32L0 HAL 串口问题
- 【《Real-Time Rendering 3rd》 提炼总结】(七) 第七章续 · 延迟渲染(Deferred Rendering)的前生今世
- Linux下安装nginx
- springboot启动正常,但出现404错误,导致无法访问
- Final、finally代码块、finalize()三者的区别
- linux连接代理服务器