lintcode-medium-Fast Power

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

For 2^31 % 3 = 2

For 100^1000 % 1000 = 0

这道题有个值得注意的问题,当n为奇数的时候,如果把递归写成:

long num = fastPower(a, b, n / 2);

return (int) ((a%b * num * num) % b);

这样的话在某些test case会溢出,所以最好还是写成以下的形式

class Solution {
    /*
     * @param a, b, n: 32bit integers
     * @return: An integer
     */
    public int fastPower(int a, int b, int n) {
        // write your code here
        a %= b;

        if(n == 0)
            return 1 % b;
        if(n == 1)
            return a;

        if(n % 2 == 0){
            long num = fastPower(a, b, n / 2);
            return (int) ((num * num) % b);
        }
        else{
            long num = fastPower(a, b, n - 1);
            return (int) ((a * num) % b);
        }
    }
};
时间: 2024-10-07 12:41:21

lintcode-medium-Fast Power的相关文章

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的. 我们利用 取模运算的乘法法则:

[lintcode medium]Palindrome Linked List

Palindrome Linked List Implement a function to check if a linked list is a palindrome. Example Given 1->2->1, return true Challenge Could you do it in O(n) time and O(1) space? //// 1\find out the medium index of Linked list 2\ reverse the right par

[lintcode medium]Anagrams

Anagrams Given an array of strings, return all groups of strings that are anagrams. Example Given ["lint", "intl", "inlt", "code"], return ["lint", "inlt", "intl"]. Given ["ab"

[lintcode medium] digit counts

Digit Counts Count the number of k's between 0 and n. k can be 0 - 9. Example if n=12, k=1 in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], we have FIVE 1's (1, 10, 11, 12) class Solution { /* * param k : As description. * param n : As description. * r

[lintcode medium]Maximum Subarray II

Maximum Subarray II Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Example For given [1, 3, -1, 2, -1, 2], the two subarrays are [1,

[lintcode medium]4 sum

4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Example Given array S = {1 0 -1 0 -2 2}, and target = 0. A solution

[lintcode medium]Divide Two Integers

Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return 2147483647 Example Given dividend = 100 and divisor = 9, return 11. public class Solution { /** * @param dividend the dividend

[lintcode medium] Two sum

Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please no

Fast Power

Calculate the an % b where a, b and n are all 32bit integers. Example For 231 % 3 = 2 For 1001000 % 1000 = 0 分析: 利用公式: (a * b) % p = (a % p * b % p) % pa^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

[lintcode medium]Roman to Integer

Roman to Integer Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range from 1 to 3999. Example IV -> 4 XII -> 12 XXI -> 21 XCIX -> 99 Clarification What is Roman Numeral? https://en.wikipedia.org/wiki