0
点赞
收藏
分享

微信扫一扫

HDU-4965 Fast Matrix Calculation (矩阵快速幂)

微言记 2023-02-07 阅读 43


Fast Matrix Calculation
​​​ http://acm.hdu.edu.cn/showproblem.php?pid=4965​​

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

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
 

 

题意:

给你一个 N * K 的矩阵 A 和一个 K * N 的矩阵 B . 矩阵元素需要%6,令 C = A * B; 然后计算M =C^(n*n) ,最后让你求矩阵 M 中所有元素的和,

分析:

开心的套模板,开心的T

我们发现n很大,但k很小,

A( N * K的矩阵) *B(K * N的矩阵)=(A*B)(N*N的矩阵)

B(K * N的矩阵)*A( N * K的矩阵) =(B*A)(K*K的矩阵)

 

(A*B)(n*n)=(A*(B*A)^(n-1)*B) 结合律

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#define N 10005
using namespace std;
const int MAXN= 10;
const int p= 6;
typedef long long ll;
ll len;
struct Matrix
{
long long M[MAXN][MAXN];
Matrix(const bool I = 0) ///初始化对角矩阵
{
memset(M, 0, sizeof(M));
if (I)
for(int i = 0; i < len; i++)
M[i][i] = 1;
}
Matrix operator *(const Matrix &y) ///矩阵乘法,对乘法重新定义
{
Matrix z;
for (int i = 0; i < len; i++)
for (int j = 0; j < len; j++)
for (int k = 0; k < len; k++)
z.M[i][j] = (z.M[i][j]+M[i][k]*y.M[k][j]%p)%p;
return z;
}
void out()
{
for (int i = 0; i <len; i++)
{
for (int j = 0; j < len; j++)
cout << M[i][j] << " ";
cout << "\n";
}
}
};
Matrix pow(Matrix A, long long b)///矩阵的快速幂
{
Matrix ans = Matrix(1);
for (; b; b>>=1)
{
if (b&1) ans = ans*A;
A = A*A;
}
return ans;
}
ll x[1050][1050],y[1050][1050],a[1050][1050],b[1050][1050];
int main()
{
int T;

ll n,m,k;
while(scanf("%lld%lld",&n,&m)!=-1&&n&&m)
{
len=m; ///构造矩阵的长度
Matrix c;
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%lld",&a[i][j]);
a[i][j]=a[i][j]%p;
}
}
memset(b,0,sizeof(b));
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
scanf("%lld",&b[i][j]);
b[i][j]=b[i][j]%p;
}
}
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])%p;
}

c=pow(c,n*n-1);

memset(x,0,sizeof(x));
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
for(int k=0;k<m;k++)
{
x[i][j]=(x[i][j]+a[i][k]*c.M[k][j])%p;
}

memset(y,0,sizeof(y));
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
for(int k=0;k<m;k++)
{
y[i][j]=(y[i][j]+x[i][k]*b[k][j])%p;
}

ll ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
ans=(ans+y[i][j]);
}
}

printf("%lld\n",ans);

}
return 0;
}

 

举报

相关推荐

0 条评论