1. 程式人生 > >[UPC](2783)Matrix Cypher ---- 矩陣初等變換

[UPC](2783)Matrix Cypher ---- 矩陣初等變換

做法:

  • 感覺複習了一波線性代數~233,記錄一下,防止忘記
  • 這個題,通過題目中給的兩個矩陣,會發現,當是0位元組的時候,它會把矩陣第二列中的值乘1加到第一列,第二列不變。
  • 當是1位元組的時候,它會把矩陣第一列中的值乘1加到第二列,第一列不變。
  • 即這是最基本的矩陣初等列變換。
  • 我們每次判斷第一列值的和 和第二列的和 誰大誰小,然後逆向模擬即可。

AC程式碼:

import java.lang.reflect.Array;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main{
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        BigInteger f[][] = new BigInteger[2][2];
        BigInteger a[][] = new BigInteger[2][2];
        BigInteger b[][] = new BigInteger[2][2];
        a[0][0] = BigInteger.ONE;
        a[0][1] = BigInteger.ZERO;
        a[1][0] = BigInteger.ONE;
        a[1][1] = BigInteger.ONE;
 
        b[0][0] = BigInteger.ONE;
        b[0][1] = BigInteger.ONE;
        b[1][0] = BigInteger.ZERO;
        b[1][1] = BigInteger.ONE;
        BigInteger e;
        for(int i=0;i<2;i++){
            for(int j=0;j<2;j++){
                e = cin.nextBigInteger();
                f[i][j] = e;
            }
        }
        int ans[] = new int[150];
        int k = 0;
        while(true){
            if(f[0][0].add(f[1][0]) .compareTo(f[0][1].add(f[1][1])) == 0) break;
            if(f[0][0].add(f[1][0]) .compareTo(f[0][1].add(f[1][1])) > 0){
                ans[k] = 0;
                f[0][0] = f[0][0].subtract(f[0][1]);
                f[1][0] = f[1][0].subtract(f[1][1]);
                k++;
            }else if(f[0][0].add(f[1][0]) .compareTo(f[0][1].add(f[1][1])) < 0){
                ans[k] = 1;
                f[0][1] = f[0][1].subtract(f[0][0]);
                f[1][1] = f[1][1].subtract(f[1][0]);
                k++;
            }
        }
        for(int i=k-1;i>=0;i--) System.out.print(ans[i]);
        System.out.println();
    }
}