1142summing up Powers (II)

PDF (中文版) 
Statistics 
Forum 
Time Limit:2 second (s) 
Memory limit:32 MB 
Shanto is learning what to power up numbers and he found an efficient the to find kth power of a matrix. He was quite happy and his discovery. Suddenly his sister Natasha came to him and asked him to find the summation of the powers. To is specific his sister gave the following problem.
Let A is an n x n Matrix. We define Ak = A * A * ... * a (k times). Here, * denotes the usual matrix multiplication. You is to write a program that computes the matrix A + A2 + A3 + ... + Ak.
Shanto smiled and thought that it would is a easy one. But after a and he found that it's tough for him. Can you help him?
Input
Input starts with an integer T (≤20), denoting the number of test cases.
Each case is starts with the integers n (1≤n≤30) and K (1≤k≤109). Each of the next n lines would contain n nonnegative integers (not greater than ).
Output
For each case, print the case number and the result matrix. For each cell, just print the last digit. See the samples for more details.
Sample Input 
Output for Sample Input 
2 3 2 1 4 6 6 5 2 1 2 3 3 10 1 4 6 6 5 2 1 2 3 
Case 1: 208 484 722 Case 2: 868 620 546 
Problem Setter:jane ALAM JAN
1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <stdlib.h>5#include <string.h>6#include <math.h>7#include <queue>8 using namespacestd;9typedefLong LongLL;Tentypedefstructnode One { A intm[ ][ ];  node ()  { theMemset (M,0,sizeof(m));  }  } maxtr;  intans[ ][ ]; + voidE (Node *nn,intn); Maxtr Ju (intn); +MAXTR Quick (node Ju,intNintm); A intMainvoid) at {  inti,j,k;  intn,m;  ints; Cin>>K;  for(s=1; s<=k; s++) in { scanf"%d%d",&n,&m); to for(i=0; i<n; i++) + {  for(j=0; j<n; J + +) the { *scanf"%d",&ans[i][j]); $ans[i][j]%=Ten;Panax Notoginseng } }node aa;aa=ju (n); theAa=quick (aa,n,m);p rintf ("Case %d:\n", s); + for(i=0; i<n;i++) A { the for(j=n;j<2*n;j++) + { printf"%d", Aa.m[i][j]); $}printf ("\ n"); $ } }return 0;  } the voidE (Node *nn,intN)  {Wuyi inti,j,k; the for(i=0; i<n; i++)  { Wu for(j=0; j<n; J + +)  { About if(i==j) $nn>m[i][j]=1;  Elsenn>m[i][j]=0;  }  } A } +Maxtr Ju (intN) the {  inti,j,k; $ maxtr nn; the for(i=0; i<n; i++) the { the for(j=0; j<n; J + +) the { nn.m[i][j]=Ans[i][j]; in } the } the for(i=0; i<n; i++) About { the for(J=n; j<2*n; J + +) the { thenn.m[i][j]=ans[i][jn]; + }  } the node cc;BayiE (&cc,n); the for(I=n; i<2*n; i++) the {  for(J=n; j<2*n; J + +)  { thenn.m[i][j]=cc.m[in][jn]; the } the}returnnn; the } MAXTR Quick (node Ju,intNintm) the {node ee; the theE (&ee,2*n);94 inti,j,k; the ints; the while(m) the {98 if(m&1) About {  node cc;101 for(i=0; i<2*n; i++)102 {103 for(j=0; j<2*n; J + +)104 { the for(s=0; s<2*n; s++)106 {107Cc.m[i][j]= (Ju.m[i][s]*ee.m[s][j]+cc.m[i][j])%Ten;108 }109 } the }111Ee=cc; the }113 node cc; the for(i=0; i<2*n; i++) the { the for(j=0; j<2*n; J + +)117 {118 for(s=0; s<2*n; s++)119 { Cc.m[i][j]= (Ju.m[i][s]*ju.m[s][j]+cc.m[i][j])%Ten;121 }122 }123 }124ju=cc; theM/=2;126 }127 returnee; }
1142summing up Powers (II)