Continued Fraction Arithmetic
Time limit3sMemory limit256 MB
Given two rationals as continued fractions, output the continued fractions of their sum, difference, product and quotient.
- Level
Medium5 of 10
- Topics
- Number theory, Math
- Solved
- No attempts yet
Problem
A simple continued fraction representation of a real number is built by splitting into its integer part and the reciprocal of what is left, then splitting that reciprocal the same way, and so on. The representation has the form
where every is an integer and are positive. The are called partial quotients. For 5.4 the partial quotients are , and .
An irrational number such as needs infinitely many partial quotients. A rational number has finitely many, and the list is unique once you require the last partial quotient to differ from 1. Two rational numbers are given as continued fractions. Compute their sum, difference, product and quotient, and write each result as a continued fraction.
Input
The input holds several test cases. Each test case takes three lines. The first line has two integers and (), the number of partial quotients of the rational numbers and . The second line has the partial quotients of and the third line has the partial quotients of . Every partial quotient satisfies and for , the last partial quotient of each number is never 1, and is not 0. A line holding two zeros ends the input.
Output
For each test case print a line reading Case k:, where is the number of the test case counting from 1. Then print four lines holding the continued fraction representations of , , and , in that order. Write the partial quotients of one number on a single line separated by single spaces, and never let the last partial quotient be 1. Every value that appears during the computation fits in a signed 64-bit integer.