Card Rearrangement

Interview

Time limit1sMemory limit128 MB

Summary
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 2n2n cards numbered from 11 to 2n2n, stacked so that from top to bottom they are in the order 1,2,3,…,2n1, 2, 3, \dots, 2n.

This stack is rearranged by applying the following two operations some number of times.

Cut by an integer kk

Take the top kk cards as pile AA and leave the remaining cards as pile BB, then place pile BB on top of pile AA. In other words, after the cut the cards of pile BB are on top, with the cards of pile AA below them.

Riffle shuffle

Split the top nn cards into pile AA and the remaining nn cards into pile BB, then merge them into a single stack so that from the top the order is the 11st card of AA, the 11st card of BB, the 22nd card of AA, the 22nd card of BB, …\dots, the nnth card of AA, the nnth card of BB.

Following the given instructions, rearrange all the cards and then print the card numbers from top to bottom.

Input

  • The first line contains nn (1≤n≤1001 \le n \le 100); that is, there are 2n2n cards.
  • The second line contains the number of operations mm (1≤m≤10001 \le m \le 1000).
  • Each of the next mm lines (lines 33 through m+2m+2) contains a single integer kk with 0≤k≤2n−10 \le k \le 2n-1, specifying the rearrangement operations in order.
    • If k=0k = 0, perform a riffle shuffle.
    • If 1≤k≤2n−11 \le k \le 2n-1, perform a cut by kk.

Output

Print 2n2n 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 ii-th line contains the number of the ii-th card from the top.

Examples2

  1. Example 1

    Input
    2
    2
    1
    0
    
    Expected output
    2
    4
    3
    1
    
  2. Example 2

    Input
    3
    4
    2
    4
    0
    0
    
    Expected output
    1
    5
    4
    3
    2
    6