UVA 11149-power of matrix (equal-to-matrix summation)

Source: Internet
Author: User
Tags mul


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< /c12>. 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

Problemsetter:mak Yan Kei



Like the POJ 3233-matrix Power series http://blog.csdn.net/kalilili/article/details/44926947

172 ms #include <cstdio> #include <iostream> #include <cstring> #include <algorithm>using    namespace Std;int n,m;struct mat{int a[45][45];    Mat () {memset (a,0,sizeof (a));    }};mat A;mat i;mat Add (Mat m1,mat m2) {mat ans;    for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) ans.a[i][j]= (m1.a[i][j]+m2.a[i][j])%10; return ans;}    Mat Mul (Mat m1,mat m2) {mat ans;                     for (int i=1;i<=n;i++) for (int. j=1;j<=n;j++) if (M1.a[i][j]) for (int k=1;k<=n;k++)    Ans.a[i][k]= (Ans.a[i][k]+m1.a[i][j]*m2.a[j][k])%10; return ans;}    Mat Quickmul (Mat m,int k) {Mat ans=i;        while (k) {if (k&1) Ans=mul (ans,m);        M=mul (M,M);    k>>=1; } return ans; void print (Mat m) {for (Int. i=1;i<=n;i++) for (int j=1;j<=n;j++) printf ("%d%c", m.a[i][j],j==n? ' \ n ': ');}    Mat getsum (int k) {if (k==1) return A;    Mat Ans=getsum (K/2);   if (k&1) {Mat T=quickmul (a,k/2+1);    Ans=add (Mul (Add (I,t), ans), t);        } else {Mat t=quickmul (A,K/2);    Ans=mul (Add (I,t), ans); } return ans; int main () {while (scanf ("%d%d", &n,&m), N) {for (Int. i=1;i<=n;i++) for (int j=1;j<=n;j+        +) scanf ("%d", &a.a[i][j]), a.a[i][j]%=10;        for (int i=1;i<=n;i++) i.a[i][i]=1;        Mat Ans=getsum (m);        print (ANS);    Puts (""); } return 0;}


UVA 11149-power of matrix (equal-to-matrix summation)

Contact Us

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.