矩阵快速幂

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;
}

 

posted @ 2018-09-07 11:30  过路人1998  阅读(102)  评论(0编辑  收藏  举报