弱弱的买了随机算法的视频水了一下2333
真的是好神
大概就是判AB=C,这样的话再等式两边同乘一个1*n的矩阵H(貌似有个专业的名字),这样矩阵乘法的复杂度就是n^2的。
因为矩阵乘法是有结合律的,所以就是先算出HA(蛤??),再算(HA)*B,然后和HC看是不是相等就好
get高端暴力姿势
1 #include<bits/stdc++.h> 2 #define LL long long 3 using namespace std; 4 5 const int QWQ=505; 6 7 LL a[QWQ][QWQ],b[QWQ][QWQ],c[QWQ][QWQ]; 8 int n; 9 LL rnd[QWQ],ans1[QWQ],ans2[QWQ],ans3[QWQ]; 10 11 int main() 12 { 13 srand(time(0)); 14 cin>>n; 15 for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) scanf("%lld",&a[i][j]); 16 for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) scanf("%lld",&b[i][j]); 17 for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) scanf("%lld",&c[i][j]); 18 int cnt=50; 19 while (cnt--) 20 { 21 memset(ans1,0,sizeof(ans1)); 22 memset(ans2,0,sizeof(ans2)); 23 memset(ans3,0,sizeof(ans3)); 24 for (int i=1; i<=n; i++) rnd[i]=(LL)rand()%16; 25 // for (int i=1; i<=n; i++) printf("%d ",rnd[i]); system("pause"); 26 27 for (int i=1; i<=n; i++) 28 for (int j=1; j<=n; j++) 29 ans1[i]+=rnd[j]*a[j][i]; 30 // for (int i=1; i<=n; i++) printf("%d ",ans1[i]); cout<<endl; 31 for (int i=1; i<=n; i++) 32 for (int j=1; j<=n; j++) 33 ans2[i]+=ans1[j]*b[j][i]; 34 // for (int i=1; i<=n; i++) printf("%d ",ans2[i]); cout<<endl; 35 for (int i=1; i<=n; i++) 36 for (int j=1; j<=n; j++) 37 ans3[i]+=rnd[j]*c[j][i]; 38 // for (int i=1; i<=n; i++) printf("%d ",ans3[i]); cout<<endl; 39 40 for (int i=1; i<=n; i++) 41 if (ans2[i]!=ans3[i]) 42 { 43 puts("No"); 44 return 0; 45 } 46 } 47 puts("Yes"); 48 return 0; 49 }
原文:http://www.cnblogs.com/ccd2333/p/6731427.html