Nezzar and Lucky Number

Practice this coding question on Coding Gurukul.

Problem statement

Nezzar prints vouchers whose positive values contain his favorite digit d somewhere in decimal notation. A customer may combine any number of these vouchers, including repeated values, to pay an exact total. The cashier needs to decide which requested totals can be paid without change. Your task For each requested positive total a, determine whether it is a sum of one or more positive integers that each contain digit d. Important details The favorite digit is between 1 and 9. A voucher may contain d in any position and may contain other digits. Reusing the same value is allowed. Each query is independent and there is no limit on voucher stock; totals need not themselves contain d. Worked example For d = 3, total 16 can be paid as 13 + 3, so print YES. Total 2 cannot use even the smallest qualifying voucher, so print NO. Total 33 is itself a valid voucher and needs no split.

Input format

Read t. For each case read q d, then q requested totals.

Output format

Print YES or NO for each total, in input order, one answer per line.

Constraints

1 <= t <= 9; 1 <= q <= 1000; total q <= 1000; 1 <= d <= 9; 1 <= a <= 1000000000.

Sample input

1
3 3
16 2 33

Sample output

YES
NO
YES

Sign in to run or submit a solution in C++, Java or Python.

Loading Coding Gurukul…