Matrix Multiplication
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17132 | Accepted: 3732 |
Description
You are given three n × n matrices A, B and C. Does the equation A × B = C hold true?
Input
The first line of input contains a positive integer n (n ≤ 500) followed by the the three matrices A, B and C respectively. Each matrix's description is a block of n × n integers.
It guarantees that the elements of A and B are less than 100 in absolute value and elements of C are less than 10,000,000 in absolute value.
Output
Output "YES" if the equation holds true, otherwise "NO".
Sample Input
2 1 0 2 3 5 1 0 8 5 1 10 26
Sample Output
YES
Hint
Multiple inputs will be tested. So O(n
3) algorithm will get TLE.
Source
题目大意:就是给你三个矩阵a,b,c,问是否a*b=c
ac代码
#include<stdio.h>
#include<string.h>
__int64 a[1010][1010],b[1010][1010],c[1010][1010],e[1010],temp[1010],f[1010];
int check(int n)
{
int i,j;
memset(temp,0,sizeof(temp));
memset(f,0,sizeof(f));
for(i=0;i<n;i++)
for(j=0;j<n;j++)
temp[i]+=a[j][i]*(j+1);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
f[i]+=b[j][i]*temp[j];
for(i=0;i<n;i++)
if(e[i]!=f[i])
return 0;
return 1;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%I64d",&a[i][j]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%I64d",&b[i][j]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%I64d",&c[i][j]);
memset(e,0,sizeof(e));
for(i=0;i<n;i++)
for(j=0;j<n;j++)
e[i]+=c[j][i]*(j+1);
if(check(n))
{
printf("YES\n");
}
else
printf("NO\n");
}
}