Alice and Bob like to play a card game, with a deck of cards numbered with consecutive nonnegative integers.
Alice has a very particular way of shuffling the deck, though. First, she takes the top card from the deck and puts it at the bottom of the deck. Then she removes the next card, and starts a pile with it. Then, again she cycles the top card to the bottom, and puts the new top card onto the pile. She repeats this process until she's emptied the deck, at which point the pile is the new deck.
deck | pile
-----------+-----------
3 1 4 0 2 |
1 4 0 2 3 |
4 0 2 3 | 1
0 2 3 4 | 1
2 3 4 | 0 1
3 4 2 | 0 1
4 2 | 3 0 1
2 4 | 3 0 1
4 | 2 3 0 1
| 4 2 3 0 1
4 2 3 0 1 |
Figure 1: Alice performs her shuffle on the 5-card deck "3, 1, 4, 0, 2". The backs of the cards are all facing left.
One day, Bob announces he's taking a week's vacation. Alice, having nobody to play the game with, enlists her friend Eve. Now, Eve is a shameless cheater, so when she sees Alice's peculiar shuffle, she realizes that she can stack the deck beforehand to her advantage!
When Eve gets home after the first day, she does some analysis on the game and figures out that her best odds are when the cards are in the order 0, 1, 2, 3, 4, 5, ... She didn't catch how many cards were in the deck, though, so she hatches a harebrained scheme to write some code on her arm that, when run, takes the size of the deck and displays the order Eve needs to put the cards in, so that when Alice shuffles the deck, the final deck is in the order 0, 1, 2, 3, ...
It doesn't really matter to Eve what language the code is in (she knows them all), or whether the code is a function taking an integer argument and returning an array, or a full program taking input via a command line argument or STDIN and writing the results to STDOUT. She does, however, need the code as short as possible, to minimize the chance of Alice seeing it and catching her.
Immoral as it might be, can you guys help Eve out?
Example inputs and outputs:
in out
1 0
2 0 1
5 2 4 0 3 1
10 2 9 4 8 0 7 3 6 1 5
52 6 51 25 50 12 49 24 48 1 47 23 46 11 45 22 44 5 43 21 42 10 41 20 40 2 39 19
38 9 37 18 36 4 35 17 34 8 33 16 32 0 31 15 30 7 29 14 28 3 27 13 26
shuffle(shuffle(range(5))) == range(5)
... \$\endgroup\$