LintCode-Fast Power
Calculate the an % b where a, b and n are all 32bit integers.
For 231 % 3 = 2
For 1001000 % 1000 = 0
O(logn)
Solution:
class Solution {
/*
* @param a, b, n: 32bit integers
* @return: An integer
*/
public int fastPower(int a, int b, int n) {
if (n==0) return 1%b;
int res = fastPowerRecur(a,b,n);
return res;
}
public int fastPowerRecur(int a, int b, int n){
if (n==1)
return a%b;
long temp = fastPowerRecur(a,b,n/2);
temp = temp*temp%b;
int res = (n%2==0) ? (int) temp: (int)(temp*a%b);
return res;
}
};
LintCode-Fast Power的更多相关文章
- Lintcode: Fast Power 解题报告
Fast Power 原题链接:http://lintcode.com/en/problem/fast-power/# Calculate the an % b where a, b and n ar ...
- Fast Power
Calculate the a^n % b where a, b and n are all 32bit integers. Example For 2^31 % 3 = 2 For 100^1000 ...
- algorithm@ Matrix fast power
一. 什么是快速幂: 快速幂顾名思义,就是快速算某个数的多少次幂.其时间复杂度为 O(log₂N), 与朴素的O(N)相比效率有了极大的提高.一般一个矩阵的n次方,我们会通过连乘n-1次来得到它的n次 ...
- 校赛热身 Problem B. Matrix Fast Power
找循环节,肯定在40项以内,不会证明. #include <iostream> #include <cstring> #include <string> #incl ...
- 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 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- [大坑]FFT学习
[大坑]FFT学习 Macros #define fon(i,s) for(int i=0;i<s; ++i) #define fone(i,s) for(int i=0;i<=s;++i ...
- ssh秘钥交换详解与实现 diffie-hellman-group-exchange-sha
ssh的DH秘钥交换是一套复合几种算法的秘钥交换算法.在RFC4419中称为diffie-hellman-groupX-exchange-shaX 的算法(也有另一种单纯的 rsaX-shaX 交换算 ...
- SSH2.0编程 ssh协议过程实现
之前为了自己做一套SSH,先自己实现了一套telnet.但经过这么多天的苦逼,发现以前的工作都是徒劳.ssh的协议很繁杂,核心的内容在于密码算法,而且自己很难在网上找到周全的细节讲解与详细的实现,只有 ...
随机推荐
- Matlab之cell使用
1.声明 (1) DataCell = cell(1,N);(2) DataCell{N} = []; 如何赋值呢? a{1,1}=rand(5) 那么a的1行1列的单元中存储的就是一个随机的5×5的 ...
- ruby学习--block
#当前块 class Block def a_method return yield if block_given? 'no block' end end obj=Block.new puts &qu ...
- 绝对好评的淘宝购物导航:baiso.uz.taobao.com
绝对好评的淘宝购物导航:baiso.uz.taobao.com 绝对好评的淘宝购物导航--百搜:http://baiso.uz.taobao.com
- 四.CSS声明
概述 一条CSS规则包括选择符和声明两个部分,声明又包括属性和值,属性指出要影响样式的那个方面,而值用于把属性设定成什么 每个HTML元素有很多属性,可以用CSS来设定,但值却只有少数的几种 CSS属 ...
- OSPF多区域配置
1.配置三台路由器IP R1(config)#INTER S1/0 R1(config-if)#IP ADDress 192.1.12.1 255.255.255.0 R1(config-if)#no ...
- 网站整站变灰的方法(不支持IE10)
html { -ms-filter: grayscale(100%); -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); - ...
- 【学习笔记】【C语言】注释
1. 什么是注释 1) 注释是在所有计算机语言中都非常重要的一个概念,从字面上看,就是注解.解释的意思 2) 注释可以用来解释某一段程序或者某一行代码是什么意思,方便程序员之间的交流.假如我写完一行代 ...
- Dicom格式文件解析器
转自:http://www.cnblogs.com/assassinx/archive/2013/01/09/dicomViewer.html Dicom全称是医学数字图像与通讯,这里讲的暂不涉及通讯 ...
- 在hibernate中使用c3p0数据源
1. jar包 hibernate-release-4.1.8.Final\lib\optional\c3p0 2. 加入配置: <!-- 数据库连接池最大连接数 --> &l ...
- Connected_Component Labelling(联通区域标记算法) C++实现
// Connected-Component Labelling.cpp : 定义控制台应用程序的入口点.//如有是使用,请务必注明引用出处网站:http://www.cnblogs.com/Amat ...