Aku Negaraku
InterviewTime limit3sMemory limit512 MB
For each N and M, simulate Josephus elimination around a circle and report the last remaining trainee's number.
- Level
Medium4 of 10
- Topics
- Simulation, Array, Recursion
- Solved
- No attempts yet
Problem
An international leadership training academy in Kuala Lumpur sends thousands of trainees to companies around the world every year. For a fair selection, trainees numbered from to stand in a circle.
A number is picked at random. Counting starts at trainee , and every -th person among those not yet selected is picked, wrapping around from back to and skipping trainees who have already been picked. The last trainee remaining after all others have been picked stays in Malaysia. Find the number that stays in Malaysia.
Input
The input consists of multiple lines. Each line contains , the number of trainees, and , the step size, with and . The last line contains 0 0 and must not be processed.
Output
For each line of input, print the number that stays in Malaysia according to the scheme above.