Lintcode: Fast Power 解题报告
Fast Power
原题链接:http://lintcode.com/en/problem/fast-power/#
Calculate the an % b where a, b and n are all 32bit integers.
For 231 % 3 = 2
For 1001000 % 1000 = 0
O(logn)
Tags Expand

SOLUTION 1:
实际上这题应该是suppose n > 0的。
我们利用 取模运算的乘法法则: http://baike.baidu.com/view/4887065.htm
(a * b) % p = (a % p * b % p) % p (3)
将 a^n % b 分解为 (a^(n/2) * a^(n/2) * (a)) %b = ((a^(n/2) * a^(n/2))%b * (a)%b) %b = ((a^(n/2)%b * a^(n/2)%b)%b * (a)%b) %b
实现如下:
注意2个base case: n = 0 n = 1都要特别处理。因为n = 1时,会分解出一个pow(a, b, 1),这个会不断循环调用。
class Solution {
/*
* @param a, b, n: 32bit integers
* @return: An integer
*/
/*
* @param a, b, n: 32bit integers
* @return: An integer
*/
public static int fastPower(int a, int b, int n) {
// write your code here
long ret = pow(a, b, n);
return (int) ret;
}
// suppose n > 0
public static long pow(int a, int b, int n) {
if (a == 0) {
return 0;
}
// The base case.
if (n == 0) {
return 1 % b;
}
if (n == 1) {
return a % b;
}
long ret = 0;
// (a * b) % p = (a % p * b % p) % p (3)
ret = pow(a, b, n / 2);
ret *= ret;
// 这一步是为了防止溢出
ret %= b;
if (n % 2 == 1) {
ret *= pow(a, b, 1);
}
// 执行取余操作
ret = ret % b;
return ret;
}
};
SOLUTION 2:
或者你也可以把pow(a, b, 1)直接写为a % b. 以下解法把base case: n = 1就拿掉了。
// SOLUTION 2:
// suppose n > 0
public static long pow(int a, int b, int n) {
if (a == 0) {
return 0;
} // The base case.
if (n == 0) {
return 1 % b;
} long ret = 0; // (a * b) % p = (a % p * b % p) % p (3)
ret = pow(a, b, n / 2);
ret *= ret; // 这一步是为了防止溢出
ret %= b; if (n % 2 == 1) {
ret *= (a % b);
} // 执行取余操作
ret = ret % b; return ret;
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/lintcode/math/FastPower.java
Lintcode: Fast Power 解题报告的更多相关文章
- Lintcode: Majority Number 解题报告
Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, th ...
- lintcode: k Sum 解题报告
K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...
- Lintcode: Minimum Subarray 解题报告
Minimum Subarray 原题链接: http://lintcode.com/zh-cn/problem/minimum-subarray/# Given an array of intege ...
- Lintcode: Subarray Sum 解题报告
Subarray Sum 原题链接:http://lintcode.com/zh-cn/problem/subarray-sum/# Given an integer array, find a su ...
- BZOJ 3969 Low Power 解题报告
我们首先将所有电池排序,那么我们可以找到一组最优方案,使得一台机器的能量之差是相邻两电池的能量之差. 然后我们就二分这个答案,从前往后贪心地选这个数对,然后看是否所有的数对都是满足条件的. 假设这个数 ...
- ACM-ICPC 2017 Asia HongKong 解题报告
ACM-ICPC 2017 Asia HongKong 解题报告 任意门:https://nanti.jisuanke.com/?kw=ACM-ICPC%202017%20Asia%20HongKon ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总
本文出自 http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner 打开 这个专题一共有25题,刷完 ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
随机推荐
- lnmp+zabbix 3.2 的编译安装
yum install pcre* gcc gcc-c++ autoconf automake zlib libxml libjpeg freetype libpng gd curl zlib-dev ...
- ASP.NET Core网站初探
原文地址:https://blog.csdn.net/iml6yu/article/details/74530679 目录结构如下图 目录: Properties:属性,记录了项目属性的配置文件. ...
- python学习笔记——urllib库中的parse
1 urllib.parse urllib 库中包含有如下内容 Package contents error parse request response robotparser 其中urllib.p ...
- 解决Android中多次点击(快速点击多次 )启动多个相同界面的问题
通过以下代码可以解决这个问题. /** * 防止快速点击 * @param ev * @return */ @Override public boolean dispatchTouchEvent(Mo ...
- python的内置下载器
python有个内置下载器,有时候在内部提供文件下载很好用. 进入提供下载的目录 # ls abc.aaa chpw.py finance.py lsdir.py ping.py u2d-partia ...
- Shell脚本开发规范
一.前言 由于工作需要,最近重新开始拾掇shell脚本.虽然绝大部分命令自己平时也经常使用,但是在写成脚本的时候总觉得写的很难看.而且当我在看其他人写的脚本的时候,总觉得难以阅读.毕竟shell脚本这 ...
- SLA等级那些9的实际意义
1. 重要的系统起码要设计达到99.9%的可靠性吧. 俗称3个9,这是什么意思呢? (1-99.9%)*365*24=8.76小时,表示该软件系统在连续运行1年时间里最多可能的业务中断时间是8.76小 ...
- stm8 I/O口模式配置
复位后的默认配置 :复位之后,所有的引脚都是悬浮输入模式. However, a few pins may have a different behavior. Refer to the datash ...
- Java 8 – StringJoiner example
In this article, we will show you a few StringJoiner examples to join String. 1. StringJoiner1.1 Joi ...
- nginx 443 https mark
#user nobody; worker_processes 4; #error_log logs/error.log; #error_log logs/error.log notice; ...