Power of MatrixTime
limit:3000MS
Memory Limit:0KB
64bit IO Format:%lld &%llu SubmitStatusPracticeUVA 11149Appoint Description:System Crawler (2015-03-15)Description
Problem B:power of Matrix |
Time Limit:10 seconds |
COnsider an n-by-n Matrix A. 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 + ... + a k.
Example
Suppose A =. Then A2 = =, thus:
Such computation has various applications. For instance, the above example actually counts all the paths in the following graph:
Input
Input consists of no more than test cases. The first line is contains, positive integers n (≤40) and K (≤1000000). This was followed by n lines, each containing n non-negative integers, giving the matrix A.
Input is terminated by a case where n = 0. This case is need not being processed.
Output
Your program should compute the matrix a + a2 + a3 + ... + a k. Since The values may is very large, you have need to print their last digit. Print a blank line after each case.
Sample Input
3 20 2 00 0 20 0 00 0
Sample Output
0 2 40 0 20 0 0
First, let's think about calculating a+a^2+a^3...+a^k.if a=2,k=6. Then how do you count2+22+23+24+25+26 =? =(2+22+23) * (1+23)
if a=2,k=7. Then how do you count2+22+23+24+25+26+27 =? = (2+22+23) * (1+23) +27 so .... Similarly: When K is even, a+a^2+a^3...+a^k= (e+a^ (K/2)) * (a+a^2...+a^ (K/2)). When k is odd, a+a^2+a^3...+a^k= (e+a^ (K/2)) * (a+a^2...+a^ (K/2)) +a^k.
Reprint Please specify the Source: Search & Star ChildrenTopic Link:UVA 11149
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;#defineLL __int64#defineMmax 45structmatrix{intMat[mmax][mmax];};intN;matrix Multiply (Matrix A,matrix b) {matrix C; memset (C.mat,0,sizeof(C.mat)); for(intI=0; i<n; i++) { for(intj=0; j<n; J + +) { if(a.mat[i][j]==0)Continue; for(intk=0; k<n; k++) { if(b.mat[j][k]==0)Continue; C.MAT[I][K]= (C.mat[i][k]+a.mat[i][j]*b.mat[j][k])%Ten; } } } returnC;} Matrix Quickmod (Matrix A,intN) {Matrix res; for(intI=0; i<n; i++)//Unit Array for(intj=0; j<n; J + +) Res.mat[i][j]= (i==j); while(n) {if(n&1) Res=multiply (a,res); A=multiply (a,a); N>>=1; } returnRes;} Matrix Add (Matrix A,matrix b) {matrix ret; for(intI=0; i<n; i++) for(intj=0; j<n; J + +) Ret.mat[i][j]= (A.mat[i][j]+b.mat[i][j])%Ten; returnret;} Matrix Solve (matrix A,intk) { if(k==1)returnA; Matrix ans; for(intI=0; i<n; i++) for(intj=0; j<n; J + +) Ans.mat[i][j]= (i==j); if(k==0)returnans; Ans=multiply (Add (Quickmod (A, (k>>1), ans), solve (A, (k>>1))); if(k%2) ans=Add (Quickmod (A,k), ans); returnans;}intMain () {intK; while(SCANF ("%d%d", &n,&k)! =EOF) { if(! N Break; Matrix ans; for(intI=0; i<n;i++) { for(intj=0; j<n;j++) { inttemp; scanf ("%d",&temp); ANS.MAT[I][J]=temp%Ten; }} ans=solve (ans,k); for(intI=0; i<n;i++) { for(intj=0; j<n-1; j + +) {printf ("%d", Ans.mat[i][j]); } printf ("%d\n", ans.mat[i][n-1]); } printf ("\ n"); } return 0;}
Power of matrix (uva11149+ matrix fast Power)