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
分析:
利用公式:
(a * b) % p = (a % p * b % p) % p
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
class Solution {
/*
* @param a, b, n: 32bit integers
* @return: An integer
*/
public int fastPower(int a, int b, int n) {
if (a == ) return ;
if (n == ) return % b;
if (n == || a == ) return a % b;
long temp = fastPower(a, b, n / );
temp = temp * temp;
temp = temp % b;
if (n % == ) {
temp = temp * (a % b);
return (int)(temp % b);
} else {
return (int)temp;
}
}
};
Fast Power的更多相关文章
- Lintcode: Fast Power 解题报告
Fast Power 原题链接:http://lintcode.com/en/problem/fast-power/# Calculate the an % b where a, b and n ar ...
- 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 ...
- [大坑]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的协议很繁杂,核心的内容在于密码算法,而且自己很难在网上找到周全的细节讲解与详细的实现,只有 ...
- SSH2.0编程 ssh协议过程实现(转)
SSh协议: 全称为Secure Shell,即很安全的shell,主要目的是用来取代传统的telnet和r系列命令(rlogin,rsh,rexec等)远程登录和远程执行命令的工具,实现远程登录和远 ...
随机推荐
- G-nav-03
/*dele masthead.css style*/.masthead .navigation .btn.btn-masthead.btn-apply:after { content: ''; di ...
- zabbix修改密码
在我们刚刚安装好zabbix之后我们的管理用户是 Admin,密码是zabbix 所以我们肯定是要更改密码的,这个方法也适用于我们忘记管理员密码 首先我们登录mysql数据库 这里有一个zabbix库 ...
- hdu1247 字典树
开始以为枚举会超时,因为有50000的词.后来试了一发就过了.哈哈.枚举没一个单词,将单词拆为2半,如果2半都出现过,那就是要求的. #include<stdio.h> #include& ...
- jQuery技术交流资料
jQuery技术交流资料https://www.zybuluo.com/jikeytang/note/65371
- 【CSU 1556】Pseudoprime numbers
题 Description Jerry is caught by Tom. He was penned up in one room with a door, which only can be op ...
- BZOJ-1491 社交网络 FLoyd+乱搞
感觉这两天一直在做乱搞的题... 1491: [NOI2007]社交网络 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1279 Solved: 732 ...
- mysql 设置编码 Incorrect string value: '\xE9\x98\xBF\xE4\xB8\x89...' for column 'cont,mysql乱码
首先这个是编码的问题 --细致的分割---------------------------------------------------------------------------------- ...
- codevs1225 八数码难题
题目描述 Description Yours和zero在研究A*启发式算法.拿到一道经典的A*问题,但是他们不会做,请你帮他们.问题描述 在 3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数 ...
- [Angularjs]视图和路由(三)
写在前面 上篇文章主要介绍了路由中when方法的第二个参数,常见的几个属性,以及作用.本篇文章,将介绍和路由相关的几个常见的服务. 系列文章 [Angularjs]ng-select和ng-optio ...
- html标签(一)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...