#P7774. Angle Combination Problem

    ID: 20960 Type: Default 1000ms 256MiB

Angle Combination Problem

Angle Combination Problem

Given N initial angles ai and M target angles bi, determine for each target angle bi whether it can be obtained by adding and subtracting any number of the initial angles. Note that each initial angle ai can be used any number of times (including zero) in the operations.

Mathematically, you need to decide if for each bi there exists integers x1, x2, …, xN (which can be negative, zero, or positive) such that:

[ b_i = x_1 \times a_1 + x_2 \times a_2 + \cdots + x_N \times a_N ]

A key observation is that the set of values that can be obtained using integer combinations of ai is exactly the set of all multiples of (d), where (d) is the (\gcd(a_1, a_2, \ldots, a_N)). Therefore, the answer for a given bi is "YES" if and only if (d) divides (b_i); otherwise, it is "NO".

inputFormat

The first line contains an integer N denoting the number of initial angles. The second line contains N space-separated integers representing a1, a2, ..., aN. The third line contains an integer M denoting the number of target angles. The fourth line contains M space-separated integers representing b1, b2, ..., bM.

outputFormat

Output M lines. The i-th line should be "YES" if the target angle bi can be obtained by adding and subtracting any number of the initial angles; otherwise, output "NO".

sample

3
1 2 3
4
1 5 7 4
YES

YES YES YES

</p>