Factor a tridiagonal matrix into unit-diagonal L and upper U, printing -1 when the factors do not exist or a pivot is zero.
Medium5MatrixMathImplementationNo attempts yetTime limit3sMemory limit128 MBYou are given an N×N matrix A. An LU decomposition splits A into a product A=LU, where L is a lower triangular matrix and U is an upper triangular matrix.
This problem only deals with band matrices whose band has width 1. In other words, A carries values on the main diagonal and on the two diagonals next to it, and every other entry is 0.
A=b1a20⋮0c1b2⋱⋱⋯0c2⋱an−10⋯⋯⋱bn−1an0⋮0cn−1bn
Find L and U such that every diagonal entry of L is 1. When such a pair exists, it is unique.
The first line contains the size N of the square matrix (1≤N≤1000). Each of the next N lines contains the N entries of one row of A, in order.
Every entry is an integer with absolute value at most 1000, and every position with ∣i−j∣≥2 holds 0. For inputs whose decomposition exists, every entry of L and U has absolute value at most 106.
If the decomposition exists and no diagonal entry of U is 0, print L on N lines and then U on the next N lines. Each line holds the N 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.0625 becomes 0.063 and −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 U is 0, print -1 on the first line and nothing else.