标签:
矩阵乘法:
a(n, m), b(m, p) 为两个二维矩阵 相乘可得矩阵c(n, p) a中每一行和b中每一列相对应数的乘积之和;
1、
hdoj 1575--Tr A http://acm.hdu.edu.cn/showproblem.php?pid=1575
A为一个矩阵 求A^K中主对角线之和 , 结果取模;
#define mod 9973 #include <cstdio> #include <cstring> #include <iostream> using namespace std; struct matrix { int a[15][15]; }; int n; matrix mul(matrix a, matrix b) { matrix ret; memset(ret.a, 0, sizeof(ret.a)); for(int i = 0; i < n; i++) { for(int k = 0; k < n; k++) if(a.a[i][k]) for(int j = 0; j < n; j++) if(b.a[k][j]) ret.a[i][j]=(a.a[i][k]*b.a[k][j]+ret.a[i][j])%mod; } return ret; } matrix mpower(matrix tem, int m) { matrix I; for(int i = 0; i < 15; i++) for(int j = 0; j < 15; j++) I.a[i][j] = (i==j); while(m) { if(m&1) I = mul(I, tem); m >>= 1; tem = mul(tem, tem); } return I; } int main() { int T; scanf("%d", &T); while(T--) { int m; matrix num; scanf("%d%d", &n, &m); for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) scanf("%d", &num.a[i][j]); num = mpower(num, m); int sum = 0; for(int i = 0; i < n; i++) { sum = (sum+num.a[i][i])%mod; } printf("%d\n", sum); } return 0; }
2、
标签:
原文地址:http://www.cnblogs.com/fengshun/p/5109168.html