不拆矩阵的人有难了 ProMax

· · 个人记录

Useless

自动拆矩阵.

#include<bits/stdc++.h>
#define pii pair<int, int>
#define fr first
#define sc second
#define int long long
using namespace std;

inline int rd(){
    int s=0, f=1; char c=getchar();
    while(!isdigit(c)) f^=(c=='-'), c=getchar();
    while(isdigit(c)) s=s*10+c-'0', c=getchar();
    return f? s:-s;
}

void wt(int x, char c=0){
    if(x<0) return putchar('-'), wt(-x, c);
    if(x>9) wt(x/10); putchar(x%10+'0');
    if(c) putchar(c);
}

const int N=105, sz=5;
const int emptyVal=0, basVal=1;
const int Mod=998244353;

int f0(int x, int y){
    return x*y%Mod;
}

int f1(int x, int y){
    return (x+y)%Mod;
}


struct Matrix{
    int c[N][N];

    Matrix(bool if_bas=false){
        for(int i=1; i<=sz; i++){
            for(int j=1; j<=sz; j++){
                c[i][j]=emptyVal;
            }
        }
        if(if_bas){
            for(int i=1; i<=sz; i++){
                c[i][i]=basVal;
            }
        }
    }

    void print(){
        for(int i=1; i<=sz; i++){
            for(int j=1; j<=sz; j++){
                wt(c[i][j], ' ');
            }
            puts("");
        }
    }

    Matrix operator *(const Matrix &o)const{
        Matrix re;
        for(int i=1; i<=sz; i++){
            for(int j=1; j<=sz; j++){
                for(int k=1; k<=sz; k++){
                    re.c[i][j]=f1(re.c[i][j], f0(c[i][k], o.c[k][j]));
                }
            }
        }
        return re;
    }
};

const Matrix Bas(true);

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int rnr(int l, int r){return l+((long long)rng())%(r-l+1);}

Matrix genmat(){
    int opt=rnr(1, 3);
    Matrix re(true);
    if(opt==1){
        int x=rnr(0, Mod-1);
        re.c[1][1]=0;
        re.c[5][1]=x;
        re.c[3][3]=0;
        re.c[2][3]=x;
    }
    else if(opt==2){
        int x=rnr(0, Mod-1);
        re.c[2][2]=0;
        re.c[5][2]=x;
        re.c[3][3]=0;
        re.c[1][3]=x;
    }
    else{
        re.c[3][4]=1;
    }
    return re;
}

const int Tim=50, Groupnum=10;

bool same[N][N];

struct Node{
    int x, y, z, w;

    void print(){
        if(x==0) wt(z);
        else if(x==-1) printf("m%lld%lld", z, w);
        else if(x==-2) printf("v%lld", z);
        else if(x==-3) printf("f0(v%lld, m%lld%lld)", y, z, w);
        else printf("f0(m%lld%lld, m%lld%lld)", x, y, z, w);
    }
};

void work(){
    auto group_calc=[&]()->void {
        Matrix cur(true);
        for(int i=1; i<=Tim; i++){
            cur=cur*genmat();
            for(int j=1; j<=sz; j++){
                for(int k=1; k<=sz; k++){
                    same[j][k]&=(cur.c[j][k]==Bas.c[j][k]);
                }
            }
        }
    };

    for(int i=1; i<=sz; i++){
        for(int j=1; j<=sz; j++){
            same[i][j]=true;
        }
    }

    for(int i=1; i<=Groupnum; i++) group_calc();

    vector<pii> changed;

    for(int i=1; i<=sz; i++){
        for(int j=1; j<=sz; j++) if(!same[i][j]){
            changed.push_back({i, j});
        }
    }

    printf("changed value in Matrix:\n");

    for(int i=0; i<changed.size(); i++){
        auto [x, y]=changed[i];
        printf("m%lld%lld%s", x, y, i==changed.size()-1? "\n":", ");
    }

    puts("");

    printf("multiplication of Matrix:\n");

    for(int i=0; i<changed.size(); i++){
        auto [x, y]=changed[i];
        vector<Node> vec;

        for(int k=1; k<=sz; k++){
            if(same[x][k] && same[k][y]){
                int val=f0(Bas.c[x][k], Bas.c[k][y]);
                if(val!=emptyVal)
                    vec.push_back({0, 0, f0(Bas.c[x][k], Bas.c[k][y]), 0});
                continue;
            }
            if(!same[x][k] && !same[k][y]) vec.push_back({x, k, k, y});
            int val=same[x][k]? Bas.c[x][k]:Bas.c[k][y];
            if(val!=emptyVal) same[x][k]? vec.push_back({-1, -1, k, y}):vec.push_back({-1, -1, x, k});
        }

        printf("m%lld%lld=", x, y);
        if(vec.size()==1) vec[0].print();
        else{
            printf("f1({ ");
            for(int i=0; i<vec.size(); i++){
                vec[i].print();
                printf("%s", i<vec.size()-1? ", ":" })");
            }
        }
        puts(i==changed.size()-1? "":",");
    }

    puts("");

    printf("Vector:\n");

    for(int i=1; i<=sz; i++) printf("v%d%s", i, i==sz? "\n":", ");

    puts("");

    printf("multiplication of Vector & Matrix:\n");

    for(int i=1; i<=sz; i++){
        vector<Node> vec;
        for(int k=1; k<=sz; k++){
            if(same[k][i]){
                int val=Bas.c[k][i];
                if(val!=emptyVal)
                    vec.push_back({-2, -2, k, 0});
                continue;
            }
            else vec.push_back({-3, k, k, i});
        }

        printf("v%d=", i);
        if(vec.size()==1) vec[0].print();
        else{
            printf("f1({ ");
            for(int i=0; i<vec.size(); i++){
                vec[i].print();
                printf("%s", i<vec.size()-1? ", ":" })");
            }
        }
        puts(i==sz? "":",");
    }
}

signed main(){
    work();
    return 0;
}