Cog-Wheels
Time limit1sMemory limit128 MB
Given a set of cog sizes where every size is a multiple of the smallest, decide for each ratio a:b whether it can be realized by chaining products of available sizes.
- Level
Medium7 of 10
- Topics
- Number theory, Math, Greedy, Dynamic programming
- Solved
- No attempts yet
Problem
Your little sister has just got a new mechanical building kit that includes many cog-wheels of different sizes. She starts building gears with different ratios, but soon notices that some ratios are quite difficult to realize, and some others she cannot realize at all. She would like a computer program that tells her which ratios can be realized and which cannot. She asks you to write that program.
For example, suppose the kit contains cog-wheels with 6, 12, and 30 cogs, and she wants to realize a gear of ratio 5 : 4. One possible solution is shown below.

Figure: A combination of cog-wheels realizing a gear of 5 : 4.
It shows a complete gear of ratio 5 : 4. Four wheels are used: cog-wheels of sizes 30 and 12 on the first axis, and cog-wheels of sizes 6 and 12 on the second axis. The gear ratio is
as desired. In contrast, a gear of ratio 1 : 6 cannot be realized with the cog-wheels she has.
Given the sizes of the cog-wheels in the kit (that is, the number of cogs they have), decide whether a given gear ratio can be built. You may use any finite number of cog-wheels of each available size.
Input
The input begins with a line containing the number of scenarios.
Each scenario starts with a description of the cog-wheels in the kit. First, a line contains the number of different cog-wheel sizes (). The next line contains integers , separated by single blanks; these are the different cog-wheel sizes in the kit, with for . You may assume that the kit contains a cog-wheel of the smallest size such that every size is a multiple of .
The description of the available cog-wheels is followed by the list of gear ratios to realize. It starts with a line containing the number of ratios. Each of the next lines contains two integers and , separated by a single blank, denoting the ratio , with .
Output
For every scenario, the output begins with a line containing "Scenario #i:", where i is the scenario number starting at 1. Then, for each gear ratio given in that scenario, print the result. For each gear ratio , print a line containing either
Gear ratio a:b can be realized.
or
Gear ratio a:b cannot be realized.
Separate the output of consecutive scenarios with a blank line.