#P7981. Sequence Transformation by Squaring Operation
Sequence Transformation by Squaring Operation
Sequence Transformation by Squaring Operation
Given a sequence \(a\) of length \(n\) (1-indexed), we define a single operation on \(a\) as follows:
For each \(i\) (\(1 \leq i \leq n\)), let \(b_i = a_{a_i}\) and then update \(a_i \gets b_i\). In other words, the operation replaces each \(a_i\) with \(a_{a_i}\).
Notice that if we interpret the sequence as a function \(f\) where \(f(i)= a_i\), then one operation transforms \(f\) into \(f \circ f\) (i.e. \(f^2\)). After \(k\) operations, the resulting sequence is given by \(f^{2^k}(i) \) for each \(i\).
Your task is, given \(n\), \(k\) and the sequence \(a\), compute the sequence after performing the operation exactly \(k\) times.
inputFormat
The first line contains two integers \(n\) and \(k\) separated by a space.
The second line contains \(n\) space-separated integers representing the sequence \(a = [a_1, a_2, \dots, a_n]\). It is guaranteed that \(1 \leq a_i \leq n\) for all \(i\).
outputFormat
Output a single line containing \(n\) space-separated integers, which is the sequence obtained after performing the operation \(k\) times.
sample
3 1
2 3 1
3 1 2