HDUOJ 4965 Fast Matrix Calculation

纵饮孤独 提交于 2020-03-06 11:03:30

HDUOJ 4965 Fast Matrix Calculation

题目链接

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 NK 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 KN 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 NN matrix C = AB.
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

直接算 (AB)nn{(A*B)^{n*n}} 的话,由于 CCnn 阶的,而 nn 最大有1000,此时会爆栈,且用 nn 算矩阵乘法的话,就是 O(N3)O(N^3) ,也会超时,此时就要换个角度,(AB)nn=ABAB...AB=A(BAB...A)B=A(BA)nn1B(A*B)^{n*n}=A*B*A*B...A*B=A*(B*A*B...A)*B=A*(B*A)^{n*n-1}*B,此时 nn 阶矩阵就转换为 kk 阶,套一个矩阵快速幂即可,AC代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=6;
struct mat
{
    ll m[10][10];
}ans,c;

ll a[1005][15],b[15][1005],s1[1005][1005],s2[1005][1005],sum;
int n,m;
mat mul(mat a,mat b)
{
    mat tmp;
    memset(tmp.m,0,sizeof(tmp.m));
    for(int i=0;i<m;i++)
        for(int j=0;j<m;j++)
            for(int k=0;k<m;k++)
             {
                 tmp.m[i][j]=(tmp.m[i][j]+a.m[i][k]*b.m[k][j])%mod;
                 if(tmp.m[i][j]<0) tmp.m[i][j]=(tmp.m[i][j]+mod)%mod;
             }
     return tmp;
}

void mat_pow(mat a,ll k)
{
    memset(ans.m,0,sizeof(ans.m));
    for(int i=0;i<m;i++) ans.m[i][i]=1;
    while(k>0)
    {
        if(k&1) ans=mul(a,ans);
        a=mul(a,a);
        k>>=1;
    }
}

int main(){
    while(cin>>n>>m && (n||m)){
        sum=0;
        mat c;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++)
                cin>>a[i][j];
        }
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++)
                cin>>b[i][j];
        }
        memset(c.m,0,sizeof(c.m));
        for(int i=0;i<m;i++)
            for(int j=0;j<m;j++)
                for(int k=0;k<n;k++)
                    c.m[i][j]=(c.m[i][j]+b[i][k]*a[k][j])%mod;
        mat_pow(c,n*n-1);
        memset(s1,0,sizeof(s1));
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++)
                for(int k=0;k<m;k++)
                s1[i][j]=(s1[i][j]+a[i][k]*ans.m[k][j])%mod;
        }
        memset(s2,0,sizeof(s2));
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++)
                for(int k=0;k<m;k++)
                s2[i][j]=(s2[i][j]+s1[i][k]*b[k][j])%mod;
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++)
               sum+=s2[i][j]%mod;
        }
        cout<<sum<<endl;
    }
    return 0;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!