No.1013 Advance ○ Squares
Problem Statement
You are given a permutation $P$ of {$1,2, \dots ,N$}.
Let $T$ be the sequence obtained by concatenating $10^{10}$ copies of $P$.
You perform the following action on $T$.
・When you are at the $i$-th element, move to the $i+T_i$-th element
For each starting position at the $i$-th element $(i=1,2, \dots ,N)$, after performing this action $K$ times,
determine which element you reach, for every $i$, and print each answer on its own line.
Input
$N\ K$ $P_1\ P_2\ \dots\ P_N$
The input is given as integers.
$ 1 \le N \le 10^5 $
$ 1 \le K \le 10^9 $
$P$ is a permutation of {$1, 2, \dots , N$}.
Output
$A_1$ $A_2$ $\vdots$ $A_N$
Print the answer $A_i$ to the problem for each $i (i=1,2, \dots , N)$ in the format above.
Print a newline at the end.
Sample
Sample 1
Input
5 2 1 2 3 4 5
Output
4 8 7 11 15
For example, if you start from the 3rd element, you advance as 3→6→7.
Sample 2
Input
4 2 1 2 3 4
Output
4 8 8 12
Sample 3
Input
10 6 1 9 5 10 2 4 3 8 7 6
Output
31 32 36 64 30 36 36 40 40 40
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
otamay6
37zigen