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:

 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的更多相关文章

  1. Lintcode: Fast Power 解题报告

    Fast Power 原题链接:http://lintcode.com/en/problem/fast-power/# Calculate the an % b where a, b and n ar ...

  2. 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 ...

  3. algorithm@ Matrix fast power

    一. 什么是快速幂: 快速幂顾名思义,就是快速算某个数的多少次幂.其时间复杂度为 O(log₂N), 与朴素的O(N)相比效率有了极大的提高.一般一个矩阵的n次方,我们会通过连乘n-1次来得到它的n次 ...

  4. 校赛热身 Problem B. Matrix Fast Power

    找循环节,肯定在40项以内,不会证明. #include <iostream> #include <cstring> #include <string> #incl ...

  5. 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 ...

  6. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  7. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

  8. [大坑]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 ...

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

    ssh的DH秘钥交换是一套复合几种算法的秘钥交换算法.在RFC4419中称为diffie-hellman-groupX-exchange-shaX 的算法(也有另一种单纯的 rsaX-shaX 交换算 ...

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

    之前为了自己做一套SSH,先自己实现了一套telnet.但经过这么多天的苦逼,发现以前的工作都是徒劳.ssh的协议很繁杂,核心的内容在于密码算法,而且自己很难在网上找到周全的细节讲解与详细的实现,只有 ...

随机推荐

  1. @font-face 用字体画图标

    HTML <body> <!-- ul.layout>li*5>a[href=#]>i.icon --> <!-- Sublime Text 快捷拼写 ...

  2. js中Frame框架的属性获取(1)

    js中window和document对象及如何操作iframe 一. window对象 . 什么是window对象? Window对象表示浏览器打开的窗口.如果文档包含iframe或者是frame标签 ...

  3. Part 100 Func delegate in c#

    What is Func<T,TResult> in C#? In simple terms,Func<T,TResult> is just generic delegate. ...

  4. Android里viewpager切换页面存在页面不相邻的页面被销毁的问题

    我之前一直因为viewpager+fragment时,所有页面的状态都会被自动保存 这次自己做了一个添加了5跟fragment的viewpager 测试时发现当从第一个切换到第四个页面时,再回到第一个 ...

  5. 解决windows端口被占用

    1, Cmd输入命令:netstat  –ano|findstr  “端口号” ,如netstat  –ano|findstr  “8080” 记下PID,最后一行为PID,这里为396 2,Cmd输 ...

  6. FreeBSD修改root密码错误passwd: pam_chau(www.111cn.net)thtok(): error in service module from:http://www.111cn.net/sys/freebsd/66713.htm

    在FreeBSD中修改帐号密码有时候会出现一些错误,针对passwd: pam_chauthtok(): error in service module这样的错误提示,简单整理了以下解决方案:错误提示 ...

  7. 上传至应用商店以及testflight相关。

    对于一个新的开发者账号来说,首先你需要创建一个新的发布证书.这个证书只要创建一次就行了,如果以后用的话,直接拿过来用就行了,当然发布证书是和配置文件一起使用的,还有就是关于p12,就是用创建证书的电脑 ...

  8. C#高效率导出Excel

    首先,需要引用excel的库: Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Exce ...

  9. 求单链表倒数第m个结点

    问题:求单链表倒数第m个结点,要求不准求链表的长度,也不许对链表进行逆转 解:设置两个指针p和q,p.q指向第一个结点.让p先移动到链表的第m个结点,然后p和q同时向后移动,直到p首先到达尾结点.此时 ...

  10. 通过百度地图API实现搜索地址--第三方开源--百度地图(三)

    搜索地址功能是建立在能够通过百度地图API获取位置的基础上 通过百度地图定位获取位置详情:http://www.cnblogs.com/zzw1994/p/5008134.html package c ...