Loading

矩阵乘法模板代码

CI mod = 1e9 + 7;

struct matrix{ int a[maxm][maxm], n, m;};

matrix matrixMul(matrix p, matrix q){
    matrix res;
    res.n = p.n, res.m = q.m;
    f (i, 0, res.n - 1)
        f (j, 0, res.m - 1){
        	res.a[i][j] = 0;
            f (k, 0, p.m - 1)
                (res.a[i][j] += p.a[i][k] * q.a[k][j] % mod) %= mod;
		}
	return res;
}

matrix ksm(matrix a, int b){
    matrix ans = I;
    for (; b; b >>= 1, a = matrixMul(a, a))
    	if (b & 1)
    		ans = matrixMul(ans, a);
	return ans;
}
posted @ 2023-06-13 13:40  DE_aemmprty  阅读(34)  评论(0)    收藏  举报