九度 题目1443:Tr A

来源:互联网 发布:2016淘宝活动报名入口 编辑:程序博客网 时间:2024/06/06 02:57

题目来源:http://ac.jobdu.com/problem.php?pid=1443

时间限制:1 秒

内存限制:128 兆

特殊判题:

提交:411

解决:243

题目描述:

A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973。

输入:

数据的第一行是一个T,表示有T组数据。
每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据。接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容。

输出:

对应每组数据,输出Tr(A^k)%9973。

样例输入:
22 21 00 13 999999991 2 34 5 67 8 9
样例输出:
22686
#include <iostream>#include <cstring>#include <cstdio>using namespace std;const int iMod = 9973;const int MAXN = 11;struct MatrixNode{int iMatrix[MAXN][MAXN];};MatrixNode iPer, iCell;void Inite(int n){int i, j;for (i = 0; i < n; ++i){for(j = 0; j < n; ++j){scanf("%d", &iCell.iMatrix[i][j]);iPer.iMatrix[i][j] = (i==j);}}}MatrixNode Multi_Matrix(MatrixNode a, MatrixNode b, int n){MatrixNode c;int i, j, k;for (i = 0; i < n; ++i){for (j = 0; j < n; ++j){c.iMatrix[i][j] = 0;for (k = 0; k < n; ++k){c.iMatrix[i][j] = (c.iMatrix[i][j] + a.iMatrix[i][k]*b.iMatrix[k][j])%iMod;}}}return c;}MatrixNode Quick_Mod_Matrix(int k, int n){MatrixNode c, iCur;c = iPer;iCur = iCell;while (k){if(k&1){c = Multi_Matrix(c, iCur, n);k--;}iCur = Multi_Matrix(iCur, iCur, n);k >>= 1;}return c;}int main(){int T, n, k, i, iSum;scanf("%d", &T);MatrixNode iRes;while (T--){scanf("%d %d", &n, &k);Inite(n);iRes = iCell;iRes = Quick_Mod_Matrix(k, n);iSum = 0;for (i = 0; i < n; ++i)iSum = (iSum + iRes.iMatrix[i][i])%iMod;printf("%d\n", iSum);}return 0;}


0 0
原创粉丝点击