#P1826. Monkey Circle Game

    ID: 15109 Type: Default 1000ms 256MiB

Monkey Circle Game

Monkey Circle Game

There are n monkeys arranged in a circle and numbered clockwise from 1 to n. Starting from monkey 1, a game is played in which every time you count exactly m monkeys in clockwise order and eliminate the m-th monkey. The counting then resumes from the next monkey immediately after the one eliminated, and this process repeats until only one monkey remains. The winner (last remaining monkey) for a given n can be computed using the recurrence:

$$\begin{cases} f(1,m)=1, \\ f(n,m)=((f(n-1,m)+m-1) \mod n)+1 \; \text{for } n>1. \end{cases}$$

Now, for all games with n ranging from a to b (i.e. for n = a, a+1, ..., b), you are to determine which monkey number wins the game most frequently. In case of a tie, output all such monkey numbers in increasing order.

inputFormat

Input consists of three integers a, b and m separated by spaces. Here, a and b define the range of n (i.e. n = a, a+1, ..., b) and m is the step count used in the elimination process.

outputFormat

Output the monkey number(s) that win the most often over all games; if multiple monkey numbers achieve the maximum frequency, print them in increasing order separated by a single space.

sample

3 7 3
1 4