And Then There Was One
InterviewTime limit1sMemory limit128 MB
Simulate a Josephus-style circle elimination game with a custom starting removal point and report the last remaining stone for each test case.
- Level
Easy3 of 10
- Topics
- Simulation, Queue, Math
- Solved
- No attempts yet
Problem
Let's play a stone-removal game.
Initially, as shown in Figure 1, stones numbered from to are arranged clockwise in a circle. Two numbers and are also given. Starting from this arrangement, remove the stones one at a time according to the rules below, until only one stone remains.
- Step 1: remove stone .
- Step (for ): starting from the position of the stone removed in step , remove the -th stone clockwise among the remaining stones. That is, skip stones and remove the next one; stones that have already been removed are not counted while skipping.
Carry out step 1, step 2, step 3, and so on in order until a single stone is left. That last stone is the answer.
For example, as in Figure 1, when , , the answer is .
Figure 1: Game Example
- Initial state: 8 stones are placed clockwise.
- Step 1: since , stone 3 is removed.
- Step 2: starting from 3 and with , skip stones 4, 5, 6, 7 (four stones) and remove stone 8.
- Step 3: starting from 8, skip stones 1, 2, 4, 5 and remove stone 6. Note that stone 3 is ignored (it is already removed, so it is not counted among the skipped stones).
- Steps 4-7: continue until only one stone remains.
- Finally, the remaining stone is 1, so the answer is 1.
Input
The input consists of several data lines; each data line contains three numbers:
n k m
The line after the last data line contains three zeros. Each number satisfies the following ranges.
, ,
The number of data lines is fewer than 100.
Output
For each data line, output the number of the last remaining stone.