Comparator
Time limit3sMemory limit2048 MB
Given a k-bit comparator defined by if-statements over bit pairs, count the words that break reflexivity and the pairs and triples that break symmetry and transitivity.
- Level
Medium6 of 10
- Topics
- Bit manipulation, Matrix, Brute force
- Solved
- No attempts yet
Problem
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, 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 an expression on those two bits. If the expression evaluates to on those chosen bits, the function returns immediately with a specified return value. Otherwise, execution falls through to the next if statement.
The syntax of each if statement is a b expr r. The integer specifies a bit from the first word, the integer specifies a bit from the second word, expr is a boolean expression using the variables x and/or y, and is the value ( or ) returned if the expression evaluates to . The variable x refers to the chosen bit in the first word, and y refers to the chosen bit in the second word. For example, the if statement 2 3 x|y 0 means: if the second bit of the first word or the third bit of the second word is set, return . Otherwise, continue with the next statement.
The grammar for valid expressions is as follows:
x,y,0, and1are valid expressions.- If
Eis a valid expression, then(E)is a valid expression. - If
Eis a valid expression, then!Eis a valid expression. - If
E1andE2are valid expressions, then all strings of the formE1 BIN_OP E2are valid expressions, whereBIN_OPis one of=(equals),&(and),|(or), or^(xor).
Parentheses take the highest precedence, followed by the unary !, followed by the binary =, &, |, and ^, in that order. All binary operators are left-associative.
The truth table for the unary operator is shown below.

Figure C.1: The truth table for the sole unary operator.

Figure C.2: Truth tables for the binary operators.
For a function to work as a comparator, it must satisfy certain properties. Informally, it should impose an ordering , where returns if and only if . For example, sample 1 is a valid comparator for bitstrings of length 2. Three of the required properties are the following.
- Reflexive: For all , returns 0.
- Symmetric: For all , if returns 1, then returns 0.
- Transitive: For all , if both and return 1, then returns 1.
Given a function in IFFY, count how often each property is violated. First, among all words of a given length, count the words for which the reflexive property fails. Next, count the pairs of words for which the symmetric property fails. Finally, count the triples of words for which the transitive property fails.
Input
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
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. The third is the number of triples of words for which the transitive property is violated.