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等)远程登录和远程执行命令的工具,实现远程登录和远 ...
随机推荐
- php empty()和isset()的区别
在使用 php 编写页面程序时,我经常使用变量处理函数判断 php 页面尾部参数的某个变量值是否为空,开始的时候我习惯了使用 empty() 函数,却发现了一些问题,因此改用 isset() 函数,问 ...
- Java怎么实现多继承的功效
Java不支持多继承,但是通过一些巧妙的设计来达到和多继承同样的效果 通过接口.内隐类,继承.实现,互相配合,达到多继承的效果 1.Java中一个类不能继承多个具体class. 2.一个类只可继 ...
- golang thrift 总结一下网络上的一些坑
我们以hello world来大概分析一下golang中的thrift包,并且扒一扒网络上有关thrift的一些坑 查看源码,服务器定义如下:(详见simple_server.go文件) type T ...
- NOI题库--图论 宗教信仰
1526:宗教信仰 总时间限制: 5000ms 内存限制: 65536kB 描述 世界上有许多宗教,你感兴趣的是你学校里的同学信仰多少种宗教. 你的学校有n名学生(0 < n <= 500 ...
- mysql 中如何查找相同的数据
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbcAAAEYCAIAAABQvy+HAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4Xu
- jsp学习(三)
<%@page contentType="text/html;charset=gbk"%> <html> <body> <font siz ...
- uC/OS-II源码分析
uC/OS-II源码分析 首先从main.c文件看起,下面是uC/OS-II main.C的大致流程: main(){ OSInit(); TaskCreate(...); OSStart(); } ...
- abstract class和interface 知多少!!!
1.相同点 A. 两者都是抽象类,都不能实例化. B. interface实现类及abstrct class的子类都必须要实现已经声明的抽象方法. 2. 不同点 A. interface需 ...
- SpringServletContext简单案例
一.项目结构及相应jar包,如下图 二.UserService代码 package com.hjp.service; /** * Created by JiaPeng on 2015/11/15. * ...
- 锋利的jQuery-4--$(document).ready()和window.onload方法的区别
jQuery中的$(document).ready()和JavaScript中的window.onload方法主要有两个方面的不同: 1.执行时机: onload : 网页中所有的元素和元素的关联文件 ...