矩阵快速幂-Fast Matrix Calculation HDU - 4965
题目:
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
题意:
输
入
矩
阵
的
行
N
(
4
<
=
N
<
=
1000
)
,
矩
阵
的
列
K
(
2
<
=
K
<
=
6
)
输入矩阵的行N(4<=N<=1000),矩阵的列K(2<=K<=6)
输入矩阵的行N(4<=N<=1000),矩阵的列K(2<=K<=6)
输
入
N
×
K
的
矩
阵
A
输入N×K的矩阵A
输入N×K的矩阵A
再
输
入
K
×
N
的
矩
阵
B
再输入K×N的矩阵B
再输入K×N的矩阵B
求
矩
阵
C
=
A
B
求矩阵C=AB
求矩阵C=AB
求
矩
阵
M
=
C
N
∗
N
并
将
M
中
所
有
的
值
对
6
取
模
,
得
到
新
矩
阵
M
′
求矩阵M=C^{N*N}并将M中所有的值对6取模,得到新矩阵M'
求矩阵M=CN∗N并将M中所有的值对6取模,得到新矩阵M′
将
M
′
中
所
有
值
求
和
得
到
结
果
s
u
m
将M'中所有值求和得到结果sum
将M′中所有值求和得到结果sum
输
出
s
u
m
输出sum
输出sum
思路:
①、对于常规思路,计算得出的矩阵
C
=
A
B
C=AB
C=AB是一个
N
×
N
N×N
N×N的矩阵,由于(4<=N<=1000),对于矩阵乘法的时间复杂度是O(N3) ,显然会TLE,而矩阵
C
′
=
B
A
C'=BA
C′=BA却是一个
K
×
K
K×K
K×K的矩阵。
②、于是我们转换一下思路:对
C
N
∗
N
=
A
B
A
B
A
B
.
.
.
A
B
=
A
(
B
A
)
(
B
A
)
.
.
.
(
B
A
)
B
C^{N*N}=ABABAB...AB=A(BA)(BA)...(BA)B
CN∗N=ABABAB...AB=A(BA)(BA)...(BA)B,那么要计算
C
N
∗
N
C^{N*N}
CN∗N,可以先计算
C
′
N
∗
N
−
1
C'^{N*N-1}
C′N∗N−1,再计算
A
C
′
B
AC'B
AC′B。
代码:
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1e3+5;
const int mod=6;
int N,K;
int E[maxn][maxn],A[maxn][maxn],B[maxn][maxn],B1[maxn][maxn];
int c[maxn][maxn];
int ans=0;
void mul(int a[maxn][maxn],int b[maxn][maxn],int r1,int r2,int r3)//r1是a的行,r2是b的列,r3是a的列b的行
{
//int c[maxn][maxn]; 栈内50w左右
memset(c,0,sizeof(c));
for(int k=0;k<r3;k++)
for(int i=0;i<r1;i++)
if(a[i][k])
for(int j=0;j<r2;j++)
if(b[k][j])
c[i][j]=(c[i][j]+a[i][k]*b[k][j]%mod+mod)%mod;
memcpy(a,c,sizeof(c));
}
void pow(int n)
{
while(n)
{
if(n&1) mul(E,B,K,K,K);
mul(B,B,K,K,K);
n>>=1;
}
}
int main()
{
while(~scanf("%d%d",&N,&K))
{
if(N==0&&K==0) return 0;
memset(E,0,sizeof(E));
for(int i=0;i<K;i++) E[i][i]=1;
memset(A,0,sizeof(A));
memset(B,0,sizeof(B));
ans=0;
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]);
B1[i][j]=B[i][j];
}
mul(B,A,K,K,N);//计算C'
pow(N*N-1);//计算C'^(N*N-1)
mul(A,E,N,K,K);//首先AC',将结果赋值给A
mul(A,B1,N,N,K);//接着AC'B,这里B矩阵已经更改过,所以用事先备份好的B1矩阵来操作
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
ans+=(A[i][j]%6);
printf("%d\n",ans);
}
}
总结:
- 栈 内 i n t 数 组 最 大 开 50 w 栈内int数组最大开50w 栈内int数组最大开50w
- 每 次 矩 阵 相 乘 m u l 后 要 注 意 前 一 个 矩 阵 被 覆 盖 了 每次矩阵相乘mul后要注意前一个矩阵被覆盖了 每次矩阵相乘mul后要注意前一个矩阵被覆盖了