P5151 HKE and His Little Friends
Background
HKE is playing a game with $n$ children.
Description
There are $n$ seats numbered $1$ to $n$, and the children are also numbered $1$ to $n$. Initially, every child sits in the seat with the same number. The game of HKE can be described by an $n$-permutation $A(A_1, A_2, \cdots, A_n)$. In one round, for every $1\leq i\leq n$, the child sitting at position $i$ moves to position $A_i$.
After $k$ rounds, HKE wants to know which child ends up at positions $1, 2, \cdots, n$.
Input Format
The first line contains two integers $n, k$.
The second line contains a permutation $A_1, A_2, \cdots, A_n$.
Output Format
Output one line with $n$ numbers denoting the child at positions $1, 2, \cdots, n$.
Explanation/Hint
- For $30\%$ of the testdata, $n\leq1000$, $k\leq1000$.
- For $100\%$ of the testdata, $n\leq100000$, $k\leq2^{31}-1$.
Translated by ChatGPT 5