码迷,mamicode.com
首页 > 其他好文 > 详细

矩阵快速幂

时间:2018-09-07 14:00:49      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:矩阵快速幂   ==   operator   code   []   nbsp   span   class   div   

class Matrix {
public:
    int r, c;
    ll mat[32][32];
    
    ll *operator [] (int x) { return mat[x]; }
    
    Matrix operator * (const Matrix &a) const {
        Matrix res;
        res.r = r; res.c = a.c;
        int i, j, k;
        for (i=1; i<=res.r; ++i) {
            for (j=1; j<=res.c; ++j) {
                res[i][j] = 0;
                for (k=1; k<=c; ++k) 
                    res[i][j] += mat[i][k] * a.mat[k][j];
            }
        }
        return res;
    }
    
    
}m;

Matrix pwr(const Matrix &a, int k) {
    Matrix base = a, r;
    int i, j;
    r.r = a.r; r.c = a.c;
    for (i=1; i<=r.r; ++i)
        for (j=1; j<=r.c; ++j)
            r[i][j] = i==j;
    while (k) {
        if (k & 1) r = r * base;
        base = base * base;
        k >>= 1;
    }
    return r;
}

 

矩阵快速幂

标签:矩阵快速幂   ==   operator   code   []   nbsp   span   class   div   

原文地址:https://www.cnblogs.com/cgjh/p/9603572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!