CCO '02 P2 - Game Show Math

View as PDF

Submit solution

Points: 12
Time limit: 2.0s
Memory limit: 64M

Problem types
Canadian Computing Competition: 2002 Stage 2, Day 1, Problem 2

A game show in Britain has a segment where it gives its contestants a sequence of positive numbers and a target number. The contestant must make a mathematical expression using all of the numbers in the sequence and only the operators: +, -, *, and, /. Each number in the sequence must be used exactly once, but each operator may be used zero to many times. The expression should be read from left to right, without regard for order of operations, to calculate the target number. It is possible that no expression can generate the target number. It is possible that many expressions can generate the target number.

There are three restrictions on the composition of the mathematical expression:

  • the numbers in the expression must appear in the same order as they appear in the input file
  • since the target will always be an integer value (a positive number), you are only allowed to use / in the expression when the result will give a remainder of zero.
  • you are only allowed to use an operator in the expression if its result is an integer from -32\,000 \dots 32\,000.

Input Specification

The input file describes multiple test cases. The first line contains the number of test cases n.

Each subsequent line contains the number of positive numbers in the sequence, k (1 \le k \le 100), followed by k positive numbers, followed by the target number. Each number is at most 32\,000.

Output Specification

The output for each case should contain an expression, including all k numbers and k-1 operators plus the equals sign and the target. Do not include spaces in your expression. Remember that order of operations does not apply here. If there is no expression possible, output NO EXPRESSION.

Sample Input

3
3 5 7 4 3
2 1 1 2000
5 12 2 5 1 2 4

Sample Output

5+7/4=3
NO EXPRESSION
12-2/5*1*2=4

Comments

There are no comments at the moment.