Fast Power

原题链接:http://lintcode.com/en/problem/fast-power/#

Calculate the an % b where a, b and n are all 32bit integers.

Example

For 231 % 3 = 2

For 1001000 % 1000 = 0

Challenge

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 解题报告的更多相关文章

  1. Lintcode: Majority Number 解题报告

    Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, th ...

  2. lintcode: k Sum 解题报告

    K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...

  3. Lintcode: Minimum Subarray 解题报告

    Minimum Subarray 原题链接: http://lintcode.com/zh-cn/problem/minimum-subarray/# Given an array of intege ...

  4. Lintcode: Subarray Sum 解题报告

    Subarray Sum 原题链接:http://lintcode.com/zh-cn/problem/subarray-sum/# Given an integer array, find a su ...

  5. BZOJ 3969 Low Power 解题报告

    我们首先将所有电池排序,那么我们可以找到一组最优方案,使得一台机器的能量之差是相邻两电池的能量之差. 然后我们就二分这个答案,从前往后贪心地选这个数对,然后看是否所有的数对都是满足条件的. 假设这个数 ...

  6. ACM-ICPC 2017 Asia HongKong 解题报告

    ACM-ICPC 2017 Asia HongKong 解题报告 任意门:https://nanti.jisuanke.com/?kw=ACM-ICPC%202017%20Asia%20HongKon ...

  7. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  8. [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总

    本文出自   http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner  打开 这个专题一共有25题,刷完 ...

  9. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

随机推荐

  1. python+stomp+activemq

    python也可以连接MQ,以ActiveMQ为例,安装stomp.py: https://github.com/jasonrbriggs/stomp.py 下载后安装: python setup.p ...

  2. 实测iOS Dynamic Framework 对 App 启动时间的影响效果

    最近看到的Slow App Startup Times里提到: The dynamic loader finds and reads the dependent dynamic libraries ( ...

  3. C语言lseek()函数:移动文件的读写位置

    相关函数:dup, open, fseek 头文件:#include <sys/types.h>    #include <unistd.h> 定义函数:off_t lseek ...

  4. Linux基本数据类型大小——int,char,long int,long long int

    转自:http://paddy-w.iteye.com/blog/1403217 在Linux操作系统下使用GCC进行编程,目前一般的处理器为32位字宽,下面是/usr/include/limit.h ...

  5. 【java】java开发中的23种设计模式详解

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  6. SQL Server中临时表与表变量的区别

    我们在数据库中使用表的时候,经常会遇到两种使用表的方法,分别就是使用临时表及表变量.在实际使用的时候,我们如何灵活的在存储过程中运用它们,虽然它们实现的功能基本上是一样的,可如何在一个存储过程中有时候 ...

  7. Axure谷歌浏览器Chrome扩展程序下载及安装方法

    对于很多需要设计产品原型的朋友来说,Axure RP Pro可谓是非常方便.好用的一款软件,因为它不仅能绘制出详细的产品构思,也能生成浏览器格式的产品原型.但是如果想把原型拿给客户查看,千万记得给浏览 ...

  8. WinForm DataGridView新增加行

      1.不显示最下面的新行 通常 DataGridView 的最下面一行是用户新追加的行(行头显示 * ).如果不想让用户新追加行即不想显示该新行,可以将 DataGridView 对象的 Allow ...

  9. Js字符串与十六进制的相互转换 【转】

    开发过程中,字符串与十六进.二进制之间的相互转换常常会用到,尤其是涉及到中文的加密时,就需要把中文转换为十六进制.下面说说具体的转换方法. 1.字符串转换为十六进制 主要使用 charCodeAt() ...

  10. android check box 自定义图片

    http://blog.csdn.net/competerh_programing/article/details/7417074