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. iOS 10 SceneKit 新特性 – SceneKit 制作 3D 场景框架

    来源:scauos(@大朕东) 链接:http://www.jianshu.com/p/b30785bb6c97 开头语: 今天的主题是探索iOS10 SceneKit的新功能,你可以观看今年WWDC ...

  2. (转)Groupon前传:从10个月的失败作品修改,1个月找到成功 并不挶泥在这个点子上面,它反而往后站一步,看看他们已经做好的这个网站,可以再怎么包装成另一个完完全全不同的网站?所有的人所做的每件失败的事情中, 一定有碰到或含有成功的答案」在里面,只是他们不知道而已。 人不怕失败」,只怕宣布失败」

    (转)Groupon前传:从10个月的失败作品修改,1个月找到成功 今天读到 一个非常励志人心的故事 ,就像现在「叶问」有「前传」,最近很火红的团集购网站Groupon 也出现了「Groupon前传」 ...

  3. 神文章1:去年(2011)一年干了些啥? -vivo神人

    评论: 来自豆瓣的vivo神人,之前不知道有着一号牛逼的人物,觉此人博学.有正义感,其中有一片文章述说了中国近代经济演变历史情况,于我感触很深.因时间关系,没通读,有时间一定读完(微博口水杂录简略看了 ...

  4. 【SqlServer】如何把本地SqlServer数据库部署到远程服务器上

    这里笔者使用的使用SqlServer2012,本机和远程环境均为Win7. 1.选中需要部署的数据库,右击>任务>分离.选中删除连接. 2.现在在左侧的表中就看不见刚才那个数据了. 3.在 ...

  5. linux文件系统 - 初始化(三)

    执行init程序 一.目的 内核加载完initrd文件后,为挂载磁盘文件系统做好了必要的准备工作,包括挂载了sysfs.proc文件系统,加载了磁盘驱动程序驱动程序等.接下来,内核跳转到用户空间的in ...

  6. sublime 技巧与快捷键篇

    技巧大全:https://www.zhihu.com/question/24896283   项目排除文件夹,更便于ctrl + p的搜索,比如可恶的node_modules "folder ...

  7. 温故而知新 babel-cli 的相关使用

    # 在线编译 http://babeljs.io/repl # babel-cli 安装入门 http://babeljs.io/setup#installation # babel-cli 使用手册 ...

  8. 转 Kubernetes 入门 概念理解

    你闺女也能看懂的插画版Kubernetes指南 原创  2016-06-30 作者 周小璐 译 编者按:Matt Butcher是Deis的平台架构师,热爱哲学,咖啡和精雕细琢的代码.有一天女儿走进书 ...

  9. 如何使用ODBC搭配dsn链接数据库

    { OdbcConnection cn; OdbcCommand cmd; string MyString; MyString="Select * from Customers"; ...

  10. telegraf input的配置

    .操作系统基础监控指标配置标准 基础监控使用通用的全局配置文件telegraf.conf,以下只贴上采集器input部分代码 telegraf -config /etc/telegraf/telegr ...