Jacobo loves to travel. He has discovered a city whose streets form a single circle with houses. Each house is labeled with an integer.
Jacobo starts at the first house. From a house with label , he moves exactly houses along the circular street:
If , he moves houses to the right.
If , he moves houses to the left.
If , he stays at the current house.
After moving, he looks at the number written on the house he reaches, and repeats the same process. Because the street is circular, moving past the last house continues from the first house, and moving before the first house continues from the last house.
Your task is to determine the length of the cycle eventually followed by Jacobo. A cycle consists of the houses visited repeatedly from some point onward. In particular, if Jacobo reaches a house labeled 0, he remains there forever, so the cycle has length 1.
Input consists of several cases, each with , followed by integer numbers with absolute value at most . Assume .
For each case, print a single integer: the length of the cycle eventually followed by Jacobo.
Author: Joan Alemany
Event: Vint-i-quatrè Concurs de Programació de la UPC -
Final
Date: 2026-10-07
Generation: 2026-10-03T14:13:10.383Z
© Jutge.org, 2006–2026.
https://jutge.org