hdu4965---Fast matrix calculation (matrix)

Source: Internet
Author: User

Problem Description
One day, Alice and Bob felt bored again, Bob knows Alice was a girl who loves math and are just learning something about mat Rix, so he decided-make a crazy problem for her.

Bob has a six-faced dice which have numbers 0, 1, 2, 3, 4 and 5 on each face. At first, he'll choose a number N (4 <= n <=), and for N times, he keeps throwing the He dice for K times (2 < ; =k <= 6) and writes down their number on the top face to make an n*k matrix A, in which each element was not less than 0 And not greater than 5. Then he does similar thing again with a bit difference:he keeps throwing his dice for N times and each time repeat it for K Times to write down a k*n matrix B, with which each element was not less than 0 and not greater than 5. With the both Matrix A and B formed, Alice's task is to perform the following 4-step calculation.

Step 1:calculate a new n*n matrix C = a*b.
Step 2:calculate M = c^ (n*n).
Step 3:for each element x in M, calculate x% 6. All the remainders form a new matrix M '.
Step 4:calculate The sum of all the elements in M '.

Bob just made this problem for kidding and he sees Alice taking it serious, so he also wonders what's the answer is. And then Bob turn-to-because he is not good at math.

Input
The input contains several test cases. Each test case is starts with the integer n and K, indicating the numbers n and K described above. Then N lines follow, and each of the line have K integers between 0 and 5, representing Matrix A. Then K lines follow, and each line has N integers between 0 and 5, representing Matrix B.

The end of input is indicated by N = K = 0.

Output
For each case, output the sum of the "elements in M '" line.

Sample Input

4 2 5 5 4 4 5 4 0 0 4 2 5 5 1 3 1 5 6 3 1 2 3 0 3 0 2 3 4 4 3 2 2 5 5 0 5 0 3 4 5 1 1 0 5 3 2 3 3 2 2 0 0

Sample Output

14 56

Author
Sysu

Source
Multi-university Training Contest 9

Recommend
We have carefully selected several similar problems for you:5189 5188 5186 5185 5184

Directly according to the practice of test instructions, make a 1000*1000 matrix, light once multiplication time out

Notice that C = a*b
c * C * ....c = A(b*a) (b*a) ..... *b
Where B*a is a k*k matrix, do n^2-1 times of the fast power, so that the time to control down

/************************************************************************* > File Name:hdu4965.cpp > Auth Or:alex > Mail: [email protected] > Created time:2015 March 16 Monday 17:51 56 seconds ******************************** ****************************************/#include <map>#include <set>#include <queue>#include <stack>#include <vector>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;inta[1010][Ten];intb[Ten][1010];intd[1010][1010];intans[1010][1010];intKclassmartix{ Public:intmat[Ten][Ten]; Martix () {memset(Mat,0,sizeof(MAT)); } Martixoperator* (ConstMartix &b)Const; martix&operator= (ConstMartix &b);}; Martix Martix::operator* (ConstMartix &b)Const{Martix C; for(inti =0; i < K; ++i) { for(intj =0; J < K; ++J) {C.mat[i][j] =0; for(intK =0; K < K; ++K) {C.mat[i][j] + = ThisMAT[I][K] * B.mat[k][j]; C.MAT[I][J]%=6; }        }    }returnC;} martix& Martix::operator= (ConstMartix &b) { for(inti =0; i < K; ++i) { for(intj =0; J < K; ++J) { ThisMAT[I][J] = B.mat[i][j]; }    }return* This;} Martix Fastpow (Martix A,intN) {Martix ans; for(inti =0; i < K; ++i) {Ans.mat[i][i] =1; } while(n) {if(N &1) {ans = ans * A; } N >>=1;    A = a * A; }returnAns;}intMain () {intN while(~scanf("%d%d", &n, &k)) {if(!n &&!) K) { Break; } for(inti =0; I < n; ++i) { for(intj =0; J < K; ++J) {scanf("%d", &a[i][j]); }        } for(inti =0; i < K; ++i) { for(intj =0; J < N; ++J) {scanf("%d", &b[i][j]); }} Martix C; for(inti =0; i < K; ++i) { for(intj =0; J < K; ++J) { for(intK =0; K < n;                    ++K) {C.mat[i][j] + = b[i][k] * A[k][j]; C.MAT[I][J]%=6; }}} C = Fastpow (c, n * N-1); for(inti =0; I < n; ++i) { for(intj =0; J < K; ++J) {D[i][j] =0; for(intK =0; K < K;                    ++K) {D[i][j] + = a[i][k] * C.mat[k][j]; D[I][J]%=6; }            }        }intres =0; for(inti =0; I < n; ++i) { for(intj =0; J < N; ++J) {Ans[i][j] =0; for(intK =0; K < K;                    ++K) {Ans[i][j] + = d[i][k] * B[k][j]; ANS[I][J]%=6;             } res + = Ans[i][j]; }        }printf("%d\n", res); }return 0;}

hdu4965---Fast matrix calculation (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.