Continued Fraction#7536

Continued Fraction

1 second
122.174 megabytes
Easy

The (simple) continued fraction representation of a real number is an expression obtained by an iterative process of representing as the sum of its integer part and the reciprocal of another number, then writing this other number as the sum of its integer part and another reciprocal, and so on. In other words, a continued fraction representation of is of the form

prb7536.gif

where a_0, a_1, a_2, ... are integers and a_1, a_2, ... > 0. We call the a_i-values partial quotients. For example, in the continued fraction representation of , the partial quotients are a_0 = 5, a_1 = 2, a_2 = 2. This representation of a real number has several applications in theory and practice. If is a rational number, then the number of partial quotients is finite.

Given two rational numbers in continued fraction representation, your task is to perform the four elementary arithmetic operations on these numbers and display the results in continued fraction representation.

Input

Consists of three lines. The first line contains two integers n_1 and n_2, where 1 \leq n_i \leq 9 is the number of partial quotients of rational number r_i for 1 \leq i \leq 2. The second line contains the partial quotients of r_1 and the third line contains the partial quotients of r_2. The absolute values of the quotients are not more than and you may assume that r_1 > r_2 > 0.

Output

Display the partial quotients of the continued fraction representations of r_1 + r_2, r_1 - r_2, r_1 * r_2 and r_1 / r_2, in order, each in a line. Consecutive partial quotients on each line are separated by a single space. Do not print any trailing zero partial quotients.

Examples

Example 1

Loading
Loading