Team Selection
Time limit1sMemory limit128 MB
Given n students in a circle and step k, report the last four positions removed by Josephus elimination.
- Level
Medium6 of 10
- Topics
- Simulation, Math, Implementation
- Solved
- No attempts yet
Problem
It is the night before the IOI team selection results are announced. Every student is awake, confident, and eager to hear the outcome. But when Bernard looks through the exam scores, he starts to worry: every student earned a perfect score, so there is no way to decide who makes the team from the exam alone.
Bernard closes his laptop and announces that this year the team will be chosen differently. He picks a number and has all the students stand in a circle. Starting the count at the first student, he walks around the circle and removes every -th student, continuing until the circle is empty. The last four students to be removed form the team.
If you want to make the team, you must plan ahead and stand in one of the four positions that will be selected. Given the number of students in the circle and the value , determine the last four positions that Bernard removes.
Input
The input contains several lines. Each line has two integers and separated by a single space, where and . Here is the number of students standing in the circle, numbered from to in circle order, and is the counting interval Bernard uses to remove students.
The last line is 0 0, which marks the end of the input and must not be processed.
Output
For each input line (except the terminating 0 0), print a single line with four integers separated by single spaces: the last four positions removed from the circle, listed in the order Bernard removes them.
Note
Consider a circle of students with . Bernard removes students in the order . The last four removed are , so those four positions form the team.