hdu Matrix Multiplication 写一个类似哈希函数的东西一切就解决了。

Problem Description

You are given three n × n matrices AB 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 AB and 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
***************************************************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 #include<queue>
 7 #include<algorithm>
 8 #include<vector>
 9 #define LL long long
10 using namespace std;
11 
12 LL a[501][501],b[501][501],c[501][501];
13 LL d[501],e[501],f[501],x[501];
14 LL n,m,i,j,k;
15 int main()
16 {
17     while(scanf("%lld",&n)!=EOF)
18     {
19         for(i=0; i<n; i++)
20             for(j=0; j<n; j++)
21                 scanf("%lld",&a[i][j]);
22         for(i=0; i<n; i++)
23             for(j=0; j<n; j++)
24                 scanf("%lld",&b[i][j]);
25         for(i=0; i<n; i++)
26             for(j=0; j<n; j++)
27                 scanf("%lld",&c[i][j]);
28         for(i=0; i<n; i++)
29             x[i]=i;
30         for(i=0; i<n; i++)
31             f[i]=e[i]=d[i]=0;
32         for(i=0; i<n; i++)
33             for(j=0; j<n; j++)
34             {
35                 e[i]+=b[i][j]*x[j];
36                 f[i]+=c[i][j]*x[j];
37             }
38         for(i=0; i<n; i++)
39             for(int j=0; j<n; j++)
40                 d[i]+=a[i][j]*e[j];
41         for(i=0; i<n; i++)
42           if(d[i]!=f[i]) break;
43         if(i<n)
44             puts("NO");
45         else
46             puts("YES");
47     }
48     return 0;
49 }
View Code