PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000034. Ảo thuật sắp xếp bộ bài (Card Trick)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Ảo thuật sắp xếp bộ bài (Card Trick)

Card Trick

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

A magician has nn cards labeled 1,2,…,n1,2,\ldots,n, held in a deck with a top and bottom. The goal is to reveal cards in the order 1,2,…,n1,2,\ldots,n by following a fixed procedure.

At step ii (starting at i=1i=1), with n−i+1n-i+1 cards remaining:

  1. Repeat exactly ii times: take the top card and move it to the bottom. Moving the only remaining card leaves the deck unchanged.
  2. Reveal and remove the top card. This must be card number ii.

Find the initial top-to-bottom arrangement of all cards that produces the required reveal order. Each card number appears exactly once.

Input

  • Only line: integer nn, the number of cards.

Output

Print exactly nn space-separated integers: the initial card labels from top to bottom.

Subtasks

Subtask 1 (20 points):

  • 1≤n≤201\le n\le 20.
  • The deck contains exactly one card of every label 1,2,…,n1,2,\ldots,n.
  • At step ii, move the top card to the bottom exactly ii times, then reveal the top.

Subtask 2 (30 points):

  • 1≤n≤15001\le n\le 1500.
  • The deck contains exactly one card of every label 1,2,…,n1,2,\ldots,n.
  • At step ii, move the top card to the bottom exactly ii times, then reveal the top.

Subtask 3 (50 points):

  • 1≤n≤200001\le n\le 20000.
  • The deck contains exactly one card of every label 1,2,…,n1,2,\ldots,n.
  • At step ii, move the top card to the bottom exactly ii times, then reveal the top.

Examples

Input

5

Output

3 1 4 5 2

Explanation

Arrange the initial deck, top to bottom, as [3,1,4,5,2][3,1,4,5,2].

  1. Step 11: move 33 to the bottom, obtaining [1,4,5,2,3][1,4,5,2,3], and reveal 11. Remaining: [4,5,2,3][4,5,2,3].
  2. Step 22: move 44 then 55 to the bottom, obtaining [2,3,4,5][2,3,4,5], and reveal 22. Remaining: [3,4,5][3,4,5].
  3. Step 33: three rotations restore [3,4,5][3,4,5], so reveal 33. Remaining: [4,5][4,5].
  4. Step 44: four rotations of [4,5][4,5] restore its order, so reveal 44. Remaining: [5][5].
  5. Step 55: moving the sole card five times changes nothing; reveal 55.

The cards are revealed in exactly the required order 1,2,3,4,51,2,3,4,5.