Decide if any pedal gear, two distinct extra gears, and tire gear combine to each target ratio P/Q.
Easy2Brute forceMathNo attempts yetTime limit5sMemory limit512 MBA typical mountain bike has two gear groups: one attached to the pedals and one attached to the rear tire. A gear group holds several gears whose tooth counts differ. A chain joins one gear of the pedal group to one gear of the tire group, and that choice fixes the ratio between the pedaling speed and the tire speed. For example, if the chain joins a pedal gear with 5 teeth to a tire gear with 10 teeth, the ratio is 1/2, because the rider turns the pedal gear twice to turn the tire once. The rider may move the chain to join any gear of the pedal group to any gear of the tire group.
You have just bought a special mountain bike with three gear groups: one on the pedals, one on the tire, and one extra group between them. This bike has two chains. The first chain must always join one gear of the pedal group to one gear of the extra group, and the second chain must always join one gear of the extra group to one gear of the tire group. The two chains cannot use the same gear of the extra group.
The ratio is the number of turns the tire makes while the pedal gear makes one turn. Given the tooth counts of the gears in the pedal, extra and tire groups, decide whether the ratio can be made exactly P/Q. Several such questions are asked about one set of gears.
The first line contains the number of test cases T. The T test cases follow.
The first line of each test case contains three integers Np, Ne and Nt, the numbers of gears in the pedal, extra and tire groups. The next three lines contain Np, Ne and Nt integers, the tooth counts of the gears in the pedal group, the extra group and the tire group in that order. Within one group all tooth counts are distinct. The next line contains the number of questions M. The last M lines each contain two integers P and Q, the target ratio. P/Q is not guaranteed to be in lowest terms.
For each test case, first print Case #x: on its own line, where x is the test case number starting from 1. Then print one line for each question of that test case, in the order the questions were given: Yes if the ratio can be made exactly P/Q, and No otherwise.
Take the first sample test case. The ratio 1/1 asked by the first question would need both chains on the same gear of the extra group, which is not allowed, so it is impossible.
For the second question, put the first chain on the pedal gear with 5 teeth and the extra gear with 4 teeth, and put the second chain on the extra gear with 6 teeth and the tire gear with 3 teeth. The ratio is then 5/2.