Shil and Crazy Operation

3.7

65 votes
Implementation, Open, Graph Theory, Algorithms, Approved, Medium
Problem

Shil likes to perform Crazy Operation on an array A1,A2,...AN.

Given an array A1,A2,...AN and permutation P1,P2, ..,PN, a Crazy Operation consists of two parts:

  1. Perform Operation Bi = Ai for every i in 1..N

  2. Perform Operation Ai = BPi for every i in 1..N. Here P is a permutation from 1 to N.

Shil asks you to print the resulting array A1, A2,...AN after doing Crazy Operation on it , T times.

Input format:
The first line of input consists of two integers N and T. The next line consists of N integers A1, A2,...AN. The next line consists of permutation P1,P2,...PN.

Output format:
Print the resulting A1,A2,...AN after doing Crazy operations on it T times.

Constraints:
1≤N≤105
1≤T≤1018
1≤Ai≤109

Sample Input
5 3
1 2 3 4 5
3 4 5 2 1
Sample Output
1 4 3 2 5
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

On doing Crazy Operation 1st time resulting array will be (3,4,5,2,1).
On doing second time , resulting array will be (5,2,1,4,3).
On doing third time, resulting array will be (1,4,3,2,5).

Editor Image

?