Under construction

Still being built. Things may change

LU decomposition of a band matrix

Time limit3sMemory limit128 MB

Summary
Factor a tridiagonal matrix into unit-diagonal L and upper U, printing -1 when the factors do not exist or a pivot is zero.
Level

Medium5 of 10

Topics
Matrix, Math, Implementation
Solved
No attempts yet

Problem

You are given an N×NN \times N matrix AA. An LU decomposition splits AA into a product A=LUA = LU, where LL is a lower triangular matrix and UU is an upper triangular matrix.

  • A lower triangular matrix has Lij=0L_{ij} = 0 whenever i<ji < j.
  • An upper triangular matrix has Uij=0U_{ij} = 0 whenever i>ji > j.

This problem only deals with band matrices whose band has width 1. In other words, AA carries values on the main diagonal and on the two diagonals next to it, and every other entry is 0.

A=[b1c10⋯0a2b2c2⋯⋮0⋱⋱⋱0⋮⋱an−1bn−1cn−10⋯0anbn]A = \begin{bmatrix} b_1 & c_1 & 0 & \cdots & 0 \\ a_2 & b_2 & c_2 & \cdots & \vdots \\ 0 & \ddots & \ddots & \ddots & 0 \\ \vdots & \ddots & a_{n-1} & b_{n-1} & c_{n-1} \\ 0 & \cdots & 0 & a_n & b_n \end{bmatrix}

Find LL and UU such that every diagonal entry of LL is 1. When such a pair exists, it is unique.

Input

The first line contains the size NN of the square matrix (1≤N≤10001 \le N \le 1000). Each of the next NN lines contains the NN entries of one row of AA, in order.

Every entry is an integer with absolute value at most 1000, and every position with ∣i−j∣≥2|i - j| \ge 2 holds 0. For inputs whose decomposition exists, every entry of LL and UU has absolute value at most 10610^6.

Output

If the decomposition exists and no diagonal entry of UU is 0, print LL on NN lines and then UU on the next NN lines. Each line holds the NN entries of that row, separated by one space.

Round the exact value of each entry at the fourth decimal place and print three decimals. Round halves away from zero, so 0.06250.0625 becomes 0.063 and −0.0625-0.0625 becomes -0.063. When the rounded value is zero, print 0.000 without a sign.

If the decomposition is impossible, or some diagonal entry of UU is 0, print -1 on the first line and nothing else.

Examples8

  1. Example 1

    Input
    3
    1 1 0
    1 0 1
    0 1 1
    
    Expected output
    1.000 0.000 0.000
    1.000 1.000 0.000
    0.000 -1.000 1.000
    1.000 1.000 0.000
    0.000 -1.000 1.000
    0.000 0.000 2.000
    
  2. Example 2

    Input
    1
    7
    
    Expected output
    1.000
    7.000
    
  3. Example 3

    Input
    1
    0
    
    Expected output
    -1
    
  4. Example 4

    Input
    2
    0 1
    1 2
    
    Expected output
    -1
    
  5. Example 5

    Input
    4
    1 -1000 0 0
    100 1000 1 0
    0 -1 2 3
    0 0 1 4
    
    Expected output
    1.000 0.000 0.000 0.000
    100.000 1.000 0.000 0.000
    0.000 0.000 1.000 0.000
    0.000 0.000 0.500 1.000
    1.000 -1000.000 0.000 0.000
    0.000 101000.000 1.000 0.000
    0.000 0.000 2.000 3.000
    0.000 0.000 0.000 2.500
    
  6. Example 6

    Input
    3
    16 0 0
    1 16 0
    0 -1 5
    
    Expected output
    1.000 0.000 0.000
    0.063 1.000 0.000
    0.000 -0.063 1.000
    16.000 0.000 0.000
    0.000 16.000 0.000
    0.000 0.000 5.000
    
  7. Example 7

    Input
    5
    1 1 0 0 0
    1 1 1 0 0
    0 1 1 1 0
    0 0 1 1 1
    0 0 0 1 1
    
    Expected output
    -1
    
  8. Example 8

    Input
    3
    1 1 0
    1 2 1
    0 1 1
    
    Expected output
    -1