The King, while searching for a new royal mathematician to advise him in all matters…
- User
The King, while searching for a new royal mathematician to advise him in all matters relating to numbers, has heard about your impressive skills. To test your abilities, he has asked the Queen, renowned for her love of card games and the techniques passed down through her family, to pose a challenge.
The Queen explains that a shuffle is a sequence of operations that rearranges the cards in a deck. A shuffle consists of one or more of the following basic operations: Reverse: The order of the deck is reversed. Swap: The cards at two specified positions are swapped. Shift: A certain number of cards are removed from the bottom of the deck and placed on top in the same order. Interleave: The deck is separated into two piles corresponding to its top half and bottom half. Then those piles are weaved together so that the cards from the bottom pile take the odd positions in the deck (1st, 3rd, 5th...), and the cards from the top pile take the even positions in the deck (2nd, 4th, 6th...). Note that if there is an odd number of cards in the deck, then the bottom pile gets one more card.
Curiously, applying the same shuffle over and over to a deck will eventually return the cards to the same positions where they began. The minimum number of times that a shuffle must be repeated for this to occur is called the shuffle's period. The Queen wishes for you to design a shuffle with a period P, which she will choose. It's a simple task for a would-be royal mathematician. Input
The input will begin with a single line containing an integer T, where 1≤T≤100. Then T test cases will follow.
Each test case will consist of an integer P (1≤P≤106) on its own line, indicating the shuffle period that the Queen is requesting. Output The output will span multiple lines for each test case. For each case:
The first line must contain an integer N denoting the number of cards in the deck. It must be positive and no greater than 104. The second line must contain an integer M indicating the number of basic operations that will follow. It must be positive and no greater than 30. Each of the next M lines must represent an operation with an uppercase letter, possibly followed by one or two integers. The entire list will describe the steps that compose a shuffle, to be performed sequentially. The operation format is as follows: R: Reverse the deck. I: Interleave the deck. S u: Perform a shift with the bottom u cards (1≤u<N). W u v: Swap the cards at positions u and v (1≤u,v≤N). The top card has index 1 and the bottom card has index N. The Queen will only request shuffle periods that could be satisfied within the constraints for N and M.
Sample test(s) Input 3 6 4 12 Output 5 2 S 1 W 1 3 4 1 S 1 7 2 S 1 W 1 5
- Assistant