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 Pi. Apply the move operation K times.

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

Can you solve Max's dream?

Constraints

1PiN

Subtask 1 [30%]

1N,K103

Subtask 2 [70%]

1N105

1K1018

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, Pi, the elements of the permutation.

Output Specification

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

Sample Input 1

Copy
5 3
5 3 2 1 4

Sample Output 1

Copy
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

Copy
5 1000000000000000000
5 1 2 3 4

Sample Output 2

Copy
5 1 2 3 4

Comments

There are no comments at the moment.