Chinese Solution, since USACO does not have an official solution for the problem above.
I had a lot of trouble with the implementation of this question, so I looked up this solution online. Google Translate could only do so much for the page (the only solution I could find), but I think I was able to discern the following from the code:
1) The array "per" reverses the permutation process, and length of 31 represents a bitmask of that length,
2) The idea behind the "per" is that the result of a number of consecutive permutations can be assembled in logarithmic time, and
3) The variable "num" in the third main loop functions as a mask.
However, I do not fully understand the purpose of "bot", "now", and "k" in the third main loop, and the mathematics in the first and third main loops.
I would appreciate an explanation for these parts of the solution.
Thanks in advance!