This page is still under construction.

Parts of this page are still being built. What you see may change.

Choose Your Own Arithmetic

Time limit2sMemory limit512 MB

Summary
Given working digits and exactly W add-or-multiply steps applied left to right from a single digit, decide whether each target value is reachable.
Level

Medium5 of 10

Topics
Brute force, Dynamic programming, Math, Implementation
Solved
No attempts yet

Problem

In Waterloo you have probably seen a goose or two. But how do you make geese appear on a calculator? Start with 66, add 77, multiply by 66, multiply by 88, add 77, multiply by 88, and multiply by 77. You get 3533635336. Flip the calculator upside down and it spells gEESE:

gEESE spelled on an upside-down calculator

You want a program that builds tricks like this automatically. The trouble is that your calculator has many broken buttons: the only operators that still work are ++ and ×\times, and only some of the digit keys respond. Given a target number, decide whether your half-broken calculator can reach it using single-digit inputs and a fixed number of operations.

Note: the calculator applies each operation the instant it is entered and ignores the usual order of operations. For example, 2+3×42 + 3 \times 4 is evaluated left to right as (2+3)×4=20(2 + 3) \times 4 = 20, not 2+12=142 + 12 = 14.

Input

The first line contains WW, the exact number of operations you must perform (0≤W≤60 \le W \le 6).

The second line contains DD, the number of working digit keys (1≤D≤101 \le D \le 10).

Each of the next DD lines contains one working digit. These digits are distinct integers from 00 to 99.

The next line contains VV, the number of target values (1≤V≤51 \le V \le 5).

Each of the following VV lines contains one integer between 00 and 50000005000000 inclusive: a value you would like your calculator to show.

Output

Print VV lines, one per target value. For each target print Y if it can be reached and N if it cannot, using exactly WW operations with the DD available digits.

Formally, a target TT is reachable if you can begin with one of the DD digits and then, by adding or multiplying by one of the digits exactly WW times, end at TT. Digits may be reused and you do not have to use all of them. You may never enter a multi-digit number.

Examples2

  1. Example 1

    Input
    6
    3
    6
    7
    8
    1
    35336
    
    Expected output
    Y
    
  2. Example 2

    Input
    3
    2
    4
    9
    2
    97
    88
    
    Expected output
    N
    Y