数据结构笔记--快速幂算法

快速幂的目的就是做到快速求幂,假设我们要求a^b,按照朴素算法就是把a连乘b次,时间复杂度是O(b)为O(n)级别,快速幂能做到O(logn)

原理如下:

             假设我们要求a^b,那么其实b是可以拆成二进制的,该二进制数第i位的权为2^(i-1),例如当b==11时  a11=a(2^0+2^1+2^3)

11的二进制是1011,11 = 2³×1 + 2²×0 + 2¹×1 + 2º×1,因此,我们将a¹¹转化为算 a^2^0*a^2^1*a^2^3,也就是1*a^1*1*a^2*0*a^4*a^8,原来算11次,现在算三次

由于是二进制,很自然地想到用位运算这个强大的工具:&和>>    

>>&运算通常用于二进制取位操作,例如一个数 & 1 的结果就是取二进制的最末位。还可以判断奇偶x&1==0为偶,x&1==1为奇。

>>运算表示二进制去掉最后一位

public double Power(double base, int n) {
    double res = 1,curr = base;
    int exponent;
    if(n>0){
        exponent = n;
    }else if(n<0){
        if(base==0)
            throw new RuntimeException("分母不能为0"); 
        exponent = -n;
    }else{// n==0
        return 1;// 0的0次方
    }
    while(exponent!=0){
        if((exponent&1)==1)
            res*=curr;
        curr*=curr;// 翻倍
        exponent>>=1;// 右移一位
    }
    return n>=0?res:(1/res);       
}

以b==11为例,b=>1011,二进制从右向左算,但乘出来的顺序是 a^(2^0)*a^(2^1)*a^(2^3),是从左向右的。我们不断的让curr*=curr目的是累乘,以便随时对res做出贡献。

其中要理解curr*=curr这一步:因为 curr*curr==curr^2,下一步再乘,就是curr^2*curr^2==curr^4,然后同理  curr^4*curr^4=curr^8,由此可以做到curr-->curr^2-->curr^4-->curr^8-->curr^16-->curr^32.......指数正是 2^i ,再看上面的例子,a¹¹= a1*a2*a8,这三项就可以完美解决了,快速幂就是这样。

另一种高效的解法,原理同快速幂差不多,如下:

class Solution {
public:
    double pow(double x, int n) {
        double res = 1.0;
        for (int i = n; i != 0; i /= 2) {
            if (i % 2 != 0) res *= x;
            x *= x;
        }
        return n < 0 ? 1 / res : res;
    }
};

矩阵快速幂也是这个道理,下面放一个求斐波那契数列的矩阵快速幂模板

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 const int mod = 10000;
 8 const int maxn = 35;
 9 int N;
10 struct Matrix {
11     int mat[maxn][maxn];
12     int x, y;
13     Matrix() {
14         memset(mat, 0, sizeof(mat));
15         for (int i = 1; i <= maxn - 5; i++) mat[i][i] = 1;
16     }
17 };
18 inline void mat_mul(Matrix a, Matrix b, Matrix &c) {
19     memset(c.mat, 0, sizeof(c.mat));
20     c.x = a.x; c.y = b.y;
21     for (int i = 1; i <= c.x; i++) {
22         for (int j = 1; j <= c.y; j++) {
23             for (int k = 1; k <= a.y; k++) {
24                 c.mat[i][j] += (a.mat[i][k] * b.mat[k][j]) % mod;
25                 c.mat[i][j] %= mod;
26             }
27         }
28     }
29     return ;
30 }
31 inline void mat_pow(Matrix &a, int z) {
32     Matrix ans, base = a;
33     ans.x = a.x; ans.y = a.y;
34     while (z) {
35         if (z & 1 == 1) mat_mul(ans, base, ans);
36         mat_mul(base, base, base);
37         z >>= 1;
38     }
39     a = ans;
40 }
41 int main() {
42     while (cin >> N) {
43         switch (N) {
44             case -1: return 0;
45             case 0: cout << "0" << endl; continue;
46             case 1: cout << "1" << endl; continue;
47             case 2: cout << "1" << endl; continue;
48         }
49         Matrix A, B;
50         A.x = 2; A.y = 2;
51         A.mat[1][1] = 1; A.mat[1][2] = 1;
52         A.mat[2][1] = 1; A.mat[2][2] = 0;
53         B.x = 2; B.y = 1;
54         B.mat[1][1] = 1; B.mat[2][1] = 1;
55         mat_pow(A, N - 1);
56         mat_mul(A, B, B);
57         cout << B.mat[1][1] << endl;
58     }
59     return 0;
60 }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值