#P8799. Gear Assembly Ratio

    ID: 21963 Type: Default 1000ms 256MiB

Gear Assembly Ratio

Gear Assembly Ratio

In this problem, you are given n gears with radii \(r_1, r_2, \dots, r_n\). You need to arrange all these gears in a line from left to right. When the leftmost gear rotates, the rotation is transmitted to the right so that only the first and the last gear affect the overall speed conversion.

The key observation is that when two gears with radii \(r_a\) and \(r_b\) mesh, their angular speeds \(\omega_a\) and \(\omega_b\) satisfy \[ \omega_b = \frac{r_a}{r_b} \omega_a. \] If you have a sequence of gears \(r_{a_1}, r_{a_2}, \dots, r_{a_n}\) arranged in that order, the overall ratio between the angular speeds of the leftmost and rightmost gears is given by \[ \frac{\omega_{a_1}}{\omega_{a_n}} = \frac{r_{a_1}}{r_{a_n}}. \]

For each query with an integer multiplier \(q_i\), your task is to determine if there exists an arrangement (i.e. a permutation of the given gears) such that the ratio of the leftmost gear's radius to the rightmost gear's radius is exactly \(q_i\); that is,

[ r_{first} = q_i \times r_{last}. ]

Note: Since the gears in the middle do not affect the overall ratio, the problem reduces to determining whether there exists a pair of distinct gears \(r_i\) and \(r_j\) satisfying \(r_i = q_i \times r_j\). In the special case when \(q_i = 1\), you must check if there are at least two gears with the same radius.

inputFormat

The input consists of several lines:

  • The first line contains an integer \(n\) (the number of gears).
  • The second line contains \(n\) space-separated positive integers \(r_1, r_2, \dots, r_n\) representing the radii.
  • The third line contains an integer \(Q\) (the number of queries).
  • Each of the following \(Q\) lines contains one integer \(q_i\), representing the desired ratio between the leftmost and rightmost gear.

outputFormat

For each query, output a single line containing "Yes" if it is possible to arrange the gears so that the ratio of the leftmost gear's radius to the rightmost gear's radius equals \(q_i\); otherwise, output "No".

sample

4
2 3 6 4
3
3
1
2
Yes

No Yes

</p>