矩阵快速幂

快速幂

例如求35,5的二进制位101,所以5也可以写成22+20,则35=34x31

int fastpow(int base,int n){
    int res=1;
    while(n){
        if(n&1)res*=base;
        base*=base;//维持base的多少次方
        n/=2;//继续扫描前一位
    }
    return res;
}

参考:
http://huanyouchen.github.io/2018/05/23/Quick-Matrix-Pow/

posted @ 2019-03-20 15:42  开局一把刀  阅读(5)  评论(0)    收藏  举报