[Unknow]神奇的矩阵

壹、题目描述 ¶

传送门 to Darkbzoj.

贰、题解 ¶

考虑两个矩阵直接相乘复杂度达到了 \(\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;
}

肆、关键之处 ¶

一种奇怪的矩阵比较技巧。

posted @ 2021-07-22 14:28  Arextre  阅读(119)  评论(3编辑  收藏  举报