Cog-Wheels
Time limit1sMemory limit128 MB
Given wheel sizes that all divide the smallest, decide for each target ratio a:b whether an unlimited gear train of meshing wheels realizes exactly that ratio.
- Level
Medium7 of 10
- Topics
- Number theory, Math, Greedy, Implementation
- Solved
- No attempts yet
Problem
Your younger sister has a mechanical construction kit containing many cog-wheels of different sizes. She likes building gear trains with various transmission ratios, but some ratios are hard to achieve and others are impossible. For each requested ratio, decide whether it can be realized with the wheels in the kit.
A transmission connects two meshing wheels and is written c:d, where c and d are the numbers of cogs of the two wheels. A gear train is a chain of transmissions
in which the second wheel of each transmission shares an axis with the first wheel of the next one (wheel and wheel are on the same axis, for ). If the first wheel turns once, the last wheel turns
times, and this product is the ratio realized by the train.
For example, with wheels of 6, 12, and 30 cogs, the ratio can be realized by the train 12:30 12:6, because . In contrast, the ratio cannot be realized with those wheels.
You have an unlimited supply of wheels of each size, and you may use any number of transmissions. Given the available wheel sizes and a target ratio , decide whether some gear train realizes exactly that ratio.
Input
The input contains several sets of cog-wheels, each followed by a list of ratios to test.
Each set begins with a line whose first number is (), the number of distinct wheel sizes, followed by the sizes . Every wheel has between 5 and 100 cogs, and the number of cogs on every wheel is divisible by the number of cogs on the smallest wheel of that set. You have an infinite supply of each size.
After the sizes comes the list of ratios. Each ratio is a line with two integers and (, ), meaning the ratio . A line 0 0 ends the list of ratios for the current set.
After the last set, a line containing a single 0 (in place of a wheel count) ends the input.
Output
For each set of cog-wheels, output a section. Begin the section with a line Set #k, where is the 1-based number of the set.
Then, for each requested ratio (in input order), output one line. If the ratio can be realized with the wheels of that set, output
Ratio aj:bj: Possible
otherwise output
Ratio aj:bj: Impossible
Here and are the numbers exactly as given in the input.
Print one blank line after every section.