[LintCode]快速幂(数论)

计算a^n % b,其中a,b和n都是32位的整数。

快速幂搞就过了.快速幂首先就是要知道

(a*b)%c = ((a%c)*b)%c ,所以经过推导得出.

(a^n)%b = ((((a%b)*a)%b)*a)..........%b)*a) %b    (n次)

这样只能解决的a^n 超出计算机计数范围,复杂度还是没有降下来呢.

怎么办呢^O^,bit-manipulation!!!!!!

具体详解自行百度好了^_^(利用了二分的思想)

Code:

class Solution {
    /*
     * @param a, b, n: 32bit integers
     * @return: An integer
     */
    public int fastPower( int a, int b, int n ) {
        Long ret = new Long(1);
        long t=0;
        t=a;
        if(b==1) return a%b;
        while(n>0){
            if(n%2==1){
                ret = (ret*t)%b;
            }
            n=n>>1;
            t=(t*t)%b;
        }
        return ret.intValue();
    }

};
时间: 2024-08-01 14:52:44

[LintCode]快速幂(数论)的相关文章

Luogu P1226 取余运算||快速幂(数论,分治)

P1226 取余运算||快速幂 题目描述 输入b,p,k的值,求b^p mod k的值.其中b,p,k*k为长整型数. 输入输出格式 输入格式: 三个整数b,p,k. 输出格式: 输出"b^p mod k=s" s为运算结果 输入输出样例 输入样例#1: 2 10 9 输出样例#1: 2^10 mod 9=7 这是一道很有趣的水题,如果知道公式. 一般求解会溢出,导致答案错误. 这里介绍取模的一个公式: a*b%k=(a%k)*(b%k)%k. 在我们这道题中是b^p = (b^(p/

Lintcode快速幂

计算an % b,其中a,b和n都是32位的整数. 例如 231 % 3 = 2 例如 1001000 % 1000 = 0 这题要考虑输入的a,b是否为负数,结果是否溢出,是否超时,这里用到了分治递归 class Solution { public: /* * @param a, b, n: 32bit integers * @return: An integer */ int fastPower(int a, int b, int n) { // write your code here l

HDU 3240 Counting Binary Trees(组合数学-斯特林数,数论-整数快速幂,数论-求逆元)

Counting Binary Trees Problem Description There are 5 distinct binary trees of 3 nodes: Let T(n) be the number of distinct non-empty binary trees of no more than n nodes, your task is to calculate T(n) mod m. Input The input contains at most 10 test

POJ 1845 - Sumdiv ( 数论 + 唯一分解定理 + 快速幂取模 )

POJ 1845 - Sumdiv ( 数论 + 唯一分解定理 + 快速幂取模 ) 这是一道数论的好题,需要较好的数学基础 题意: 给定A,B,求A^B的所有因数的和,再MOD 9901 分析: 这里用到了数论当中相当一部分知识 a. 唯一分解定理 任何一个整数都可以分解为若干个素数的幂的乘积的形式 A = ( p1 ^ q1 + p2 ^ q2 + ..... + pn ^ qn ) p为素数 A^B = ( p1 ^ (q1*B) + p2 ^ (q2*B) + ..... + pn ^ (

POJ 2154 Color(组合数学-波利亚计数,数论-欧拉函数,数论-整数快速幂)

Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7693   Accepted: 2522 Description Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of th

POJ 1995 Raising Modulo Numbers (数论-整数快速幂)

Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4379   Accepted: 2516 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth

数论基础——循环节和矩阵快速幂的运用

首先我们来看一道基础题: 题目链接:HDU1005 Number Sequence 题目描述: Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 147421    Accepted Submission(s): 35814 Problem Description A number sequence is

HDU 2256 Problem of Precision 数论矩阵快速幂

题目要求求出(√2+√3)2n的整数部分再mod 1024. (√2+√3)2n=(5+2√6)n 如果直接计算,用double存值,当n很大的时候,精度损失会变大,无法得到想要的结果. 我们发现(5+2√6)n+(5-2√6)n是一个整数(2√6的偶数次幂总会正负抵消掉),并且(5-2√6)n是小于1的.所以我们就只需要求出Sn-1即可.令 An=(5+2√6)n;  Bn=(5-2√6)n. Sn=An+Bn     Sn为整数. Sn*((5+2√6)+(5-2√6))=Sn*10 Sn*

【BZOJ 1409】 Password 数论(扩展欧拉+矩阵快速幂+快速幂)

读了一下题就会很愉快的发现,这个数列是关于p的幂次的斐波那契数列,很愉快,然后就很愉快的发现可以矩阵快速幂一波,然后再一看数据范围就......然后由于上帝与集合对我的正确启示,我就发现这个东西可以用欧拉函数降一下幂,因为两个数一定互质因此不用再加一个phi(m),于是放心的乘吧宝贝!! #include <cstdlib> #include <cstring> #include <cstdio> #include <iostream> #include &