Under construction

Still being built. Things may change

RREF

Interview

Time limit0.5sMemory limit1024 MB

Summary
Compute the reduced row echelon form of an N by M matrix of rational numbers, printing each entry as a reduced fraction.
Level

Medium4 of 10

Topics
Math, Implementation, Matrix, Number theory
Solved
No attempts yet

Problem

Output the RREF of the given matrix.

RREF (Reduced Row Echelon Form) is a matrix with the following properties.

  • Every row whose entries are all 00 (a zero row) is below every row that is not (a row with a nonzero entry).
  • The leading coefficient of every nonzero row is 11.
  • The leading coefficient of every nonzero row is to the right of the leading coefficient of the row above it.
  • In the column of the leading coefficient of every nonzero row, all other entries are 00.

Input

The first line gives NN and MM, the dimensions of the matrix. (1≤N,M≤51 \leq N, M \leq 5)

The next NN lines each contain MM rational numbers. Each rational number is given as a fraction PP/QQ in lowest terms. (P∈Z∩[−9,9]P \in \mathbb{Z} \cap \left[-9, 9\right], Q∈Z∩(0,9]Q \in \mathbb{Z} \cap \left(0, 9\right])

If Q=1Q = 1, only PP is given, and if P>0P > 0, the sign is omitted.

Output

Output the RREF of the given matrix.

Print each entry as a fraction PP/QQ in lowest terms. If Q=1Q = 1, print only PP, and if P>0P > 0, omit the sign.

Hint

RREF stands for Reduced Row Echelon Form.

Examples2

  1. Example 1

    Input
    3 5
    1 0 0 1 2
    0 2 0 2 6
    0 1 2 1 3
    
    Expected output
    1 0 0 1 2
    0 1 0 1 3
    0 0 1 0 0
    
  2. Example 2

    Input
    3 4
    1 2 4 2/3
    -2/3 1 2 3
    4 -6 7/6 2
    
    Expected output
    1 0 0 -16/7
    0 1 0 -2591/1659
    0 0 1 120/79