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

#FT0000033. Trò chơi Josephus II (Josephus Problem II)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Trò chơi Josephus II (Josephus Problem II)

Josephus Problem II

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn children standing in a circle, numbered 1,2,…,n1,2,\ldots,n in circular order. Counting starts at child 11.

In every round, skip kk 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 k=0k=0, immediately remove the child at the current starting position. If kk exceeds the number of remaining children, continue around the circle for the required number of steps.

Input

  • Only line: integers n,kn,k: the number of children and the number of children skipped before each removal.

Output

Print nn space-separated integers: the children in the exact removal order.

Subtasks

Subtask 1 (20 points):

  • 1≤n≤201\le n\le 20.
  • 0≤k≤1090\le k\le 10^9.
  • Initial child labels are exactly 1,2,…,n1,2,\ldots,n in circular order.
  • Exactly kk remaining children are skipped before removing the next.

Subtask 2 (30 points):

  • 1≤n≤30001\le n\le 3000.
  • 0≤k≤1090\le k\le 10^9.
  • Initial child labels are exactly 1,2,…,n1,2,\ldots,n in circular order.
  • Exactly kk remaining children are skipped before removing the next.

Subtask 3 (50 points):

  • 1≤n≤2000001\le n\le 200000.
  • 0≤k≤1090\le k\le 10^9.
  • Initial child labels are exactly 1,2,…,n1,2,\ldots,n in circular order.
  • Exactly kk 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 [1,2,3,4,5,6,7][1,2,3,4,5,6,7], counting from 11 and skipping two people per round.

  1. Skip 1,21,2 and remove 33; restart at 44.
  2. Skip 4,54,5 and remove 66; restart at 77.
  3. Skip 7,17,1 and remove 22; restart at 44.
  4. Skip 4,54,5 and remove 77; restart at 11.
  5. Skip 1,41,4 and remove 55; restart at 11.
  6. With 1,41,4 left, skip 1,41,4 and remove 11.
  7. Remove the final child, 44.

Thus the output is 3 6 2 7 5 1 4.