#247MediumMath & Simulation· The Josephus Recurrence
Circle Elimination Winner
Input format
`n`: the number of players; `k`: the step size.
Output format
A single integer: the number (1 to n) of the winning player.
Examples
Input: n = 5, k = 2
Output: 3
Players leave in the order 2, 4, 1, 5, so player 3 wins.
Input: n = 6, k = 5
Output: 1
Players leave in the order 5, 4, 6, 2, 3 and player 1 is the last one.
Input: n = 1, k = 7
Output: 1
A single player wins immediately regardless of k.
Constraints
- 1 <= n <= 10^6
- 1 <= k <= 10^9
- #simulation
- #math
- #recurrence
- #josephus