#FT0000033. Trò chơi Josephus II (Josephus Problem II)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Trò chơi Josephus II (Josephus Problem II)
Josephus Problem II
Source: CSES
Version: Phuoc Hung OJ Extended
Problem Statement
There are children standing in a circle, numbered in circular order. Counting starts at child .
In every round, skip children still in the circle, then remove the next child. Already removed children are not counted. After a removal, counting resumes at the next remaining child in the circular direction.
Determine the complete removal order. If , immediately remove the child at the current starting position. If exceeds the number of remaining children, continue around the circle for the required number of steps.
Input
- Only line: integers : the number of children and the number of children skipped before each removal.
Output
Print space-separated integers: the children in the exact removal order.
Subtasks
Subtask 1 (20 points):
- .
- .
- Initial child labels are exactly in circular order.
- Exactly remaining children are skipped before removing the next.
Subtask 2 (30 points):
- .
- .
- Initial child labels are exactly in circular order.
- Exactly remaining children are skipped before removing the next.
Subtask 3 (50 points):
- .
- .
- Initial child labels are exactly in circular order.
- Exactly remaining children are skipped before removing the next.
Examples
Input
7 2
Output
3 6 2 7 5 1 4
Explanation
Initially the circle is , counting from and skipping two people per round.
- Skip and remove ; restart at .
- Skip and remove ; restart at .
- Skip and remove ; restart at .
- Skip and remove ; restart at .
- Skip and remove ; restart at .
- With left, skip and remove .
- Remove the final child, .
Thus the output is 3 6 2 7 5 1 4.