## CCO '12 P1 - Choose Your Own Arithmetic

View as PDF

Points: 7
Time limit: 1.0s
Memory limit: 64M

Problem type
##### Canadian Computing Competition: 2012 Stage 2, Day 1, Problem 1

In Waterloo, you probably have seen some geese. How can you see geese with your calculator? Start with 6, add 7, multiply by 6, multiply by 8, add 7, multiply by 8, and multiply by 7, giving 35336. Then if you flip your calculator upside down, it says gEESE.

You want to write a program to help automatically build tricks of this type. However, your calculator has a lot of broken buttons: the only mathematical operators that work are and , and only a few of the digits work. Your goal is to figure out whether your half-broken calculator can achieve a given target value, using single-digit inputs and a fixed number of operations.

Note: the calculator performs the operations as soon as they are entered, rather than following any rules for order of operations (see Sample Input 2).

#### Input Specification

The first line of input is , the exact number of operations you must use. will be an integer between and . The second line of input is , the number of working digit keys. On each of the following lines, a working digit is given; these values are distinct integers from to . Finally, an integer is given, the number of target values; on each of the following lines there is an integer between and (inclusive) giving a target value which you'd like to achieve on your calculator.

#### Output Specification

The output consists of lines corresponding to the target values; each line contains Y if that target value can be achieved, and N if it cannot be achieved, using exactly operations with the given digits.

Precisely, a target value can be achieved if, starting with one of the digits, and then by adding or multiplying exactly times by one of the digits, you end up with . Digits can be re-used, and you do not need to use all of the digits. You cannot enter multi-digit numbers.

#### Sample Input 1

6
3
6
7
8
1
35336

#### Output for Sample Input 1

Y

#### Sample Input 2

3
2
4
9
2
97
88

#### Output for Sample Input 2

N
Y
##### Explanation

First line: we cannot achieve using the rules of this calculator, so the output is N (even despite that , when the typical order of operations rules are taken into account). Second line: start with , add , add , and multiply by ; this gives .

## Comments

• commented on Aug. 24, 2020, 5:24 p.m.

how is my code outputting everything but still getting TLE? there are 5 values in the clipped output, and the worst case should not output more than 5?