爆零求助
查看原帖
爆零求助
221233
newsheep楼主2020/6/14 11:56
#include <cstdio>
#include <cstring>
#define MAXN 105
#define LL long long
using namespace std;

int n;
LL k, p = 1000000007;

LL read() {
	bool f = false; LL x = 0; char ch = getchar();
	while ('0'>ch || ch>'9') {if (ch == '-') f = true; ch = getchar();};
	while ('0'<=ch && ch<='9') {x=(x<<3) + (x<<1) + ch - 48, ch = getchar();};
	return f ? -x : x;
}

struct Matrix {
	LL g[MAXN][MAXN];

	void unit() {
		for (int i = 1; i <= n; ++i) g[i][i] = 1;
	}//单位矩阵 
	
	Matrix operator *(const Matrix &x) const {
		Matrix z;
		for (int k = 1; k <= n; ++k)
			for (int i = 1; i <= n; ++i)
				for (int j = 1; j <= n; ++j)
					(z.g[i][j] += g[i][k] * x.g[k][j]) %= p;
		return z;
	}//重载乘法 
	
	void power(Matrix &a, LL b) {
//		Matrix c; c.unit();
		Matrix c = a; b--;
		for (; b; b >>= 1, a = a * a)
			if (b & 1) c = c * a;
		a = c;
	}//幂运算 
} a;

int main() {
//	freopen("C:\\Users\\Administrator\\Desktop\\P3390_1.in","r",stdin);
//	freopen("C:\\Users\\Administrator\\Desktop\\P3390.out","w",stdout);
	n = read(); k= read();
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= n; ++j)
			a.g[i][j] = read();
	a.power(a, k);
	for (int i = 1; i <= n; printf("\n"), ++i)
		for (int j = 1; j <= n; ++j)
			printf("%lld ", a.g[i][j]);
	return 0;
}

求大佬指错……

以及还有一个疑问,为什么幂运算中用单位矩阵乘上 aka^k 得到的结果不同于用 aak1a*a^{k-1},是我理解错了矩阵的运算吗qaq

2020/6/14 11:56
加载中...