#P14367. [ICPC 2024 NAC] Comparator
[ICPC 2024 NAC] Comparator
Problem Description
Many programming languages allow custom comparators for sorting user-defined objects.
In the fictional language IFFY, every program is a comparator over two bitstrings (called words).
Words are 1-indexed from left to right.
An IFFY program is a sequence of if-statements.
Each statement has the form:
a b expr r
where:
ais the index of a bit chosen from the first word,bis the index of a bit chosen from the second word,expris a Boolean expression involving variablesxand/ory,ris the return value (0or1) if the expression evaluates to1.
Here:
xdenotes the chosen bit from the first word,ydenotes the chosen bit from the second word.
The function evaluates the if-statements in order.
As soon as one expression evaluates to 1, the function immediately returns r.
If no if-statement triggers, the function returns the value given on the last line of the input.
For example, the line
2 3 x|y 0
means: look at the second bit of the first word and the third bit of the second word; if x OR y = 1, then return 0, otherwise continue.
Expression Grammar
Valid expressions are defined recursively:
x,y,0, and1are valid expressions.- If
Eis valid, then(E)is valid. - If
Eis valid, then!Eis valid. - If
E1andE2are valid, thenE1 BIN_OP E2is valid, whereBIN_OPis one of:=: equality&: and|: or^: xor
Operator Precedence
From highest to lowest:
- Parentheses
- Unary
! - Binary
= - Binary
& - Binary
| - Binary
^
All binary operators are left-associative.
Truth Tables
Unary Operator
| x | !x |
|---|---|
| 0 | 1 |
| 1 | 0 |
Binary Operators
Equality =
| x | y | x = y |
|---|---|---|
| 0 | 0 | 1 |
| 1 | 0 | |
| 1 | 0 | |
| 1 | ||
And &
| x | y | x & y |
|---|---|---|
| 0 | 0 | 0 |
| 1 | ||
| 1 | 0 | |
| 1 | ||
Or |
| x | y | x | y |
|---|---|---|
| 0 | 0 | |
| 1 | 1 | |
| 1 | 0 | |
| 1 | ||
Xor ^
| x | y | x ^ y |
|---|---|---|
| 0 | 0 | |
| 1 | 1 | |
| 1 | 0 | |
| 1 | 0 | |
Comparator Properties
Let f(x, y) be the comparator result for two words x and y.
A proper comparator should represent an ordering <, meaning f(x, y) = 1 exactly when x < y.
You must count violations of these three properties:
- Reflexive condition: for all
x,f(x, x)should return0. - Symmetric condition: for all
x, y, iff(x, y) = 1, thenf(y, x)must be0. - Transitive condition: for all
x, y, z, iff(x, y) = 1andf(y, z) = 1, thenf(x, z)must also be1.
Task
Given an IFFY comparator and a fixed word length k, compute:
- the number of words for which the reflexive condition fails,
- the number of ordered pairs of words for which the symmetric condition fails,
- the number of ordered triples of words for which the transitive condition fails.
Input
The first line contains two integers n and k:
0 <= n <= 2 * 10^51 <= k <= 10
where:
nis the number of if-statements,kis the length of the bitstrings being compared.
Each of the next n lines contains four tokens of the form:
a b expr r
with:
1 <= a, b <= kexpra valid expressionrequal to0or1
The final line contains the return value used if no if-statement triggers.
The total length of all expressions is at most 10^6.
Output
Output one line with three space-separated integers:
- the number of words violating the reflexive condition,
- the number of pairs violating the symmetric condition,
- the number of triples violating the transitive condition.
Sample Input 1
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
Sample Output 1
0 0 0
Sample Input 2
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
Sample Output 2
3 25 52