Under construction

Still being built. Things may change

Josephus Permutation

Interview

Time limit2sMemory limit512 MB

Summary
Remove every Kth person from a circle of N people and print the removal order in angle brackets.
Level

Easy3 of 10

Topics
Queue, Simulation
Solved
No attempts yet

Problem

NN people numbered 1 to NN sit in a circle, and a positive integer KK (K≤NK \le N) is given. Starting the count at person 1, remove the KKth person from the circle. Once a person is removed, count again from 1 at the next person, remove the KKth person, and repeat the same process along the circle of the remaining people. The process continues until all NN people are removed. The order in which the people are removed is the (N,K)(N, K)-Josephus permutation. For example, the (7,3)(7, 3)-Josephus permutation is <3, 6, 2, 7, 5, 1, 4>.

Given NN and KK, write a program that finds the (N,K)(N, K)-Josephus permutation.

Input

The first line contains NN and KK in this order, separated by a space. (1≤K≤N≤10001 \le K \le N \le 1000)

Output

Print the Josephus permutation on one line. Start with the opening angle bracket <, write the numbers in the order they were removed, separate consecutive numbers with a comma and one space, and finish with the closing angle bracket >.

Examples7

  1. Example 1

    Input
    7 3
    
    Expected output
    <3, 6, 2, 7, 5, 1, 4>
    
  2. Example 2

    Input
    1 1
    
    Expected output
    <1>
    
  3. Example 3

    Input
    5 1
    
    Expected output
    <1, 2, 3, 4, 5>
    
  4. Example 4

    Input
    5 5
    
    Expected output
    <5, 1, 3, 4, 2>
    
  5. Example 5

    Input
    2 2
    
    Expected output
    <2, 1>
    
  6. Example 6

    Input
    6 4
    
    Expected output
    <4, 2, 1, 3, 6, 5>
    
  7. Example 7

    Input
    10 2
    
    Expected output
    <2, 4, 6, 8, 10, 3, 7, 1, 9, 5>