Hduoj 1575[매트릭스 모드]

1736 단어 c
/*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
", sum%9973); } return 0; }

좋은 웹페이지 즐겨찾기