HDU 4920 矩阵乘积 优化

时间:2023-03-09 08:16:36
HDU 4920  矩阵乘积 优化

Matrix multiplication

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1289    Accepted Submission(s): 568

Problem Description
Given two matrices A and B of size n×n, find the product of them.

bobo hates big integers. So you are only asked to find the result modulo 3.

Input
The input consists of several tests. For each tests:

The first line contains n (1≤n≤800). Each of the following n lines
contain n integers -- the description of the matrix A. The j-th integer
in the i-th line equals Aij. The next n lines describe the matrix B in similar format (0≤Aij,Bij≤109).

Output
For each tests:

Print n lines. Each of them contain n integers -- the matrix A×B in similar format.

Sample Input
1
0
1
2
0 1
2 3
4 5
6 7
Sample Output
0
0 1
2 1
#include <iostream>
#include <string.h>
#include <stdio.h> using namespace std;
#define MAX 810
int a[MAX][MAX],b[MAX][MAX],c[MAX][MAX]; int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
scanf("%d",&a[i][j]);
a[i][j]=a[i][j]%;
}
}
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
scanf("%d",&b[i][j]);
b[i][j]=b[i][j]%;
}
} memset(c,,sizeof(c));
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
if(a[i][j]==) continue;
for(int k=;k<n;k++)
{
c[i][k]+=a[i][j]*b[j][k]; 如果这里 c[i][k]+=(a[i][j]*b[j][k])%3;就会超时
}
}
}
for(int i = ; i < n; i++)
{
for(int j = ; j < n; j++)
if(j == )
printf("%d", c[i][j]%);
else
printf(" %d", c[i][j]%);
printf("\n");
}
}
return ;
}
#include <iostream>
#include<stdio.h>
#include<vector>
///他把所有的0都忽略了,很巧妙的优化,aa[][], bb[][]里存储的是下一个不为0的位置:
#include<queue>
#include<stack>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
#define LL long long
#define gcd(a,b) (b==0?a:gcd(b,a%b))
#define lcm(a,b) (a*b/gcd(a,b))
//O(n)求素数,1-n的欧拉数
#define N 100010
//A^x = A^(x % Phi(C) + Phi(C)) (mod C)
int a[][];
int aa[][];
int b[][];
int bb[][]; int c[][];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(a,,sizeof(a));
memset(b,,sizeof(b));
memset(aa,,sizeof(aa));
memset(bb,,sizeof(bb));
memset(c,,sizeof(c));
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
scanf("%d",&a[i][j]);
a[i][j]%=;
/// cout<<"%%%"<<a[i][j]<<endl;
}
}
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
scanf("%d",&b[i][j]);
b[i][j]%=;
}
}
for(int i=; i<=n; i++)
{
int x=-;
for(int j=n; j>=; j--)
{
aa[i][j]=x; ///后退了一位 所以j:n~~0
if(a[i][j]) x=j;
//cout<<"~~~~"<<a[i][j]<<' '<<aa[i][j]<<endl;///记录矩阵中0的位置 赋值给aa【】【】
}
}
for(int i=; i<=n; i++)
{
int x=-;
for(int j=n; j>=; j--)
{
bb[i][j]=x;
if(b[i][j])x=j;
}
}
for(int i=; i<=n; i++)
{
for(int j=aa[i][]; j!=-; j=aa[i][j])
{
for(int k=bb[j][]; k!=-; k=bb[j][k]) ///a==0时 对应b那一列乘a==0 所以根据bb【j】来转变
c[i][k]+=a[i][j]*b[j][k];
}
}
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
printf("%d",c[i][j]%);
if(j!=n)printf(" ");
else printf("\n");
}
}
}
return ;
}