#P16848. [GKS 2021 #C] Binary Operator
[GKS 2021 #C] Binary Operator
Problem Description
You are given a list of valid arithmetic expressions using non-negative integers, parentheses , plus , multiply , and an extra operator . The expressions are fully parenthesized and in infix notation.
A fully parenthesized expression is an expression where every operator and its operands are wrapped in a single parenthesis.
For example, the expression becomes when fully parenthesized, and becomes . However, is still when fully parenthesized, because it consists of a single number and no operators. is not considered fully parenthesized because it has redundant parentheses.
The operators and denote addition and multiplication, and can be any total function.
You want to group the expressions into equivalence classes, where expressions are in the same equivalence class if and only if they are guaranteed to result in the same numeric value, regardless of which function represents.
You can assume that represents the same function across all expressions in a given test case. That might mean that represents some known function like addition or subtraction, but not both in different parts of the same test case.
For example, consider the following expressions:
$$\begin{aligned} F_1&=((1\#(1+1))+((2\#3)*2))\\ F_2&=(((2\#3)+(1\#2))+(2\#3))\\ F_3&=((2*(2\#3))+(1\#2)) \end{aligned}$$Let , and let . Then we can say , regardless of the function represents because the expressions can be rewritten as:
$$\begin{aligned} F_1&=((1\#2)+((2\#3)*2))=(A+(B*2))=(A+2B)\\ F_2&=(((2\#3)+(2\#3))+(1\#2))=((B+B)+A)=(A+2B)\\ F_3&=((2*(2\#3))+(1\#2))=((2*B)+A)=(A+2B) \end{aligned}$$However, consider the expressions and . If represents addition, then . However, if is , such that is a non-zero integer, then since . Therefore and are not in the same equivalence class.
Input Format
The first line of the input gives the number of test cases, . test cases follow. Each test case begins with a line containing the integer . lines follow. The -th line contains one expression, .
Output Format
For each test case, output one line containing Case #: , where is the test case number (starting from ) and is the lexicographically smallest sequence satisfying the conditions below:
- , where denotes the total number of equivalence classes in a given test case.
- if and only if and are in the same equivalence class.
3
7
(1*(1#2))
(0*(1#2))
(1#2)
0
(3*0)
((1#2)*1)
(((1+(1#2))+3)*0)
5
(1*((1+(2#2))+3))
((0+(2#2))+4)
(100#2)
(((1+(2#2))+3)*1)
((50*2)#2)
2
(9999999999999999999999999999999999999999+1)
(100000000000000000000*100000000000000000000)
Case #1: 1 2 1 2 2 1 2
Case #2: 1 1 2 1 2
Case #3: 1 1
1
9
((2*(2#3))+(1#2))
(0*(1#2))
0
((1#(1+1))+((2#3)*2))
(3*0)
(1#(2#3))
(((2#3)+(1#2))+(2#3))
(4#7)
(7#4)
Case #1: 1 2 2 1 2 3 1 4 5
Hint
This sample test set contains test cases.
Test case has expressions and a total of equivalence classes, denoted and .
, , , .
, , and belong to , and , , , and belong to .
There are sequences of that satisfy the requirement about equivalence classes in test case : and .
Since is the lexicographically smaller one, the output for test case is: Case #: .
Test case has expressions and a total of equivalence classes, denoted and .
, , and belong to , and and belong to .
Therefore, the output for test case is: Case #: .
Test case has expressions that do not contain any .
These two expressions evaluate to the same value, and therefore belong to the same equivalence class.
In the provided sample , there are a total of equivalence classes. The first expression in the input is . All expressions from its equivalence class are denoted with in the output. The equivalence class denoted with consists of , , and . The equivalence class denoted with consists of . Finally, the last two expressions, and , are not equivalent to any of the prior expressions or to one another. Note that is one of many other sequences that satisfy the requirement about equivalence classes the given input, but it is not a correct answer because this sequence is not the lexicographically smallest one.
Limits
The length of is at most , for all .
will be valid, for all .
Test Set
No more than one in each expression.
Test Set
No additional constraints.