#P16084. [ICPC 2024 NAC] Comparator
[ICPC 2024 NAC] Comparator
题目描述
Many programming languages let you define custom comparators to sort user-defined objects.
IFFY is a programming language where the only programs are functions intended to be comparators. These functions operate on bitstrings, hereafter called “words”. Words are 1-indexed starting with the leftmost character.
To write a function on two words in IFFY, the programmer writes a series of if-statements. Each if statement chooses a bit from the first word, a bit from the second word, and evaluates some expression on those two bits. If the expression evaluates to on those chosen bits, then the function returns immediately with a specified return value. Otherwise, execution falls through to the next if statement, not returning anything.
The syntax of each if statement is a b expr r, where the integer specifies a bit from the first word, integer specifies a bit from the second word, and is a boolean expression using variables and/or , and is the value ( or ) returned from the function if the expression evaluates to . The variable refers to the specified bit in the first word, and the variable refers to the specified bit in the second word. For example, consider the if statement ‘2 3 x|y 0’. This expression means: if the second bit of the first word or the third bit of the second word is set, then return , otherwise continue with the next statement.
The grammar for valid expressions is as follows:
x,y,0, and1are valid expressions.- If is a valid expression, then
(E)is a valid expression. - If is a valid expression, then
!Eis a valid expression. - If and are valid expressions, then all strings of the form
E1 BIN_OP E2are valid expressions, whereBIN_OPis one of=(equals),&(and),|(or), or^(xor).
Parentheses take highest precedence, followed by the unary !, followed by the binary =, &, |, and ^, in that order. All binary operators are left-associative.
Here are the truth tables for each operator:
Figure C.1: The truth table for the sole unary operator.
Figure C.2: Truth tables for the binary operators.
In order for a function to operate properly as a comparator, it must satisfy certain properties. Informally, for to be a comparator, it should impose some ordering , where returns if and only if . For example, sample 1 is a valid comparator to sort bitstrings of length . More formally, three of the properties that comparators must satisfy are the reflexive, symmetric, and transitive properties, as follows:
- Reflexive: For all , should return .
- Symmetric: For all , if returns , then must return .
- Transitive: For all , if both and return , then so must .
Given a function in the IFFY language, determine how well it satisfies these three properties by counting how often they are violated. First, among all words of a given length, count the number of words for which the reflexive property fails. Next, count the number of pairs of words for which the symmetric property fails. Finally, count the number of triples of words for which the transitive property fails.
输入格式
The first line of input contains two integers () and (), where is the number of lines in the function and is the number of bits in the values being compared.
Each of the next lines contains four tokens of the form a b expr r, where and () are the indices of the bits to consider in and , expr is a valid expression, and () is the return value. The final line gives the returned value if no if statement is triggered. The total length of all expressions is at most .
输出格式
Output a single line with three space separated integers. The first integer is the number of words for which the reflexive property is violated, the second is the number of pairs of words for which the symmetric property is violated, and the third is the number of triples of words for which the transitive property is violated.
3 2
1 1 (x=0)&(y=1) 1
1 1 (x=1)&(y=(x^x)) 0
2 2 (x=1)|(y=0) 0
1
0 0 0
4 3
2 1 x=0&(y=1) 1
1 2 !x^!!y 0
2 3 ((x|1)=y)&1&1 1
3 1 !x&!x&!x 0
1
3 25 52