Quadratic Irrationality Representation#5968

Quadratic Irrationality Representation

1 second
64 megabytes
Hard

It is known that any finite or periodic continued fraction can be represented either as a rational fraction or as a quadratic irrationality.

For the given fraction, you are required to find one of these representations.

Input

The input consists of a single line containing the continued fraction. If the fraction is finite, it is given in the format [a_0, a_1, \ldots, a_{n-1}], where 1 \le n \le 13. If the fraction is periodic, it is given in the format [a_0, \ldots, a_{p-1}, (a_p, \ldots, a_{p+l-1})], where p \ge 0, l > 0, and p + l \le 13. The constraints for the coefficients are -5 \le a_0 \le 5 when p > 0, and 1 \le a_i \le 5 for i > 0.

Output

Output a single line representing the continued fraction as either a rational , where and are integers, c > 0, or as a quadratic irrationality (a + b\sqrt{N})/c (or (a - b\sqrt{N})/c), where , , , are integers, a, b > 0, c > 0, N > 0. If both representations are possible, output the rational one. If there are several possible representations, choose the one with the minimal , and among those, the one with the minimal .

Examples

Example 1

Loading
Loading