Hdu4965--fast matrix calculation (Fast Matrix power optimization)

Source: Internet
Author: User
Tags mul

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
3 1 5 4 5 2
0 0

Sample Output
14
56

At least two points were noted when looking at the topic:
One is that the scale of the matrix has 1000*1000, too large, and another number m too small, suspicious
Second, the steps are written too detailed, it is not possible to follow the direct walk can be
However, I still cannot control myself to make the hair to explode the stack ....

The topic is the request (A*B) ^ (n*n), is actually a*b*a*b*a*b*a*b*a*b ..., the middle of the b*a proposed to become a A * (b*a) ^ (n*n-1) *b, and B*a is the matrix of 6*6, operation time dropped down a lot

#include <iostream> #include <cstring> #include <string> #include <vector> #include <queue > #include <cstdio> #include <set> #include <math.h> #include <algorithm> #include <queue&
Gt
#include <iomanip> #define INF 0x3f3f3f3f #define MAXN 10000005 #define MOD 10000007 using namespace std;
const int N = 10;
Long Long m,n;
struct Matrix {int mat[n][n];};
    Matrix Mul (Matrix A,matrix b) {matrix res;
            for (int i=0, i<m; ++i) for (int j=0; j<m; ++j) {res.mat[i][j]=0;
                for (int k=0; k<m; ++k) {res.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
            res.mat[i][j]%=6;
}} return res;
    } Matrix Pow_matrix (Matrix A,long long k) {matrix res;
    memset (res.mat,0,sizeof (Res.mat));
    for (int i=0; i<m; ++i) res.mat[i][i]=1;
        while (k!=0) {if (k%2) Res=mul (res,a);
   A=mul (A,a);     k>>=1;

} return res;
} int a[1005][1005],b[1005][1005],c1[1005][1005],c2[1005][1005];
    int main () {Matrix tmp;
        while (~SCANF ("%i64d%i64d", &n,&m)) {if (n==0&&m==0) break;
        memset (tmp.mat,0,sizeof (Tmp.mat));
        for (int. i=0; i<n; ++i) for (int j=0; j<m; ++j) scanf ("%d", &a[i][j]);
        for (int. i=0; i<m; ++i) for (int j=0; j<n; ++j) scanf ("%d", &b[i][j]);
                for (int i=0, i<m; ++i) for (int j=0; j<m; ++j) {tmp.mat[i][j]=0;
                    for (int k=0; k<n; ++k) {tmp.mat[i][j]+=b[i][k]*a[k][j];
                tmp.mat[i][j]%=6;
        }} Matrix P=pow_matrix (tmp,n*n-1);
                for (int i=0, i<n; ++i) for (int j=0; j<m; ++j) {c1[i][j]=0; for (int k=0; k<m;++K) {c1[i][j]+=a[i][k]*p.mat[k][j];
                c1[i][j]%=6; }} for (int i=0, i<n; ++i) for (int j=0; j<n; ++j) {c2[i
                ][j]=0;
                    for (int k=0; k<m; ++k) {c2[i][j]+=c1[i][k]*b[k][j];
                c2[i][j]%=6;
        }} long long ans=0;
        for (int i=0, i<n; ++i) for (int j=0; j<n; ++j) ans+=c2[i][j];
    printf ("%i64d\n", ans);
} return 0;
 }

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.