首页 > 代码库 > hdu 4965 Fast Matrix Calculation(矩阵快速幂)

hdu 4965 Fast Matrix Calculation(矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4965


Problem Description
One day, Alice and Bob felt bored again, Bob knows Alice is a girl who loves math and is just learning something about matrix, so he decided to make a crazy problem for her.

Bob has a six-faced dice which has numbers 0, 1, 2, 3, 4 and 5 on each face. At first, he will choose a number N (4 <= N <= 1000), and for N times, he keeps throwing his dice for K times (2 <=K <= 6) and writes down its number on the top face to make an N*K matrix A, in which each element is 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, in which each element is not less than 0 and not greater than 5. With the two 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 but he sees Alice taking it serious, so he also wonders what the answer is. And then Bob turn to you for help because he is not good at math.
 

Input
The input contains several test cases. Each test case starts with two integer N and K, indicating the numbers N and K described above. Then N lines follow, and each line has 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 all the elements in M’ in a 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
 

Author
SYSU
 

Source
2014 Multi-University Training Contest 9


思路:

(这里把题目中的k叙述为m)

由于A是n×m的矩阵,B是m×n的矩阵,而n很大,m很小。
所以我们可以 M = A*(B*A)*(B*A)*....*(B*A)*B
这样我们就可以先用矩阵快速幂求得(B×A)^(n*n-1),然后右乘一个B,再左乘一个A即可。

代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 1017;
#define mod  6
struct Matrix
{
    int mat[6][6];
};
int n, m;//矩阵大小
int A[MAXN][6],B[6][MAXN],C[MAXN][6],D[MAXN][MAXN];
Matrix T, E;
Matrix mul(Matrix a,Matrix b)
{
    Matrix t;
    memset(t.mat,0,sizeof(t.mat));
    for(int i = 0; i < m; i++)//最矩阵大小是m*m的
    {
        for(int j = 0; j < m; j++)
        {
            for(int k = 0; k < m; k++)
            {
                t.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
                t.mat[i][j]%=mod;
            }
        }
    }
    return t;
}
Matrix quickP(int k)
{
    Matrix p = T, mm;
    memset(mm.mat,0,sizeof(mm.mat));
    for(int i = 0; i < m; i++)//单位矩阵
    {
        mm.mat[i][i]=1;
    }
    while(k)
    {
        if(k & 1)
            mm = mul(mm,p);
        p = mul(p,p);
        k >>= 1 ;
    }
    return mm;
}
void init()
{
    memset(T.mat,0,sizeof(T.mat));
    memset(C,0,sizeof(C));
    memset(D,0,sizeof(D));
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n == 0 && m == 0)
            break;
        init();
        for(int i = 0; i < n; i++)//矩阵A的大小n*m
        {
            for(int j = 0; j < m; j++)
            {
                scanf("%d",&A[i][j]);
            }
        }
        for(int i = 0; i < m; i++)//矩阵B的大小m*n
        {
            for(int j = 0; j < n; j++)
            {
                scanf("%d",&B[i][j]);
            }
        }
        for(int i = 0; i < m; i++)//矩阵T = B*A大小为m*m
        {
            for(int j = 0; j < m; j++)
            {
                for(int k = 0; k < n; k++)
                {
                    T.mat[i][j] += B[i][k]*A[k][j];
                    T.mat[i][j] %= mod;
                }
            }
        }

        E = quickP(n*n-1);//只需要乘n*n-1次

        for(int i = 0; i < n; i++)//矩阵C = A*E 大小为n*m
        {
            for(int j = 0; j < m; j++)
            {
                for(int k = 0; k < m; k++)
                {
                    C[i][j] += A[i][k]*E.mat[k][j];
                    C[i][j] %= mod;
                }
            }
        }
        int ans = 0;
        for(int i = 0; i < n; i++)//矩阵D = C*B大小为n*n
        {
            for(int j = 0; j < n; j++)
            {
                for(int k = 0; k < m; k++)
                {
                    D[i][j] += C[i][k]*B[k][j];
                }
                ans += D[i][j]%mod;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}