hdu 4965 Fast Matrix Calculation(数学)

31 篇文章 0 订阅

题目:

Fast Matrix Calculation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1107    Accepted Submission(s): 531


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

题意;给一个n*k的矩阵A和一个k*n的矩阵B,求(AB)^(n*n),直接求的话由于AB是1000*1000的,会爆掉内存,所以我们转化一下,注意到矩阵乘法有结合律,所以可以转化为A*(BA)^(n*n-1)*B,而BA只有6*6,就可以愉快的用快速幂了。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
struct matrix
{int m[10][10];
void init()
{memset(m,0,sizeof(m));
}
};
int n,k;
matrix mul(matrix a,matrix b)
{matrix c;
c.init();
for(int p=0;p<k;p++)
for(int i=0;i<k;i++)
for(int j=0;j<k;j++)
{ c.m[i][j]=(c.m[i][j]+a.m[i][p]*b.m[p][j])%6;
}
return c;
}
matrix mypow(matrix a,int q)
{matrix tmp=a;
matrix ans;
ans.init();
for(int i=0;i<k;i++)
ans.m[i][i]=1;
while(q)
{if(q&1)
ans=mul(ans,tmp);
tmp=mul(tmp,tmp);
q>>=1;
}
return ans;
}
int A[1005][10];
int B[10][1005];
int D[1005][10];
int E[1005][1005];
int main()
{while(scanf("%d%d",&n,&k)!=EOF)
{if(!n&&!k)
break;
memset(A,0,sizeof(A));
memset(B,0,sizeof(B));
memset(D,0,sizeof(D));
memset(E,0,sizeof(E));
for(int i=0;i<n;i++)
for(int j=0;j<k;j++)
scanf("%d",&A[i][j]);
for(int i=0;i<k;i++)
for(int j=0;j<n;j++)
scanf("%d",&B[i][j]);
matrix C;
C.init();
for(int p=0;p<n;p++)
for(int i=0;i<k;i++)
for(int j=0;j<k;j++)
C.m[i][j]=(C.m[i][j]+B[i][p]*A[p][j])%6;
matrix M=mypow(C,n*n-1);

for(int p=0;p<k;p++)
for(int i=0;i<n;i++)
for(int j=0;j<k;j++)
D[i][j]=(D[i][j]+A[i][p]*M.m[p][j])%6;

for(int p=0;p<k;p++)
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
E[i][j]=(E[i][j]+D[i][p]*B[p][j])%6;
int ans=0;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
ans+=E[i][j];
printf("%d\n",ans);
}
return 0;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值