P8799 [Lanqiao Cup 2022 National B, Group B] Gears
Description
On this day, Xiaoming is assembling gears.
He has a total of $n$ gears. The radius of the $i$-th gear is $r_{i}$. He needs to assemble these $n$ gears in some order from left to right, so that after the leftmost gear starts rotating, the motion can be transmitted to the rightmost gear. Also, these gears can be used to increase or decrease the rotation speed (angular velocity).

Looking at these gears, Xiaoming suddenly has $Q$ questions: Is it possible to assemble these gears in some order so that the rotation speed of the rightmost gear is $q_{i}$ times that of the leftmost gear?
Input Format
The input has $Q+2$ lines. The first line contains two positive integers $n, Q$, representing the number of gears and the number of queries.
The second line contains $n$ positive integers $r_{1}, r_{2}, \ldots, r_{n}$, representing the radius of each gear.
The next $Q$ lines each contain one positive integer $q_{i}$, representing a query.
Output Format
Output $Q$ lines. For each query, if there exists at least one assembly plan that satisfies the condition, output `YES`; otherwise output `NO`.
Explanation/Hint
**[Sample Explanation]**
For query $1$, one possible plan is: `23341`.
For query $2$, one possible plan is: `42331`.
For query $3$, there is no plan.
**[Constraints and Notes on Testdata]**
For $15\%$ of the testdata, it is guaranteed that $n, Q \leq 100$.
For $30\%$ of the testdata, it is guaranteed that $n, Q \leq 2000$.
For $100\%$ of the testdata, $n \ge 2$, $n, Q \leq 2 \times 10^{5}$, and $r_{i}, q_{i} \leq 2 \times 10^{5}$.
Lanqiao Cup 2022 National Finals, Group B, Problem I.
Translated by ChatGPT 5