Recovering the Common Ratio of a Geometric Sequence
Time limit1sMemory limit128 MB
Given a shuffled, partially deleted integer geometric sequence, find the common ratio with the largest absolute value (positive on ties), or 0 if none exists.
- Level
Hard8 of 10
- Topics
- Math, Number theory, Sorting, Hash map
- Solved
- No attempts yet
Problem
A sequence of integers is called a geometric sequence if the ratio between consecutive terms is constant. For example, is a geometric sequence in which each term is twice the previous one. This constant ratio is called the common ratio.
Someone took such a geometric sequence, shuffled its terms into an arbitrary order, and then removed some of them. Given the resulting collection of numbers, recover the common ratio of the original geometric sequence.
If several common ratios are possible, output the one with the greatest absolute value. If several of those tie in absolute value, output the positive one. If no geometric sequence contains all of the given numbers, output .
Because the common ratio of an integer geometric sequence is always rational, the ratio may be a fraction such as , not only an integer. A "possible common ratio" is a ratio for which there exists an integer geometric sequence that contains every given number as a term (in some order).
Input
The first line contains a single integer , the count of integers in the transformed collection ().
Each of the next lines contains one integer of the transformed collection. Every integer has absolute value at most , and no integer is .
Output
Output the common ratio as an exact reduced fraction.
- Write the ratio as a fully reduced fraction and print it in the form
p/q. - If the denominator equals , print only the integer .
- If the ratio is negative, put a leading
-on the numerator. - If no geometric sequence contains all of the given numbers, print
0.