【数学】矩阵的逆

struct InverseMatrix {

    static const int MOD = 1e9 + 7;
    static const int MAXN = 400 + 10;

    int n;
    int A[MAXN][MAXN];
    int B[MAXN][MAXN];

    void Init(int n) {
        this->n = n;
        ms(A);
    }

    void ShowB() {
        for(int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j)
                printf("%d%c", B[i][j], " 
"[j == n]);
        }
    }

    ll qpow(ll x, ll n) {
        ll res = 1;
        while(n) {
            if(n & 1)
                res = res * x % MOD;
            x = x * x % MOD;
            n >>= 1;
        }
        return res;
    }

    int Inverse() {
        for(int i = 1; i <= n; ++i) {
            for(int j = 1; j <= n; ++j)
                B[i][j] = (j == i);
        }
        for(int cur = 1, pit; cur <= n; ++cur) {
            for(pit = cur; pit <= n; ++pit) {
                if(A[pit][cur])
                    break;
            }
            if(pit > n)
                return -1;
            if(pit != cur) {
                for(int j = cur; j <= n; ++j)
                    swap(A[pit][j], A[cur][j]);
                for(int j = 1; j <= n; ++j)
                    swap(B[pit][j], B[cur][j]);
            }
            int tmp = qpow(A[cur][cur], MOD - 2);
            for(int j = cur; j <= n; ++j)
                A[cur][j] = 1LL * A[cur][j] * tmp % MOD;
            for(int j = 1; j <= n; ++j)
                B[cur][j] = 1LL * B[cur][j] * tmp % MOD;
            for(int i = 1; i <= n; ++i) {
                if(i == cur || A[i][cur] == 0)
                    continue;
                int tmp = A[i][cur];
                for(int j = cur; j <= n; ++j)
                    A[i][j] = (A[i][j] - 1LL * tmp * A[cur][j] % MOD + MOD) % MOD;
                for(int j = 1; j <= n; ++j)
                    B[i][j] = (B[i][j] - 1LL * tmp * B[cur][j] % MOD + MOD) % MOD;
            }
        }
        return n;
    }

} ma;

类似的方法可以找出使得A矩阵变换到B矩阵要左乘的矩阵。

分开A和B来写可以减少矩阵第二维的长度,(在交换两行时)对内存更友好。

原文地址:https://www.cnblogs.com/purinliang/p/14333027.html