#P15064. [UOI 2024 II Stage] Everyone Loves Permutations

[UOI 2024 II Stage] Everyone Loves Permutations

题目描述

A permutation of length nn is an array of length nn containing all integers from 11 to nn, and all its elements are pairwise distinct.

Having grown up and played with arrays, Anton moved on to studying more interesting arrays --- permutations. While writing his thesis, he faced a very difficult task.

He has a permutation pp of length nn and an integer kk. He decided to construct a two-dimensional array aa with sizes (k+1)×n(k+1) \times n.

  • a0j=ja_{0j}=j for all jj (1jn1 \leq j \leq n);
  • aij=a(i1)pja_{ij}=a_{(i-1)p_j} for all ii (1ik1 \leq i \leq k) and jj (1jn1 \leq j \leq n).

Let p=[5,3,1,4,2]p=[5, 3, 1, 4, 2] and k=3k=3, then we have the following array.

$$\begin{array}{|c|c|c|c|c|c|} \hline a_{ij} & j=1 & j=2 & j=3 & j=4 & j=5 \\ \hline\hline i=0 & 1 & 2 & 3 & 4 & 5 \\ \hline i=1 & 5 & 3 & 1 & 4 & 2 \\ \hline i=2 & 2 & 1 & 5 & 4 & 3 \\ \hline i=3 & 3 & 5 & 2 & 4 & 1 \\ \hline \end{array}$$

For each xx (1xn1 \leq x \leq n), he wants to know the sum of all jj such that aij=xa_{ij}=x, where 1ik1 \leq i \leq k. In other words, he wants to find the sum of kk numbers --- the indices of the number xx in each aia_i.

Consider the last example. If x=1x=1, the answer will be 3+2+5=103+2+5=10.

After some deliberation and simple ideas, Anton managed to solve this problem quickly. Now he wants to check if you can solve it too.

输入格式

The first line of the input contains two integers nn, kk (1n1061 \le n \le 10^6, 1k1091 \le k \le 10^9) --- the length of the permutation and the number of repetitions of operations, respectively.

The second line contains the permutation pp (1pin1 \le p_i \le n).

输出格式

Print nn integers, where the ii-th number is the answer for x=ix=i.

3 2
2 1 3
3 3 6
5 3
5 3 1 4 2
10 9 8 12 6

提示

  • (88 points): k=1k = 1;
  • (1717 points): pi=ip_i = i;
  • (2626 points): n2000,k2000n \le 2000, k \le 2000;
  • (2828 points): n2000n \le 2000, for any ii and jj, there exists a kk such that p[p[pp[i]]]=jp[p[p \dots p[i] \dots ]]=j, where the nesting is taken kk times;
  • (99 points): for any ii and jj, there exists a kk such that p[p[pp[i]]]=jp[p[p \dots p[i] \dots ]]=j, where the nesting is taken kk times;
  • (1212 points): without additional restrictions.