矩阵的幂

模板题 快速幂求矩阵的幂

https://www.luogu.com.cn/problem/P3390

	#include<iostream>
	#include<stdio.h>
	#include<algorithm>
	using namespace std;
	const int N = 110; long long  n, k;
	const int mod = 1000000007;
	struct Matrix {
		long long c[N][N];
	}A, I;
	Matrix operator*(const Matrix &x, const Matrix &y)
	{
		Matrix a;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				a.c[i][j] = 0;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				for (int k = 1; k <= n; k++)//求矩阵里的数
				{
					a.c[i][j] += x.c[i][k] * y.c[k][j] % mod;
					a.c[i][j] %= mod;
				}
		return a;
	}
	
	
	int main()
	{
		cin >> n >> k;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				cin >> A.c[i][j];
		for (int i = 1; i <= n; i++)
			I.c[i][i] = 1;
		while (k > 0)//快速幂的模板改一改
		{
			if (k & 1)I = I * A;
			A = A * A;
			k >>= 1;
		}
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++)
				cout << I.c[i][j] << ' ';
			cout << endl;
		}
		return 0;
	
	}
posted @ 2020-04-15 20:13  arbor_one  阅读(265)  评论(0)    收藏  举报