SAC '22 Code Challenge 4 Junior P4 - Obligatory Permutation Problem

View as PDF

Submit solution


Points: 7 (partial)
Time limit: 1.0s
Memory limit: 256M

Author:
Problem types

After falling asleep, Max dreamt about a problem:

You are given a permutation, P, of N numbers from 1 to N. Initially, set A = P. A move operation is when, for every index i in A simultaneously, the element at index i is moved to index P_i. Apply the move operation K times.

Since Max was dreaming, he could not solve this problem.

Can you solve Max's dream?

Constraints

1 \le P_i \le N

Subtask 1 [30%]

1 \le N, K \le 10^3

Subtask 2 [70%]

1 \le N \le 10^5

1 \le K \le 10^{18}

Input Specification

The first line will contain N and K, the number of elements in the permutation and the number of times to perform the operation, respectively.

The next line will contain N space-separated integers, P_i, the elements of the permutation.

Output Specification

Output the permutation, A, after applying the operation K times.

Sample Input 1

5 3
5 3 2 1 4

Sample Output 1

5 2 3 1 4

Explanation for Sample Output 1

After the first operation, the permutation becomes [1, 2, 3, 4, 5].

After the second operation, the permutation becomes [4, 3, 2, 5, 1].

After the third and final operation, the permutation becomes [5, 2, 3, 1, 4].

Sample Input 2

5 1000000000000000000
5 1 2 3 4

Sample Output 2

5 1 2 3 4

Comments

There are no comments at the moment.