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. 【转】同一台机器部署两个jboss方法

    更改jboss的端口信息 1) 更改webservic的端口信息D:\jboss-new\server\default\conf\jboss-service.xml文件 <mbean code= ...

  2. web前端开发控件学习笔记之jqgrid+ztree+echarts

    版权声明:本文为博主原创文章,转载请注明出处.   作为web前端初学者,今天要记录的是三个控件的使用心得,分别是表格控件jqgrid,树形控件ztree,图表控件echarts.下边分别进行描述. ...

  3. bind() to 0.0.0.0:80 failed (98: Address already in use)

    You can kill it using: sudo fuser -k 80/tcp And then try restarting nginx again: service nginx start

  4. C#--深入分析委托与事件

    本篇文章将为你介绍一下 Delegate 的使用方式,逐渐揭开 C# 当中事件(Event)的由来,它能使处理委托类型的过程变得更加简单. 还将为您解释委托的协变与逆变,以及如何使用 Delegate ...

  5. 略谈Android之Intent

    前言:大家都知道Android程序的实现一般都由四大组件构成: Activity :Android程序实现功能的主体,提供了和客户交互的界面,也提供了和后台交互的功能. Service :是一个没有界 ...

  6. JS闭包理解_摘

    原文地址1:http://www.cnblogs.com/mzwr1982/archive/2012/05/20/2509295.html 闭包是一个比较抽象的概念,尤其是对js新手来说.书上的解释实 ...

  7. c# SQL CLR 之一

    CLR就是公共运行时,本文就对c#编写SQL StoredProcedures的过程进行简单讲解. [步骤] 2. 3. 7.打开设置 8. 注意删除方式:注意删除Assembly时,一定要先把引用此 ...

  8. linux下开发板网络速度测试记录

        由于做的项目对于网络和USB的读写速度有很高的要求,因此新拿回来的板子要测试网络和usb的最佳传输速度.要考虑不少因素,先把我能想到的记录下来.     测试的环境是开发板和ubuntu虚拟机 ...

  9. equals()源代码及释义

    源代码: public boolean equals(Object anObject) {if (this == anObject) { return true;}if (anObject insta ...

  10. Mysql表基本操作

    一. 创建表的方法 语法:create table 表名( 属性名数据类型完整约束条件, 属性名数据类型条完整约束件, ......... 属性名数据类型 ); (1)举例:1 create tabl ...