Template

You are given N 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 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 ≤ i ≤ N) of each case
contains two integers n_(i) and d_(i), where |n_(i)|,|d_(i)| ≤ 10⁹, and
n_(i), d_(i) ≠ 0. You can assume that the value of the product of all
M_(i), where M_(i) = max (|n_(i)|,|d_(i)|), does not exceed 10¹⁸.

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 n_(m) and d_(m) — the nominator and denominator
of the smallest possible result. In the second line output n_(M) and
d_(M) — the nominator and denominator of the largest possible result.
Both should be given as irreducible fractions, with d_(m), d_(M) > 0.

Problem information

Author: Eryk Kopczynski

Generation: 2026-01-25T22:57:45.333Z

© Jutge.org, 2006–2026.
https://jutge.org
