Cards
Time limit1sMemory limit16 MB
Find the initial deck order that makes the shuffle, driven by the primes, output cards N down to 1.
- Level
Medium7 of 10
- Topics
- Simulation, Math, Implementation, Greedy
- Solved
- No attempts yet
Problem
Lukas loves card games and has invented a new way to shuffle a deck.
There are cards, numbered to , stacked in the first pile in some order. A second pile starts out empty. You perform exactly operations. Let be the -th prime number (, , , ). In the -th operation you:
- move cards, one at a time, from the top of the first pile to its bottom, then
- take the card now on top of the first pile and place it on top of the second pile.
For example, suppose the first pile holds from top to bottom. Performing an operation with first moves the top cards to the bottom, giving ; then the top card is removed and placed on the second pile, leaving .
Arrange the cards in the first pile so that after all operations the second pile reads, from top to bottom, (that is, card ends up at the very bottom of the second pile).
Input
A single integer () — the number of cards in the first pile.
Output
Output lines. The -th line contains , the number of the card at position in the first pile, where position is the top card and position is the bottom card.
Note
The first four primes are . Starting from the arrangement (top to bottom), the first pile evolves as follows through the four operations:
Operation (): move card to the bottom, then take card . Operation (): after two moves the top card is , which is taken. Operations and take cards and . The second pile, filled from bottom to top, becomes — that is read from the top.