This page is still under construction.

Parts of this page are still being built. What you see may change.

Rational Approximation

Time limit1sMemory limit128 MB

Level

Not classified yet

Solved
No attempts yet

Problem

A polynomial p(x)p(x) of degree nn can be used to approximate a function f(x)f(x) by setting the coefficients of p(x)p(x) to match the leading coefficients of the power series of f(x)f(x) (expanded about x=0x = 0). For example,

11−x≈1+x+x2+⋯+xn.\frac{1}{1-x} \approx 1 + x + x^2 + \cdots + x^n.

Unfortunately, polynomials are "nice" and do not work well when approximating functions that behave poorly (for example, functions with singularities). To overcome this, we can instead approximate f(x)f(x) by a rational function of the form p(x)/q(x)p(x)/q(x), where p(x)p(x) and q(x)q(x) are polynomials.

Given mm, nn, and the first m+nm+n coefficients of the power series of f(x)f(x), compute two polynomials p(x)p(x) and q(x)q(x) of degrees at most m−1m-1 and n−1n-1, respectively, such that the power series expansion of q(x)f(x)−p(x)q(x)f(x) - p(x) has 00 as its first m+n−1m+n-1 coefficients and 11 as the coefficient of the xm+n−1x^{m+n-1} term. In other words, find p(x)p(x) and q(x)q(x) such that

q(x)⋅f(x)−p(x)=xm+n−1+⋯ ,q(x)\cdot f(x) - p(x) = x^{m+n-1} + \cdots,

where ⋯\cdots contains terms with powers of xx higher than m+n−1m+n-1. From this, f(x)f(x) can be approximated by p(x)/q(x)p(x)/q(x).

Input

The input consists of multiple cases. Each case is given on one line in the form

m n f0 f1 ... f(m+n-1)

where fif_i is the coefficient of xix^i in the power series expansion of ff. You may assume that 1≤m1 \le m, 1≤n≤41 \le n \le 4, 2≤m+n≤102 \le m+n \le 10, and that each fif_i is an integer with ∣fi∣≤5|f_i| \le 5. The end of input is indicated by a line containing m=n=0m = n = 0 with no coefficients for ff. You may assume that the solution for each input is unique.

Output

For each test case, print two lines. Print the polynomial p(x)p(x) on the first line and q(x)q(x) on the second line.

Print the polynomial p(x)p(x) as a list of pairs (pi,i)(p_i, i) arranged in ascending order of ii, where pip_i is a non-zero coefficient of the xix^i term. Print each non-zero coefficient pip_i as a/ba/b, where b>0b > 0 and a/ba/b is in lowest terms; if b=1b = 1, print only aa (omit bb). If p(x)=0p(x) = 0, print a line containing only (0,0)(0,0). Separate the pairs in the list by a single space.

Print the polynomial q(x)q(x) in the same manner. Insert a blank line between cases.

Hint

A polynomial p(x)p(x) can be written as p0+p1x+p2x2+⋯p_0 + p_1 x + p_2 x^2 + \cdots, and likewise for q(x)q(x). In this problem the coefficients pip_i and qiq_i are rational numbers.

The power series expansion of f(x)f(x) about 00 can be written as f0+f1x+f2x2+⋯f_0 + f_1 x + f_2 x^2 + \cdots, where the fif_i are integers in this problem.

Examples2

  1. Example 1

    Input
    2 2 0 0 1 1
    4 2 1 2 3 4 5 -2
    1 1 2 3
    1 4 -5 0 -2 1 -2
    0 0
    
    Expected output
    (0,0)
    (1,1)
    
    (-4/33,0) (-1/11,1) (-2/33,2) (-1/33,3)
    (-4/33,0) (5/33,1)
    
    (2/3,0)
    (1/3,0)
    
    (25/6,0)
    (-5/6,0) (1/3,2) (-1/6,3)
    
  2. Example 2

    Input
    1 1 1 2
    0 0
    
    Expected output
    (1/2,0)
    (1/2,0)