Sunny has an interesting program that takes an non-negative signed 32-bit integer and outputs an non-negative signed 32-bit integer.
It has sequence of bitwise AND and bitwise XOR operations.
When the program is run, the input integer is passed through all operations.
The result will be outputted.
The machine works fine, but Rock thinks that the program is too long.
He wants to create the shortest program that produces the same output for all inputs.
Can you help him?
Formally, a program of length $N$ consists of an array of strings $S$ and an array of integers $X$, both of length $N$.
Each element of $S$ is either "AND" or "XOR", representing the
bitwise AND operation and
bitwise XOR operation respectively.
The output of the program is $((((I \, S_1 \, X_1) S_2 \, Y_2) \dots ) S_N \, Y_N)$, where $0 \le I \lt 2^{31}$ is the input of the program.
For example, if $S=[$"AND","XOR","AND"$]$ and $X=[6,9,420]$, the output of the program is $(((I$ AND $6)$ XOR $9)$ AND $420)$
when the input integer is $I$.
One can check that this to program is equivalent to another the program with $S=[$"AND"$]$ and $X=[4]$,
meaning $(((I$ AND $6)$ XOR $9)$ AND $420)=(I$ AND $4)$ for all integers $0 \le I \lt 2^{31}$.
Input
The first line contains an integer $N$, the number of operations in the Sunny's program.
The $i$-th line of the next $N$ lines contain a string $S_i$ and an integer $X_i$, describing the $i$-th operation in Sunny's program.
($S_i$ is "AND" or "XOR")
Output
On the first line, output an integer $K$, the minimal number of operations in Rock's program.
On the $i$-th line of the next $K$ lines, output a string $T_i$ and an integer $Y_i$, describing the $i$-th operation in Rock's program.
($T_i$ is "AND" or "XOR", $0 \le Y_i \lt 2^{31}$)
If there are many solutions, output any one of them.
Subtasks
For all cases, $1 \le N \le 10^5, 0 \le X_i \lt 2^{31}$
Subtask 1 (10%): $S_i =$ "AND" for all $i$
Subtask 2 (10%): $S_i =$ "XOR" for all $i$
Subtask 3 (20%): $N \le 9$
Subtask 4 (20%): $X_i \lt 16$ for all $i$
Subtask 5 (40%): No additional constraints
Sample Test Cases
Input | Output | |
---|---|---|
3 AND 6 XOR 9 AND 420 |
1 AND 4 |
|
3 AND 3 AND 69 AND 51 |
1 AND 1 |
|
4 XOR 4 XOR 2 XOR 0 XOR 14 |
1 XOR 8 |
|
1 XOR 0 |
0 |
Scoring: Per Subtask
Authored by s17r28
Appeared in 2022 Mini Contest 1