Choose Your Own Arithmetic
Time limit2sMemory limit512 MB
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 , add , multiply by , multiply by , add , multiply by , and multiply by . You get . Flip the calculator upside down and it spells gEESE:

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 , 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, is evaluated left to right as , not .
Input
The first line contains , the exact number of operations you must perform ().
The second line contains , the number of working digit keys ().
Each of the next lines contains one working digit. These digits are distinct integers from to .
The next line contains , the number of target values ().
Each of the following lines contains one integer between and inclusive: a value you would like your calculator to show.
Output
Print lines, one per target value. For each target print Y if it can be reached and N if it cannot, using exactly operations with the available digits.
Formally, a target is reachable if you can begin with one of the digits and then, by adding or multiplying by one of the digits exactly times, end at . Digits may be reused and you do not have to use all of them. You may never enter a multi-digit number.