Collatz pseudo-sequences (1)

Let us define sequences similar to those of Collatz with two parameters xx and yy. Given a number nn, the algorithm to get the next number is:

The standard Collatz sequence corresponds to x=0x = 0 and y=1y = 1.

Given xx, yy and a starting number nn, compute the length of the cycle reached by applying the above algorithm. For example, if x=1x = 1, y=5y = 5 and n=8n = 8, then the defined sequence is 8, 5, 20, 11, 38, 20, 11, 38, … so the cycle has length 3.

Since numbers can become very large, and we have no mathematical guarantee that we will reach a cycle, we will stop if at some point the sequence reaches a number greater than 10610^6.

Input

Input consists of several cases, each with three natural numbers xx, yy and nn. Assume that both xx and yy do not exceed 1000, that yy is odd (for the sequence to have some interest), and that the initial nn is not larger than 10610^6.

Output

For every case, print the length of the cycle, or the first number that strictly exceeds 10610^6.

Problem information

Author: Unknown
Translator: Salvador Roura

Generation: 2026-01-25T11:54:11.940Z

© Jutge.org, 2006–2026.
https://jutge.org