LintCode-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)

Solution:

 1 class Solution {
 2     /*
 3      * @param a, b, n: 32bit integers
 4      * @return: An integer
 5      */
 6     public int fastPower(int a, int b, int n) {
 7         if (n==0) return 1%b;
 8
 9         int res = fastPowerRecur(a,b,n);
10         return res;
11     }
12
13     public int fastPowerRecur(int a, int b, int n){
14         if (n==1)
15             return a%b;
16
17         long temp = fastPowerRecur(a,b,n/2);
18         temp = temp*temp%b;
19         int res = (n%2==0) ? (int) temp: (int)(temp*a%b);
20         return res;
21     }
22 };
时间: 2024-10-27 07:46:40

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

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: Hash Function && Summary: Modular Multiplication, Addition, Power && Summary: 长整形long

In data structure Hash, hash function is used to convert a string(or any other type) into an integer smaller than hash size and bigger or equal to zero. The objective of designing a hash function is to "hash" the key as unreasonable as possible.

SSH2.0编程 ssh协议过程实现(转)

之前为了自己做一套SSH,先自己实现了一套telnet.但经过这么多天的苦逼,发现以前的工作都是徒劳.ssh的协议很繁杂,核心的内容在于密码算法,而且自己很难在网上找到周全的细节讲解与详细的实现,只有靠自己刷RFC和问大神还有就是靠强X我的服务器艰难地完成. 现计算了下时间,自己做SSH耗费了进两个月的时间,虽然期间也夹着其他的繁杂事物,但自己在这方面确是是耗费了非常大的精力.因为这方面详细资料的匮乏,自己以前也几乎没有接触过密码学方面的东西,很多只有靠自己摸索,所以我得经常拿我自己的服务器来做

ssh秘钥交换详解与实现 diffie-hellman-group-exchange-sha

ssh的DH秘钥交换是一套复合几种算法的秘钥交换算法.在RFC4419中称为diffie-hellman-groupX-exchange-shaX 的算法(也有另一种单纯的 rsaX-shaX 交换算法).本文就以diffie-hellman-group-exchange-sha256为例,详尽地讲解整个完整的秘钥交换过程. 笔者在RFC上和网上看了很久,也只是做了一个大致了解,对实现的帮助不大.实际在实现过程中,有太多的细节需要注意,在很多细节的分歧中,需要自己抱着勇气去测试.(原谅我不看op

LeetCode#50 Pow(x, n)

Just... Implement pow(x, n). Solution: 1)Naive solution:multiply x by itself for n-1 times. (Or simply reyurn 1 if n==0). This takes O(n) time. When n is big enough, it's relatively slow. 2)Fast Power. Take pow(3, 11) as an example. Here's what we're

SSH2.0编程 ssh协议过程实现

之前为了自己做一套SSH,先自己实现了一套telnet.但经过这么多天的苦逼,发现以前的工作都是徒劳.ssh的协议很繁杂,核心的内容在于密码算法,而且自己很难在网上找到周全的细节讲解与详细的实现,只有靠自己刷RFC和问大神还有就是靠强X我的服务器艰难地完成. 现计算了下时间,自己做SSH耗费了进两个月的时间,虽然期间也夹着其他的繁杂事物,但自己在这方面确是是耗费了非常大的精力.因为这方面详细资料的匮乏,自己以前也几乎没有接触过密码学方面的东西,很多只有靠自己摸索,所以我得经常拿我自己的服务器来做

Codeforces Round #257 (Div. 2)B 矩阵快速幂

B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, please calculate fn m

PatentTips - Fast awake from low power mode

BACKGROUND Electronic devices, such as electronic book readers ("eBook reader devices"), cellular telephones, portable media players, desktop computers, laptops, tablet computers, netbooks, personal digital assistants, and the like, rely on elec

[Lintcode]142. O(1) Check Power of 2

142. O(1) Check Power of 2 本题难度: Easy Topic: Math&Bit Manipulation Description Using O(1) time to check whether an integer n is a power of 2. Example Example 1: Input: 4 Output: true Example 2: Input: 5 Output: false Challenge O(1) time 别人的代码 参考野球拳刷刷