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.

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),这个会不断循环调用。

 1 class Solution {
 2     /*
 3      * @param a, b, n: 32bit integers
 4      * @return: An integer
 5      */
 6     /*
 7      * @param a, b, n: 32bit integers
 8      * @return: An integer
 9      */
10     public static int fastPower(int a, int b, int n) {
11         // write your code here
12         long ret = pow(a, b, n);
13
14         return (int) ret;
15     }
16
17     // suppose n > 0
18     public static long pow(int a, int b, int n) {
19         if (a == 0) {
20             return 0;
21         }
22
23         // The base case.
24         if (n == 0) {
25             return 1 % b;
26         }
27
28         if (n == 1) {
29             return a % b;
30         }
31
32         long ret = 0;
33
34         // (a * b) % p = (a % p * b % p) % p (3)
35         ret = pow(a, b, n / 2);
36         ret *= ret;
37
38         // 这一步是为了防止溢出
39         ret %= b;
40
41         if (n % 2 == 1) {
42             ret *= pow(a, b, 1);
43         }
44
45         // 执行取余操作
46         ret = ret % b;
47
48         return ret;
49     }
50 };

SOLUTION 2:

或者你也可以把pow(a, b, 1)直接写为a % b. 以下解法把base case: n = 1就拿掉了。

 1 // SOLUTION 2:
 2     // suppose n > 0
 3     public static long pow(int a, int b, int n) {
 4         if (a == 0) {
 5             return 0;
 6         }
 7
 8         // The base case.
 9         if (n == 0) {
10             return 1 % b;
11         }
12
13         long ret = 0;
14
15         // (a * b) % p = (a % p * b % p) % p (3)
16         ret = pow(a, b, n / 2);
17         ret *= ret;
18
19         // 这一步是为了防止溢出
20         ret %= b;
21
22         if (n % 2 == 1) {
23             ret *= (a % b);
24         }
25
26         // 执行取余操作
27         ret = ret % b;
28
29         return ret;
30     }

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/lintcode/math/FastPower.java

时间: 2024-11-10 17:21:11

Lintcode: Fast Power 解题报告的相关文章

Lintcode: Majority Number 解题报告

Majority Number 原题链接:http://lintcode.com/en/problem/majority-number/# Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it. Example For [1, 1, 1, 1, 2, 2, 2], return 1 Challenge O(

lintcode: k Sum 解题报告

k SumShow Result My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers where sum is target. Calculate how many solutions there are? Ex

BZOJ 3969 Low Power 解题报告

我们首先将所有电池排序,那么我们可以找到一组最优方案,使得一台机器的能量之差是相邻两电池的能量之差. 然后我们就二分这个答案,从前往后贪心地选这个数对,然后看是否所有的数对都是满足条件的. 假设这个数对是 i - 1, i,并且是第 j 个数对,那么我们称满足条件为: 2nk - i + 2 >= 2k(n - j + 1) 意思就是能拿出足够多的电池来组成机器人. 然后注意特判:如果不能选出足够多的数对就返回 false,我在这里 WA 到死... 毕竟 Gromah 太弱,只会做水题. 1

解题报告 之 HOJ2816 Power Line

解题报告 之 HOJ2816 Power Line Problem Description While DUT is hosting this NECPC Contest, in order to control the expenditure, careful considerations should be given in many respects, such as the layout of the contest venue. When the contest venue is ar

ACM-ICPC 2017 Asia HongKong 解题报告

ACM-ICPC 2017 Asia HongKong 解题报告 任意门:https://nanti.jisuanke.com/?kw=ACM-ICPC%202017%20Asia%20HongKong 按AC次序: D - Card collection In an online game, a player can collect different types of power cards. Each power card can enable a player to have a uni

poj分类解题报告索引

图论 图论解题报告索引 DFS poj1321 - 棋盘问题 poj1416 - Shredding Company poj2676 - Sudoku poj2488 - A Knight's Journey poj1724 - ROADS(邻接表+DFS) BFS poj3278 - Catch That Cow(空间BFS) poj2251 - Dungeon Master(空间BFS) poj3414 - Pots poj1915 - Knight Moves poj3126 - Prim

解题报告 之 ZOJ2332 Gems

解题报告 之 ZOJ2332 Gems Description Wealthy alsomagic! Supernatural alsomagic! But also poor alsomagic! Because he is now puzzled by a problem, and will go crazy if you can't help him. alsomagic has a lot of gems with different colors and shapes. His sup

LeetCode解题报告:Reorder List

Reorder List Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 思路: 1.利用快慢两个指针将链表一分为二: 2.针对第二个子链表求倒序

LeetCode解题报告:Linked List Cycle &amp;&amp; Linked List Cycle II

Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follo