In this quiz, you are asked to implement a game about passing a bomb. The rules are as follows:
The bomb is passed from its current holder to the next j-th person, and the bomb will explode after the passing completes.
For example, player a1 (the current bomb holder) passes the bomb to player ak, and the bomb will explode at player ak.
Next, players a1 to ak-1 need to change their positions as following:
Figure: bomb passing sequence
For example:
At the beginning, we have players numbered 1 to 7, with player 1 initially holding the bomb.
Run 1: j = 9
The passing sequence is as follows: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 1 -> 2 -> 3. Player 3 holds the bomb at the end, so player 3 is removed from the sequence, and the bomb is passed to the next player, which is player 4.
After passing the bomb, we need to reorder their positions. Since every player has now passed the bomb, we reorder the entire sequence from 1 -> 2 -> 4 -> 5 -> 6 -> 7 to 1 -> 7 -> 2 -> 6 -> 4 -> 5, and player 4 holds the bomb.
Run 2: j = 4
The passing sequence is 4 -> 5 -> 1 -> 7 -> 2. Player 2 holds the bomb at the end, so player 2 is removed from the sequence, and the bomb is passed to the next player, which is player 6.
In this run, players 4, 5, 1, 7 have passed the bomb, so we only reorder their positions from 4 -> 5 -> 1 -> 7 to 4 -> 7 -> 5 -> 1, and we need to insert this reordered sequence into the original sequence. The final sequence will be 1 -> 6 -> 4 -> 7 -> 5, and player 6 holds the bomb.
Run 3: j = 2
Following the operations above, the final reordered sequence will be 1 -> 6 -> 4 -> 5.
Run 4: j = -1
When j is -1, there is only one player remaining. In this case, you should output the sequence starting from the player who holds the bomb at the end. Thus, the sequence is 5 -> 1 -> 6 -> 4, as player 5 holds the bomb in the end.
Note that all STL library are forbidden.
The first line has one number, N.
The second line has N numbers, denoting the indices of the players.
For the remaining lines, players pass the bomb based on the rules mentioned above.
2 ≤ N ≤ 10,000
1 ≤ Player index ≤ 1,000,000
1 ≤ number of remaining lines ≤ 1,000,000
1 ≤ j ≤ 264-1
Please print the indices of the remaining players in the sequence, starting at the person who has the bomb.
Print ' ' (space) between two numbers and '\n' after the last number.