矩阵快速幂模板求助
查看原帖
矩阵快速幂模板求助
47911
xiaozhangma楼主2022/11/21 21:12

看了几遍没发现问题,long long也都开了。 第一次写这个东西QAQ。

//code by xiaozhangma
#include<bits/stdc++.h>
#define LL long long
#define F(x,s,t) for(int x=s;x<=t;x++)
using namespace std;
LL read(){
	LL x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
const int N = 110;
const int MOD = 1e9 + 10;
LL n,k;
struct Info{
	LL c[N][N];
}A, I;
Info operator * (const Info &x,const Info &y){
	Info a;
	F(i, 1, n){
		F(j, 1, n){
			a.c[i][j] = 0;
		}
	}
	F(i, 1, n){
		F(j, 1, n){
			F(k, 1, n){
				a.c[i][j] += (x.c[i][k] * y.c[k][j] % MOD);
				a.c[i][j] %= MOD;
			}
		}
	}
	return a;
}
Info qmi(Info x,int k){
	Info res = I;
	while(k){
		if(k&1)res = res * x;
		x = x * x;
		k >>= 1;
	}
	return res;
}
int main(){
	n = read(),k = read();
	F(i, 1, n){
		F(j, 1, n){
			A.c[i][j] = read();
		}
	}
	
	F(i, 1, n)I.c[i][i] = 1;
	
	Info ans = qmi(A, k);
	
	F(i, 1, n){
		F(j, 1, n){
			printf("%lld ",ans.c[i][j]);
		}
		puts("");
	}
	return 0;
}
2022/11/21 21:12
加载中...