矩阵模板

来源:互联网 发布:电力scada源码 编辑:程序博客网 时间:2024/04/29 22:35

struct Matrix{    int mat[15][15];    int n;    void init(int _n){        n = _n;        memset(mat,0,sizeof(mat));    }    Matrix operator *(const Matrix &b)const{        Matrix ret;        ret.init(n);        for(int i = 0;i < n;i++)            for(int j = 0;j < n;j++){                ret.mat[i][j] = 0;                for(int k = 0;k < n;k++){                    ret.mat[i][j] += (long long)mat[i][k]*b.mat[k][j]%MOD;                    if(ret.mat[i][j] >= MOD)                        ret.mat[i][j] -= MOD;                }            }        return ret;    }};Matrix pow_M(Matrix a,long long n){    Matrix ret;    Matrix tmp = a;    ret.init(a.n);    for(int i = 0;i < a.n;i++)ret.mat[i][i] = 1;    while(n){        if(n&1)ret = ret*tmp;        tmp = tmp*tmp;        n >>= 1;    }    return ret;}


0 0