GUESSNM2 - Guess the Numbers

no tags 

John has never been very good at maths. Due to his bad grades, his parents have sent him to the Academic Coalition of Mathematics (ACM). Despite the large amount of money his parents are spending on the ACM, John does not pay much attention during classes. However, today he has begun to think about all the effort his parents are putting into his education, and he has started to feel somewhat… guilty. So he has made a decision: he is going to improve his maths grades!

However, no sooner had he resolved to pay attention than the lesson ended. So the only thing he has been able to do is to hurriedly copy the content of the blackboard in his notebook. Today, the teacher was explaining basic arithmetic expressions with unknowns. He vaguely remembers that his classmates have been substituting values into the unknowns to obtain the expressions’ results. However, in all the hurry, John has only written down expressions, values and results in a messy fashion. So he does not know which value comes with each unknown, or which result goes with each expression.

That is the reason he needs your help: he wants to know, given an expression, some values and a result, whether it is possible or not to assign those values to the unknowns in order for the expression to evaluate to the given result. The particular assignment of values does not matter to John, as he wants to do it by himself. He only wants to know whether it is possible or not.


Each test case in the input file consists of two lines:

  • The first line contains a sequence of natural numbers. The first one (1 ≤ n ≤ 5) is the number of unknowns that will occur in the expression. It is followed by a sequence of n integers v1vn (0 ≤ vi ≤ 50), which are the values to be assigned to the unknowns. Finally, there is an integer m (0 ≤ m ≤ 1000) representing the desired result of the evaluation of the expression.
  • The second line contains an arithmetic expression composed of lowercase letters (a-z), brackets (( and )) and binary operators (+, -, *). This expression will contain n unknowns, represented by n different lowercase letters, without repetitions. The expression will not contain any blanks and will always be syntactically correct, i.e. it is just an unknown or has the form (e1 op e2 ), where e1 and e2 are expressions and op is one of the three possible binary operators.

The input will finish with a dummy test case of just one line containing 0 0, which must not be processed.


For each test case, print a single line with YES if there exists an assignment of the values v1vn to the unknowns such that the expression evaluates to m, and NO otherwise. Notice that each value vi must be assigned to exactly one unknown.

Sample Input

3 2 3 4 14
2 4 3 11
1 2 2
0 0

Sample Output


Problemstter: Enrique Martín Martín

hide comments
:D: 2012-06-10 14:53:50

Re LeppyR64:
It's invalid. Each '()' pair directly encloses exactly one binary operator.

Re Vratika Ghatiya:
It's invalid. Every argument (letter) is used exactly once.

LeppyR64: 2012-01-11 06:51:48

Is this valid?
1 1 1
0 0

Last edit: 2012-01-11 06:52:14
David García Soriano: 2011-12-12 13:22:06

@Vratika Ghatiya: No.

Vratika Ghatiya: 2011-12-12 07:21:45

For the above problem is this a valid test case ..??

2 1 3 5
0 0


Last edit: 2011-12-12 07:28:18
David García Soriano: 2011-11-28 19:09:37

Read the problem statement. The expression you wrote is not valid.

Last edit: 2011-11-28 19:11:24
A. Muh. Primabudi: 2011-11-28 18:35:57

in this problem,
what is the answer for

9 or 7?

@ProblemSetter: Thanks Got AC :D

Last edit: 2011-11-28 19:26:40

Added by:David García Soriano
Time limit:0.583s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Southwestern Europe Regional, SWERC 2011