[Unknow]神奇的矩陣
阿新 • • 發佈:2021-07-22
壹、題目描述 ¶
貳、題解 ¶
考慮兩個矩陣直接相乘複雜度達到了 \(\mathcal O(n^3)\),但是這道題顯然是 \(\mathcal O(n^2)\) 的範圍,我們不妨隨機一個向量 \(\alpha\),然後檢測 \(\alpha\times A\times B\) 與 \(\alpha\times C\) 的結果,這樣就變成了 \(\mathcal O(n^2)\) 的了。
叄、參考程式碼 ¶
#include<cstdio> #include<vector> #include<cstring> #include<algorithm> #include<ctime> using namespace std; // #define NDEBUG #include<cassert> namespace Elaina{ #define rep(i, l, r) for(int i=(l), i##_end_=(r); i<=i##_end_; ++i) #define drep(i, l, r) for(int i=(l), i##_end_=(r); i>=i##_end_; --i) #define fi first #define se second #define mp(a, b) make_pair(a, b) #define Endl putchar('\n') #define mmset(a, b) memset(a, b, sizeof a) // #define int long long typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; template<class T>inline T fab(T x){ return x<0? -x: x; } template<class T>inline void getmin(T& x, const T rhs){ x=min(x, rhs); } template<class T>inline void getmax(T& x, const T rhs){ x=max(x, rhs); } template<class T>inline T readin(T x){ x=0; int f=0; char c; while((c=getchar())<'0' || '9'<c) if(c=='-') f=1; for(x=(c^48); '0'<=(c=getchar()) && c<='9'; x=(x<<1)+(x<<3)+(c^48)); return f? -x: x; } template<class T>inline void writc(T x, char s='\n'){ static int fwri_sta[1005], fwri_ed=0; if(x<0) putchar('-'), x=-x; do fwri_sta[++fwri_ed]=x%10, x/=10; while(x); while(putchar(fwri_sta[fwri_ed--]^48), fwri_ed); putchar(s); } } using namespace Elaina; #define int long long const int maxn=1000; const int maxa=1000; struct matrix{ int n, m; int a[maxn+5][maxn+5]; matrix(){} matrix(int N, int M): n(N), m(M){ rep(i, 1, n) rep(j, 1, m) a[i][j]=0; } inline void epsilon(int N){ n=m=N; rep(i, 1, n) rep(j, 1, n) a[i][j]=(i==j); } inline matrix operator *(const matrix rhs) const{ assert(m==rhs.n); matrix ret(n, rhs.m); rep(i, 1, n) rep(j, 1, m) if(a[i][j]) rep(k, 1, rhs.m) ret.a[i][k]+=a[i][j]*rhs.a[j][k]; return ret; } inline bool operator ==(const matrix rhs) const{ if(n!=rhs.n || m!=rhs.m) return false; rep(i, 1, n) rep(j, 1, m) if(a[i][j]!=rhs.a[i][j]) return false; return true; } inline bool operator !=(const matrix rhs) const{ return !((*this)==rhs); } }; matrix a, b, c, vec; int n; inline int getrnd(){ return (rand()<<15|rand())%maxa; } inline void randVec(){ vec=matrix(1, n); rep(i, 1, n) vec.a[1][i]=getrnd(); } inline void input(){ a=b=c=matrix(n, n); rep(i, 1, n) rep(j, 1, n) a.a[i][j]=readin(1); rep(i, 1, n) rep(j, 1, n) b.a[i][j]=readin(1); rep(i, 1, n) rep(j, 1, n) c.a[i][j]=readin(1); } signed main(){ srand((unsigned)time(NULL)); while(~scanf("%d", &n)){ input(); bool flg=1; rep(_, 1, 5){ randVec(); if(vec*a*b!=vec*c){ flg=false; break; } } if(flg) printf("Yes\n"); else printf("No\n"); } return 0; }
肆、關鍵之處 ¶
一種奇怪的矩陣比較技巧。