/*Tr A
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3015 Accepted Submission(s): 2246
Problem Description
A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973。
Input
数据的第一行是一个T,表示有T组数据。
每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据。接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容。
Output
对应每组数据,输出Tr(A^k)%9973。
Sample Input
2
2 2
1 0
0 1
3 99999999
1 2 3
4 5 6
7 8 9
Sample Output
2
2686
Author
xhd
Source
HDU 2007-1 Programming Contest
*/
#include<stdio.h>
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int m, n, A[11][11], B[11][11], C[11][11];
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
scanf("%d", &A[i][j]);
B[i][j] = A[i][j];
}
}
m--;
while(m)
{
if(m&0x1)
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
{
C[i][j] = 0;
for(int k = 1; k <= n; k++)
C[i][j] = (C[i][j] + B[i][k]*A[k][j]%9973) % 9973;
C[i][j] %= 9973;
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
B[i][j] = C[i][j];
m -= 1;
}
else
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
{
C[i][j] = 0;
for(int k = 1; k <= n; k++)
C[i][j] = (C[i][j] + A[i][k]*A[k][j] % 9973) % 9973;
C[i][j] %= 9973;
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
A[i][j] = C[i][j];
m >>= 1;
}
}
int sum = 0;
for(int i = 1; i <= n; i++)
{
sum = (sum + B[i][i]) % 9973;
}
printf("%d\n", sum%9973);
}
return 0;
}
Hduoj1575【矩阵幂取模】
最新推荐文章于 2021-07-01 20:08:54 发布