Parentheses X93697


Statement
 

pdf   zip

You are given NN fractions:

$$({n_1 \over d_1}) / ({n_2 \over d_2}) / ({n_3 \over d_3}) / \cdots / ({n_N \over d_N})$$

Assuming that you can execute the N1N-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 N1N \geq 1. Each of the following NN lines contains a description of one fraction: i+1i+1-th line (1iN1 \leq i \leq N) of each case contains two integers nin_i and did_i, where |ni|,|di|109|n_i|, |d_i| \leq 10^9, and ni,di0n_i, d_i \neq 0. You can assume that the value of the product of all MiM_i, where Mi=max(|ni|,|di|)M_i = \max(|n_i|, |d_i|), does not exceed 101810^{18}.

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 nmn_m and dmd_m — the nominator and denominator of the smallest possible result. In the second line output nMn_M and dMd_M — the nominator and denominator of the largest possible result. Both should be given as irreducible fractions, with dm,dM>0d_m, d_M > 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++