给出三个N*N的矩阵A, B, C,问A * B是否等于C?
Input
第1行,1个数N。(0 <= N <= 500) 第2 - N + 1行:每行N个数,对应矩阵A的元素。(0 <= M[i] <= 16) 第N + 2 - 2N + 1行:每行N个数,对应矩阵B的元素。(0 <= M[i] <= 16) 第2N + 2 - 3N + 1行:每行N个数,对应矩阵C的元素。
Output
如果相等输出Yes,否则输出No。
Input示例
2 1 0 0 1 0 1 1 0 0 1 1 0
Output示例
Yes
H*A*B=H*C
随机几个H看能不能满足这个式子就好啦。
#include <bits/stdc++.h> //#include <ext/pb_ds/tree_policy.hpp> //#include <ext/pb_ds/assoc_container.hpp> //using namespace __gnu_pbds; using namespace std; #define pi acos(-1) #define endl '\n' #define me(x) memset(x,0,sizeof(x)); #define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++) #define close() ios::sync_with_stdio(0); #define srand() srand(time(0)); typedef long long LL; const int INF=0x3f3f3f3f; const LL LINF=0x3f3f3f3f3f3f3f3fLL; //const int dx[]={-1,0,1,0,-1,-1,1,1}; //const int dy[]={0,1,0,-1,1,-1,1,-1}; const int maxn=5e2+5; const int maxx=1e5+100; const double EPS=1e-9; const int MOD=1000000007; #define mod(x) ((x)%MOD); template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);} template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);} template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));} template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));} //typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree; /*lch[root] = build(L1,p-1,L2+1,L2+cnt); rch[root] = build(p+1,R1,L2+cnt+1,R2);中前*/ /*lch[root] = build(L1,p-1,L2,L2+cnt-1); rch[root] = build(p+1,R1,L2+cnt,R2-1);中后*/ long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);} int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn]; int ans1[maxn],ans2[maxn],ans3[maxn],rnd[maxn]; int n; inline int Scan() { int res=0,ch,flag=0; if((ch=getchar())=='-')flag=1; else if(ch>='0' && ch<='9')res=ch-'0'; while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-'0'; return flag ? -res : res; } int main() { srand(); cin>>n; for(int i=1;i<=n;i++) for (int j=1; j<=n; j++) a[i][j]=Scan(); for(int i=1;i<=n;i++) for (int j=1; j<=n; j++) b[i][j]=Scan(); for(int i=1;i<=n;i++) for (int j=1; j<=n; j++) c[i][j]=Scan(); int cnt=20; while(cnt--) { me(ans1);me(ans2);me(ans3); for(int i=1;i<=n;i++) rnd[i]=(LL)rand()%16; for(int i=1;i<=n;i++) for (int j=1; j<=n; j++) ans1[i]+=rnd[j]*a[j][i]; for(int i=1;i<=n;i++) for (int j=1; j<=n; j++) ans2[i]+=ans1[j]*b[j][i]; for(int i=1;i<=n;i++) for (int j=1; j<=n; j++) ans3[i]+=rnd[j]*c[j][i]; for(int i=1;i<=n;i++) if(ans2[i]!=ans3[i]) { puts("No"); return 0; } } puts("Yes"); }