Parentheses X93697


Statement
 

pdf   zip

html

You are given N fractions:

(
n1 
d1
) / (
n2 
d2
) / (
n3 
d3
) / ⋯ / (
nN 
dN
)

Assuming that you can execute the N−1 divisions in any way you want, your task is to find the smallest and largest result that can be obtained.

Input

Input consists of several cases. The first line of each case contains a single number N ≥ 1. Each of the following N lines contains a description of one fraction: i+1-th line (1 ≤ iN) of each case contains two integers ni and di, where |ni|, |di| ≤ 109, and ni, di ≠ 0. You can assume that the value of the product of all Mi, where Mi = max(|ni|, |di|), does not exceed 1018.

Edit: Changed the limit to the one that was actually used.

The input ends with a single line containing 0.

Output

In the first line output nm and dm — the nominator and denominator of the smallest possible result. In the second line output nM and dM — the nominator and denominator of the largest possible result. Both should be given as irreducible fractions, with dm, dM > 0.

Public test cases
  • Input

    3
    1 2
    3 4
    1 3
    0
    

    Output

    2 9
    2 1
    
  • Information
    Author
    Eryk Kopczynski
    Language
    English
    Official solutions
    Unknown.
    User solutions
    C++