#K48927. The Josephus Problem
The Josephus Problem
The Josephus Problem
You are given a circle of n contestants numbered from 1 to n. Starting from the first contestant, every k-th contestant is eliminated until only one remains. The problem can be described by the recurrence relation:
$$J(1,k)=1$$
$$J(n,k)= ((J(n-1,k)+k-1) \mod n) + 1, \quad \text{for } n>1$$
Your task is to determine the winning contestant's original position for each dataset provided. Input will consist of multiple datasets. Each dataset contains two integers n and k. The input terminates with a line containing 0 0
, which should not be processed.
inputFormat
The input consists of multiple lines. Each line contains two space-separated integers n (the number of contestants) and k (the step count for elimination). The input terminates with a line containing "0 0", which indicates the end of input and should not be processed.
outputFormat
For each dataset (except the terminating one), print the number of the winning contestant on a separate line.## sample
7 3
5 2
0 0
4
3
</p>