Card Rearrangement
InterviewTime limit1sMemory limit128 MB
Starting from the stack 1, 2, ..., 2n, apply a sequence of cuts and riffle shuffles and print the final order of the cards.
- Level
Easy3 of 10
- Topics
- Simulation, Implementation, Array
- Solved
- No attempts yet
Problem
There are cards numbered from to , stacked so that from top to bottom they are in the order .
This stack is rearranged by applying the following two operations some number of times.
Cut by an integer
Take the top cards as pile and leave the remaining cards as pile , then place pile on top of pile . In other words, after the cut the cards of pile are on top, with the cards of pile below them.

Riffle shuffle
Split the top cards into pile and the remaining cards into pile , then merge them into a single stack so that from the top the order is the st card of , the st card of , the nd card of , the nd card of , , the th card of , the th card of .

Following the given instructions, rearrange all the cards and then print the card numbers from top to bottom.
Input
- The first line contains (); that is, there are cards.
- The second line contains the number of operations ().
- Each of the next lines (lines through ) contains a single integer with , specifying the rearrangement operations in order.
- If , perform a riffle shuffle.
- If , perform a cut by .
Output
Print lines. The first line contains the number of the topmost card after all rearrangements are finished, the second line contains the number of the second card from the top, and in general the -th line contains the number of the -th card from the top.