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
where are integers and . We call the -values partial quotients. For example, in the continued
fraction representation of , the partial quotients are
. This representation of a real number has several
applications in theory and practice. If is a rational number, the partial
quotients are eventually all zero, so we only need to consider
a finite number of partial quotients.
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
The input consists of a single test case. The test case
consists of three lines. The first line contains two integers
and , where is the number of
partial quotients of rational number for . The second line
contains the partial quotients of and the third line contains the
partial quotients of . The absolute values of the
quotients are not more than 10 and you may assume that
.
Output
Display the partial quotients of the continued fraction
representations of , , , and , 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.
Sample Input 1 |
Sample Output 1 |
4 3
5 1 1 2
5 2 2
|
11
0 5
30 4 6
1 27
|