Problem H
Echidna Array

redAttention: This problem
is HARD! Only attempt to solve it after you are done with the
other problems.
Edna the echidna was walking around and found an array in the wild!
After careful analysis she realized that this array has size $N$ where $N$ is a power of $2$. Analysing further she realized that the array was actually a permutation of the numbers from $0$ to $N-1$. In other words, each number between $0$ and $N-1$ appears exactly once in the array.
Edna likes order in her arrays, so she would like to rearrange the wild array so that it is in increasing order.
Edna asked for help, and she learned $3$ tricks:
-
A fish called Wanda likes a pair of numbers $x$ and $y$. She taught Edna how to swap the positions of the elements $x$ and $y$.
-
Kermit the parrot taught Edna how to choose any number $k$ ($1 \le k < N$) and add it to all elements of the array (sums are carried modulo $N$). ($a_ i = (a_ i + k) \% N$ in most programming languages).
-
Finally, Skimpy the cat taught Edna how to choose any number $s$ ($1 \le s < N$) and replace every element of the array $a_ i$ with the exclusive or between $a_ i$ and $s$. ($a_ i = a_ i \wedge s$ in most programming languages)
Edna can use these operations as many times as she wants. You are to write a program that helps our spiky friend to sort the wild array, or tell her it’s impossible.
Input
The first line of the input contains a single integer $N$. $N = 2^ k$ with $1 \le k \le 10$.
The second line contains two integers $x$ and $y$, with $0 \le x < y < N$, the numbers Wanda likes.
The third line contains $N$ numbers. It is guaranteed that the array is a permutation of the numbers from $0$ to $N-1$.
Output
If there is no way for Edna to sort the array using only the tricks that her friends taught her, output $-1$ as the only line of the output.
If there is a solution, the first line should contain an integer $m$ ($0 \le m \le 32\, 768$), the total number of tricks Edna should do.
This should be followed by the description of the tricks used, in order, one per line.
If Edna uses Wanda’s trick, output “W” (without the quotes).
If Edna uses Kermit’s trick, output “K $k$”, where $k$ is the number Edna wants to add to each element of the array.
Finally, if she uses Skimpy’s trick, output “S $s$”, where $s$ is the number to be xor’d with all elements of the array.
It can be proven that if there is a solution, there is a solution with no more than $32\, 768$ tricks.
Any sequence of not more than $32\, 768$ that sorts the array will be accepted.
Sample Input 1 | Sample Output 1 |
---|---|
2 0 1 1 0 |
1 W |
Sample Input 2 | Sample Output 2 |
---|---|
8 0 4 1 0 2 3 4 5 6 7 |
-1 |
Sample Input 3 | Sample Output 3 |
---|---|
4 1 3 3 2 1 0 |
2 K 1 W |
Sample Input 4 | Sample Output 4 |
---|---|
4 0 1 2 3 1 0 |
2 W S 2 |