Untitled
unknown
plain_text
6 months ago
947 B
2
Indexable
Never
#include<iostream> using namespace std; struct matrix{ int ma[100][100]; }; int t,n,k; matrix mat; matrix kq; matrix product(matrix a, matrix b){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++) kq.ma[i][j]=0; } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ for(int k=0;k<n;k++){ kq.ma[i][j] += a.ma[i][k]*b.ma[k][j]; } } } return kq; } matrix luythua(matrix base, int sm){ if(sm==1) return base; else{ if(sm%2==0) return product(luythua(base,sm/2),luythua(base,sm/2)); else{ return product(base,product(luythua(base,sm/2),luythua(base,sm/2))); } } } int main(){ freopen("input.txt","r",stdin); cin >> t; for(int tc=1;tc<=t;tc++){ cin >> n >> k; for(int i=0;i<n;i++){ for(int j=0;j<n;j++) cin >> mat.ma[i][j]; } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ cout << luythua(mat,k).ma[i][j] << " " ; } cout << endl; } } return 0; }
Leave a Comment