首页 > 代码库 > hdu 4965 Fast Matrix Calculation(矩阵快速幂)2014多校训练第9场

hdu 4965 Fast Matrix Calculation(矩阵快速幂)2014多校训练第9场

Fast Matrix Calculation

                                                                  Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

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
 
比赛时没有转化,就按照题目描述的方法求,结果一直超时,各种优化后还是超时。看了题解后才明白要转化一下。
题意:给出一个矩阵n*k的矩阵A和一个k*n的矩阵B,矩阵C=A*B,然后求C^(n*n),对新矩阵里的每一个元素对6取余后,求新矩阵里所有元素的和。
分析:如果直接算A*B的话,得到的是1000*1000的矩阵,所以会一直超时。因为C^(n*n)=(A*B)^(n*n)=A*(B*A)^(n*n-1)*B,由于B*A是6*6的矩阵,再用矩阵快速幂来求就行了。
#include<cstdio>
#include<algorithm>
using namespace std;

const int N = 1005;
#define mod 6
struct Matrix
{
    int mat[10][10];
} ;
Matrix unit_matrix, c;
int A[N][10], B[10][N], aa[N][10], bb[N][N];
int n, k;

Matrix mul(Matrix a, Matrix b) //矩阵相乘
{
    Matrix res;
    for(int i = 0; i < k; i++)
        for(int j = 0; j < k; j++)
        {
            res.mat[i][j] = 0;
            for(int t = 0; t < k; t++)
            {
                res.mat[i][j] += a.mat[i][t] * b.mat[t][j];
                res.mat[i][j] %= mod;
            }
        }
    return res;
}

Matrix pow_matrix(Matrix a, int m)  //矩阵快速幂
{
    Matrix res = unit_matrix;
    while(m != 0)
    {
        if(m & 1)
            res = mul(res, a);
        a = mul(a, a);
        m >>= 1;
    }
    return res;
}

int main()
{
    int  i, j, t;
    while(~scanf("%d%d",&n,&k) && (n+k))
    {
        for(i = 0; i < n; i++)
            for(j = 0; j < k; j++)
               scanf("%d",&A[i][j]);
        for(i = 0; i < k; i++)
            for(j = 0; j < n; j++)
                scanf("%d",&B[i][j]);

        //初始化单位矩阵
        for(i = 0; i < k; i++)
            for(j = 0; j < k; j++)
                unit_matrix.mat[i][j] = 0;
        for(i = 0; i < n; i++)
            unit_matrix.mat[i][i] = 1;

        for(i = 0; i < k; i++) //求B*A
        {
            for(j = 0; j < k; j++)
            {
                c.mat[i][j] = 0;
                for(t = 0; t < n; t++)
                {
                    c.mat[i][j] += B[i][t] * A[t][j];
                    c.mat[i][j] %= mod;
                }
            }
        }

        Matrix ans = pow_matrix(c, n*n-1);

        for(i = 0; i < n; i++)
        {
            for(j = 0; j < k; j++)
            {
                aa[i][j] = 0;
                for(t = 0; t < k; t++)
                {
                    aa[i][j] += A[i][t] * ans.mat[t][j];
                    aa[i][j] %= mod;
                }
            }
        }

        for(i = 0; i < n; i++)
        {
            for(j = 0; j < n; j++)
            {
                bb[i][j] = 0;
                for(t = 0; t < k; t++)
                {
                    bb[i][j] += aa[i][t] * B[t][j];
                    bb[i][j] %= mod;
                }
            }
        }

        int sum = 0;
        for(i = 0; i < n; i++)
            for(j = 0; j <n; j++)
                sum += bb[i][j];
        printf("%d\n", sum);
    }
    return 0;
}