2017 ICPC Sichuan Province Competition C Question Determinant

Topic description
Question C Determinat

To the effect
Bobo has learned the definition of the determinant det(A) of matrix A in ICPCCamp. He also knew that the determinant can be computed in O(n3) using Gaussian elimination.
Bobo has an n×n matrix B and he wants to find det(Bi,j) modulo (109+7) for all i, j ∈ {1,2,…,n} where Bi,j are from B The matrix after removing the i-th row and j-th column.
Input
Input contains zero or more test cases and is terminated by a terminator. For each test case: the first line contains an integer n. The ith row of the following n rows contains n integers Bi, 1, Bi, 2, . . . , Bi, n.
• 2≤n≤500
• 0≤Bi, j < 109 + 7
• The sum of n does not exceed 5000.
Output
For each case, output n lines, where line i contains the n integers det(Bi, 1), det(Bi, 2), ..., det(Bi, n) modulo (109 + 7).
Sample input
0 1
0 1000000006
Sample output
1000000006 0
1 0

Problem- solving ideas
There are a few prerequisites to know about this problem, which are the solution of the determinant and the Gaussian elimination method. Those who don't know it can learn it by themselves.
Find Bi,j for each element of the n×n matrix B, and then find the determinant for Bi,j.

java code

import java.util.Scanner;

public class Determinat {
    static double epc=10e9+7;

    public static double[][] removal(double a[][],int x,int y){//计算矩阵B移除i行j列所有元素所生成的矩阵
        int n=a.length;
        double b[][]=new double[n][n],re[][]=new double[n-1][n-1];//n表示原矩阵的阶数,b表示临时矩阵,re表示结果矩阵
        for(int i=0;i<n;i++)//复制a矩阵
            for(int j=0;j<n;j++)
                b[i][j]=a[i][j];

        for(int i=x;i<n-1;i++)//行删除
            for(int j=0;j<n;j++)
                b[i][j]=b[i+1][j];

        for(int i=y;i<n-1;i++)//列删除
            for(int j=0;j<n-1;j++)
                b[j][i]=b[j][i+1];

        for(int i=0;i<n-1;i++)//生成结果矩阵
            for(int j=0;j<n-1;j++)
                re[i][j]=b[i][j];
        return re;
    }

    public static double Det(double Matrix[][],int N) {//计算N阶行列式
         double result=0;//结果变量
         if(N>=1){
          if(N==1)//1阶行列式
           return Matrix[0][0];
          else if(N==2)//2阶行列式
           return (Matrix[0][0]*Matrix[1][1]-Matrix[0][1]*Matrix[1][0])%epc;
          else{//N>2阶行列式
           for(int T0=0;T0<N;T0++)//将行列式拆分为N个子式
            result+=Matrix[0][T0]*Det(removal(Matrix,0,T0),N-1)*Math.pow(-1,T0);
           return result%epc;
           }
         }
           return 0;
    } 

    public static void main(String[] args) {

        Scanner scan=new Scanner(System.in);
        int n=scan.nextInt();
        double arr[][]=new double[n][n];//变量n表示矩阵阶数,二维数组arr表示n×n阶矩阵
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                arr[i][j]=scan.nextDouble();

        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
                System.out.printf("%.0f ",Det(removal(arr,i,j),n-1));
            System.out.println();
        }
    }
}

The above code is purely hand-typed by the editor, and there may be errors. Welcome to leave a message for discussion and correction.

Test Data
Test 1 Input:
3
1 2 3
0 1 2
2 1 2
Test 2 Input:
0 -4 -2
1 -4 -3
1 2 1

Test 1 Input:
4
1 32 1 2
0 1 2 3
2 4 2 1
12 4 1 1
Test 2 Input:
-11 -46 -110 -58
24 -33 240 660
-35 -13 1117 749
-129 -6 177 120

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325997018&siteId=291194637