# 1142-summing up Powers (II)

Source: Internet
Author: User

1142-summing 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 non-negative 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 23 21 4 66 5 21 2 33 101 4 66 5 21 2 3 Case 1:208484722Case 2:868620546

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][j-n]; +         } -     } 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[i-n][j-n]; 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; -}`

1142-summing up Powers (II)

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.