Under construction

Still being built. Things may change

Josephus Permutation

Interview

Time limit2sMemory limit256 MB

Summary
Simulate the Josephus elimination order for N people counting off every K-th person in a circle and print the removal sequence.
Level

Easy3 of 10

Topics
Queue, Simulation, Implementation
Solved
No attempts yet

Problem

N people numbered from 1 to N sit in a circle. Given a positive integer K, repeatedly remove the K-th remaining person while counting around the circle. After a person is removed, counting resumes from the next remaining person. The order in which all people are removed is called the (N, K)-Josephus permutation.

Given N and K, print the (N, K)-Josephus permutation.

Input

The first line contains N and K separated by a space.

  • 1 <= K <= N <= 5,000

Output

Print the removed numbers in order, enclosed in angle brackets < and >. Separate adjacent numbers with a comma followed by one space: , .

Examples1

  1. Example 1

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