Fast Matrix Calculation(hdu 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
1.如果先计算 A*B的矩阵,然后再快速幂,那么矩阵最大可达: 1 e 3 ∗ 1 e 3 1e3*1e3 1e31e3,计算量是十分庞大的。

  1. ( A ∗ B ) n ∗ n = A ∗ B ∗ A ∗ B ∗ A ∗ B ∗ A ∗ B … … ∗ A ∗ B = A ∗ ( B ∗ A ) n ∗ n − 1 ∗ B {(A*B)}^{n*n}= A*B*A*B*A*B*A*B……*A*B = A* {(B*A)}^{n*n-1}*B (AB)nn=ABABABABAB=A(BA)nn1B,其中 B ∗ A B*A BA最大只为 6 ∗ 6 6*6 66,因而可先用矩阵快速幂算出 ( B ∗ A ) n ∗ n − 1 {(B*A)}^{n*n-1} (BA)nn1,然后再计算 A ∗ ( B ∗ A ) n ∗ n − 1 ∗ B A* {(B*A)}^{n*n-1}*B A(BA)nn1B
#include <iostream>
#include<algorithm>
#include <stdio.h>
#include <string>
#include <string.h>
#include <map>
#include <math.h>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <deque>
//#include <bits/stdc++.h>
using namespace std;
typedef  long long ll;
int n,k;
struct sa
{
    int m[6][6];
};
sa Mul(sa a,sa b)//矩阵乘法
{
    sa c;
    memset(c.m,0, sizeof(c.m));
    for(int i=0;i<k;i++)
        for(int j=0;j<k;j++)
            for(int l=0;l<k;l++)
                c.m[i][j]=((c.m[i][j]+(a.m[i][l]*b.m[l][j])%6));
    return c;
}
sa quickjz(sa a,int n)//矩阵快速幂
{
    sa res;
    memset(res.m,0, sizeof(res.m));
    for(int i=0;i<k;i++)
        res.m[i][i]=1;
    while(n)
    {
        if(n&1)
            res=Mul(res,a);
        n>>=1;
        a=Mul(a,a);
    }
    return res;
}
int p[1001][1001],q[1001][1001],r[1001][1001];
sa x;
int main()
{
    while(scanf("%d %d",&n,&k)!=-1)
    {
        if(n==0&&k==0)
            break;
        memset(r,0, sizeof(r));
        for(int i=0;i<n;i++)
            for(int j=0;j<k;j++)
                scanf("%d",&p[i][j]);
            for(int i=0;i<k;i++)
                for(int j=0;j<n;j++)
                    scanf("%d",&q[i][j]);
                for(int i=0;i<k;i++)
                    for(int j=0;j<k;j++)
                        for(int l=0;l<n;l++)
                        {
                            r[i][j]+=(q[i][l]*p[l][j])%6;//     得到B*A
                        }
                for(int i=0;i<k;i++)
                    for(int j=0;j<k;j++)
                        x.m[i][j]=r[i][j]%6;
                    x=quickjz(x,n*n-1);              //    x= (B*A)^(n*n-1)
        memset(r,0, sizeof(r));
        for(int i=0;i<n;i++)
            for(int j=0;j<k;j++)
                for(int l=0;l<k;l++)
                {
                    r[i][j]+=(p[i][l]*x.m[l][j])%6;    //r=A*x;
                }
        memset(p,0, sizeof(p));
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                for(int l=0;l<k;l++)
                {
                    p[i][j]+=(r[i][l]*q[l][j])%6;//  p=r*B;
                }
                    int ans=0;
                    for(int i=0;i<n;i++)
                        for(int j=0;j<n;j++)
                            ans+=p[i][j]%6;

                        cout<<ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值