(Template) inverse matrix luoguP4783

Topic link: https: //www.luogu.org/problem/P4783

Meaning of the questions: Inverse of Matrix.

Ideas: Matrix inverse Gaussian elimination method, n is 400, the card often, I just opened the O2 optimization of AC. .

AC Code:

#include<cstdio>
#include<cctype>
#include<algorithm>
#define R register int
using namespace std;

const int maxn=405;
const int MOD=1e9+7;
int n;

struct Matrix{
    int m[maxn][maxn];
    void SWAP(int x,int y){   //交换两行
        for(R i=1;i<=n;++i)
            swap(m[x][i],m[y][i]);
    }
    void MUL(intX, int K) {     // a row multiplied by K 
        for (I = R & lt . 1 ; I <= n-; ++ I) 
            m [X] [I] = (1LL m * [X] [I] *% K + the MOD the MOD)% the MOD; 
    } 
    void MULADD ( int x, int y, int k) { // the row y multiplied by x k applied to the first row go 
        for (I = R & lt . 1 ; I <= n-; ++ I) 
            m [X] [I] = ((m [X] [I] + 1LL * m [Y] [I] * K% the MOD)% the MOD + the MOD)% the MOD; 
    } 
} A, B; 

int qpow ( int A, int B) {    // inversing 
    int RET =1;
    while(b){
        if(b&1) ret=1LL*ret*a%MOD;
        a=1LL*a*a%MOD;
        b>>=1;
    }
    return ret;
}

void Invmatrix(){
    for(R i=1;i<=n;++i){
        if(!A.m[i][i]){
            for(R j=i+1;j<=n;++j)
                if(A.m[j][i]){
                    B.SWAP(i,j);
                    A.SWAP(i,j);
                    break;
                }
        }
        if(!A.m[i][i]){  //没有逆矩阵
            puts("No Solution");
            return;
        }
        int tmp=qpow(A.m[i][i],MOD-2);
        B.MUL(i,tmp);
        A.MUL(i,tmp);    //系数化为1
        for(R j=i+1;j<=n;++j){  //消元
            tmp=-A.m[j][i];
            B.MULADD(j,i,tmp);
            A.MULADD(j,i,tmp);
        }
    }    
    for(R i=n-1;i>=1;--i)  //回带
        for(R j=i+1;j<=n;++j){
            int tmp=-A.m[i][j];
            B.MULADD(i,j,tmp);
            A.MULADD(i,j,tmp);
        }
    for(R i=1;i<=n;++i){
        for(R j=1;j<=n;++j){
            printf("%d",B.m[i][j]);
            if(j!=n) printf(" ");
        }
        printf("\n");
    }
}

int main(){
    scanf("%d",&n);
    for(R i=1;i<=n;++i)
        for(R j=1;j<=n;++j)
            scanf("%d",&A.m[i][j]);
    for(R i=1;i<=n;++i){
        for(R j=1;j<=n;++j)
            B.m[i][j]=0;
        B.m[i][i]=1;
    }
    Invmatrix();
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/FrankChen831X/p/11764185.html