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
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Ả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 cards labeled , held in a deck with a top and bottom. The goal is to reveal cards in the order by following a fixed procedure.
At step (starting at ), with cards remaining:
- Repeat exactly times: take the top card and move it to the bottom. Moving the only remaining card leaves the deck unchanged.
- Reveal and remove the top card. This must be card number .
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 , the number of cards.
Output
Print exactly space-separated integers: the initial card labels from top to bottom.
Subtasks
Subtask 1 (20 points):
- .
- The deck contains exactly one card of every label .
- At step , move the top card to the bottom exactly times, then reveal the top.
Subtask 2 (30 points):
- .
- The deck contains exactly one card of every label .
- At step , move the top card to the bottom exactly times, then reveal the top.
Subtask 3 (50 points):
- .
- The deck contains exactly one card of every label .
- At step , move the top card to the bottom exactly times, then reveal the top.
Examples
Input
5
Output
3 1 4 5 2
Explanation
Arrange the initial deck, top to bottom, as .
- Step : move to the bottom, obtaining , and reveal . Remaining: .
- Step : move then to the bottom, obtaining , and reveal . Remaining: .
- Step : three rotations restore , so reveal . Remaining: .
- Step : four rotations of restore its order, so reveal . Remaining: .
- Step : moving the sole card five times changes nothing; reveal .
The cards are revealed in exactly the required order .