http://poj.org/problem?id=3233
题解:
构造矩阵:
a:A 1
b:A 0
A 1
其中
矩阵套矩阵;
A代表原矩阵;
1代表单位矩阵,即对角线上元素为1,其余为0;
0代表全部为0;
/*
*@Author: STZG
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int s[N][N];
int b[N][N];
int a[N][N];
char str;
struct node{};
void Matrix(int a[N][N],int b[N][N]){
int c[N][N];
memset(c,0,sizeof(c));
for(int i=0;i<2*n;i++){
for(int j=0;j<2*n;j++){
for(int k=0;k<2*n;k++){
c[i][j]=(c[i][j]+a[i][k]*b[k][j])%m;
}
}
}
for(int i=0;i<2*n;i++){
for(int j=0;j<2*n;j++){
a[i][j]=c[i][j];
}
}
}
void power(int k){
for(int i=0;i<2*n;i++){
for(int j=0;j<2*n;j++){
a[i][j]=(i==j);
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
b[i+n][j+n]=(i==j);
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
b[i][j]=b[i+n][j]=s[i][j];
}
}
while(k){
if(k&1)Matrix(a,b);
Matrix(b,b);
k>>=1;
}
}
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
//
while(~scanf("%d%d%d",&n,&k,&m)){
memset(s,0,sizeof(s));
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf("%d",&s[i][j]);
s[i][j]%=m;s[i][j+n]=(i==j);
}
}
power(k-1);
Matrix(s,a);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
printf("%d%c",s[i][j]," \n"[j==n-1]);
}
}
}
#ifdef DEBUG
printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
//cout << "Hello world!" << endl;
return 0;
}