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. LATeX 插入脚注

    LATeX 插入脚注: 使用  \footnote{...注释内容} 命令: To maximize the lower-bound $ we employ conjugate gradient me ...

  2. java struts2入门学习实例--用户注册和用户登录整合

    需求: 1.用户注册(user_register.jsp)-->注册成功(UserRegister.action)-->显示注册信息(register_success.jsp)2.用户登录 ...

  3. 【LeetCode】213. House Robber II

    House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...

  4. 软件公司的两种管理方式 总体来说,这个世界上存在两种不同的软件公司的组织结构。我把他们叫做 Widget Factory(小商品工厂) 和 Film Crews(电影工作组

    软件公司的两种管理方式 一个简单的回答应该是——“因为在我们的社会里,我们总是会认为薪水和会和职位的层次绑在一起”.但是,这个答案同时也折射出一个事实——我们的薪资是基于我们的所理解的价值,但这并没有 ...

  5. 关于 os模块的常用用法

    作为常用模块中的os模块,需要掌握的用法是非常重要的,今天就在这里把它归纳总结总结,以便自己日后的使用 一.os模块 含义:提供程序与操作系统直接操作的各个功能 二.常用的几个用法 os.getcwd ...

  6. Openlayers2中统计图的实现

    概述: 在前文中.介绍了Arcgis for js和Openlayers3中统计图的实现.在本文,书接上文.介绍在Openlayers2中,统计图的实现. 实现: 在Openlayers2中,popu ...

  7. asp.net C#取Excel 合并单元格内容

    asp教程.net c#取excel 合并单元格内容读取excel数据,填充dataset// 连接字符串 string xlspath = server.mappath("~/www.11 ...

  8. Linux中在防火墙中开启80端口的例子

    最近自己在学习Linux.搭建一个LNMP环境.在测试时一切都好.然后重启Linux后.再次访问网站无法打开.最终原因是在防火墙中没有加入 80 端口的规则.具体方法如下: 在CentOS下配置ipt ...

  9. Android 获取闹钟引发的血案

    想做一个锁屏的软件.锁屏后可以显示闹钟信息. 一开始的思路是通过android content provider获取 mActivityObject.getContentResolver().quer ...

  10. springboot 中文文档

    springboot 中文文档https://qbgbook.gitbooks.io/spring-boot-reference-guide-zh/content/