For any function that maps a finite set to itself, and for any initial value in the set, the sequence of values eventually repeats some values, i.e., there is some and some such that . Once this happens, the sequence continues by repeating the cycle from to .
For instance, the function that maps to generates the following sequence when :
In this sequence, the beginning of the cycle is found after 2 steps. In this case, , , and the periodicity is .
Given a function that maps the interval to itself, and several starting values , compute the corresponding values of and .
Input starts with the number of cases. Every such case begins with two integer numbers and . Follow, in order, the images of the numbers in . Follow numbers: the ’s for which the result must be computed.
For every case, print its number and lines each one with and .
Since some of the private cases are huge, a recursive program may exhaust the recursion stack.
Author: Xavier Martínez
Generation: 2026-01-25T10:09:21.548Z
© Jutge.org, 2006–2026.
https://jutge.org