UVA 11149 Power of Matrix

Source: Internet
Author: User

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

For each case, 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


Note that K is even when there is A1 + a^2 + a^3 + .... A^k = (E + a^ (K/2) (a^1+a^2+a^3+... A ^ k/2)
To K is odd when the last think A^k alone to propose the remaining as an even
DFS can
#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib>using namespace std; #define MAXN 50int n,k;struct node{int mat[maxn][maxn];};    Node CALCU (node x, node Y) {node ret;    memset (ret.mat,0,sizeof (Ret.mat));            for (int i = 1, i <= N; i++) for (int j = 1; J <= N; j + +) {for (int k = 1; k <= N; k++)    RET.MAT[I][J] = (Ret.mat[i][j] + x.mat[i][k] * y.mat[k][j])% 10; } return ret;}    Node Add (node X,node y) {node ret;    memset (ret.mat,0,sizeof (Ret.mat)); for (int i = 1, i <= N; i++) for (int j = 1; J <= N; j + +) {Ret.mat[i][j] = X.mat[i][j] + Y.mat I           [j];   RET.MAT[I][J]%= 10; } return ret;}    Node Pow_mat (node X,int cnt) {node ret;    memset (ret.mat,0,sizeof (Ret.mat));    for (int i = 1; i < MAXN; i++) ret.mat[i][i] = 1;        while (CNT) {if (CNT & 1) ret = CALCU (ret,x);        x = CALCU (x,x);    CNT >>= 1; } return ret;} Node DFS (node cur, int k) {if (k = = 1) return cur;    Node res = DFS (CUR,K/2);    Node ans;    Ans = Add (RES,CALCU (Res,pow_mat (CUR,K/2)));    if (K & 1) ans = Add (Ans,pow_mat (cur,k)); return ans;}        int main () {while (scanf ("%d%d", &n,&k)! = EOF) {if (n = = 0) break;        Node ans; for (int i = 1; I <= n; i++) for (int j = 1; J <= N; j + +) {scanf ("%d", &ans.mat[i][j]); ans.mat[        I][J]%= 10;}        node ret = DFS (ans,k);                for (int i = 1; I <= n; i++) {printf ("%d", ret.mat[i][1]);                for (int j = 2; J <= N; j + +) printf ("%d", ret.mat[i][j]);        Putchar (' \ n ');    } putchar (' \ n '); } return 0;}

  

UVA 11149 Power of Matrix

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.