Fast Matrix Calculation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1700 Accepted Submission(s): 784
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.
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.
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
题目大意:
给你两个矩阵,让你先把他们乘起来再算幂,最后求各个位置上的元素之和。
解题思路:
之间按照题目要求计算即可。不过有一点需要注意,最坏情况下A是一个1000*6的矩阵,B是一个6*1000的矩阵,如果直接算(A*B)^N*N的话是对一个1000*1000的矩阵快速幂,会超时。转化成A*(B*A)^(N*N-1)*B就成了一个6*6的矩阵快速幂,速度就非常快了。
AC代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <string>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define fi first
#define se second
#define mem(a,b) memset((a),(b),sizeof(a))
#define vec vector<int>
#define mat vector<vec>
const int MOD=6;
int N,K;
mat mul(mat &A, mat &B)
{
mat C(A.size(),vec(B[0].size()));
for(int i=0;i<A.size();++i)
for(int k=0;k<B.size();++k)
for(int j=0;j<B[0].size();++j)
C[i][j]=(C[i][j]+A[i][k]*B[k][j])%MOD;
return C;
}
mat pow(mat A,int n)
{
mat B(A.size(),vec(A.size()));
for(int i=0;i<A.size();++i)
B[i][i]=1;
while(n>0)
{
if(n&1)
B=mul(B,A);
A=mul(A,A);
n>>=1;
}
return B;
}
int main()
{
while(~scanf("%d%d",&N,&K)&&(N||K))
{
mat A(N,vec(K)),B(K,vec(N));
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]);
mat C(K,vec(K));
C=pow(mul(B,A),N*N-1);
mat res(N,vec(N));
res=mul(A,C);
res=mul(res,B);
int ans=0;
for(int i=0;i<N;++i)
for(int j=0;j<N;++j)
ans+=res[i][j];
printf("%d\n",ans);
}
return 0;
}