Matrix Multiplication
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
POJ Monthly--2007.08.05, qzc
|
[Submit] [Go Back] [Status] [Discuss]
涨姿势哦
输入外挂强行n3水过
ACcode:
#pragma warning(disable:4786)//使命名长度不受限制 #pragma comment(linker, "/STACK:102400000,102400000")//手工开栈 #include <map> #include <set> #include <queue> #include <cmath> #include <stack> #include <cctype> #include <cstdio> #include <cstring> #include <stdlib.h> #include <iostream> #include <algorithm> #define rd(x) scanf("%d",&x) #define rd2(x,y) scanf("%d%d",&x,&y) #define rds(x) scanf("%s",x) #define rdc(x) scanf("%c",&x) #define ll long long int #define maxn 505 #define mod 1000000007 #define INF 0x3f3f3f3f //int 最大值 #define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i) #define MT(x,i) memset(x,i,sizeof(x)) #define PI acos(-1.0) #define E exp(1) using namespace std; int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn]; inline int Scan(){ int d=0; char c,t=0; while((c=getchar())==' '||c=='\n'); if(c=='-')t=1; else d=c-'0'; while((c=getchar())>='0'&&c<='9') d=d*10+c-'0'; if(t)return -d; else return d; } int main(){ int n,t;n=Scan(); FOR(i,1,n)FOR(j,1,n)a[i][j]=Scan(); FOR(i,1,n)FOR(j,1,n)b[i][j]=Scan(); FOR(i,1,n)FOR(j,1,n)c[i][j]=Scan(); FOR(i,1,n)FOR(j,1,n){ t=0; FOR(k,1,n)t+=a[i][k]*b[k][j]; if(t!=c[i][j]){puts("NO\n");return 0;} } puts("YES\n"); return 0; } /* 2 1 0 2 3 5 1 0 8 5 1 10 26 */