比赛 201712练习 评测结果 WWTTTTEETE
题目名称 矩阵幂之和 最终得分 0
用户昵称 @@@ 运行时间 11.768 s
代码语言 C++ 内存使用 0.32 MiB
提交时间 2018-01-08 19:58:28
显示代码纯文本
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int n,k,m;

struct JZ
{
	int a[44][44];
}A;
JZ sum(JZ x,JZ y)
{
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			x.a[i][j] += y.a[i][j];
			x.a[i][j] %= m;

			/* code */
		}
		/* code */
	}
	return x;
}	
JZ mul(JZ x,JZ y)
{
	JZ B;
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			for (int k = 1; k <= n; ++k)
			{
				B.a[i][j] += (x.a[i][k]*y.a[k][j])%m;
				B.a[i][j]%=m;
				/* code */
			}
			
			/* code */
		}
		/* code */
	}
	return B;
}
JZ KS(JZ x,int i)
{
	if (i <= 1)
	{
		return x;
		/* code */
	}
	int y = i/2;
	JZ  Z = KS(x,y);
	Z = mul(Z,Z);
	if (i%2 == 0)
	{
		return Z;
		/* code */
	}
	else
	{
		return mul(x,Z);
	}
}
int main()
{
	freopen("matrix_sum.in","r",stdin);
	freopen("matrix_sum.out","w",stdout);
	cin >> n >> k >> m;
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			cin >> A.a[i][j];
			/* code */
		}
		/* code */
	}
 	JZ ans;
	for (int i = 1; i <= k; ++i)
	{
		ans = sum(ans, KS(A,i));
		/* code */
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			cout << ans.a[i][j] << ' ' ;
			/* code */
		}
		cout << endl;
		/* code */
	}
	return 0;

}