#P1054. Equivalence of Algebraic Expressions
Equivalence of Algebraic Expressions
Equivalence of Algebraic Expressions
After learning algebraic expressions in middle school, Mingming encountered a challenging multiple‐choice question. In this problem, you are given an algebraic expression and several candidate expressions. The task is to determine which of the candidate expressions are equivalent to the given expression.
Each expression satisfies the following properties:
- The expression contains only one variable, \(a\).
- All numbers in the expression are positive integers less than 10000.
- The expression may include the four operators
+
(addition),-
(subtraction),*
(multiplication), and^
(power), as well as parentheses( )
. The operator precedence is: parentheses >^
>*
>+
/-
. Note: Operators of the same precedence, including the power operator^
, are evaluated from left to right. - The exponent in the power operator is a positive integer between 1 and 10 (inclusive).
- The expression may have extra spaces at the beginning or the end.
Examples of valid expressions include:
((a^1) ^ 2)^3
a*a+a-a
((a+a))
9999+(a-a)*a
1 + (a -1)^3
1^10^9
(evaluated left-to-right)
Your task is to write a program that, given the main expression and several candidate expressions, determines for each candidate whether it is equivalent to the main expression. Two expressions are considered equivalent if for a variety of integer values assigned to \(a\), they produce the same result. A simple method is to test several values of \(a\) and compare the evaluations.
Input and Output Format
inputFormat
The input consists of multiple lines:
- The first line contains the main algebraic expression.
- The second line contains a positive integer \(n\), the number of candidate expressions.
- The following \(n\) lines each contain one candidate algebraic expression.
All expressions may include redundant spaces.
outputFormat
For each candidate expression, output a line containing yes
if it is equivalent to the main expression, and no
otherwise.
sample
a+a
2
a*2
a+a
yes
yes
</p>