Shuffling Cards

Automated Casino Machines (ACM), a company that manufactures roulettes, slot machines, 6-faced die and the like, has developed the next-generation machine for shuffling cards. This machine offers the feature of shuffling N cards at a time. The machine works as follows. Initially, each of the N cards is loaded into a slot. There are N slots in total and they are numbered between 1 and N, inclusive. When the machine is activated, the cards are mechanically moved around and as a result each card ends up in a (possibly) different slot. The act of activating the machine once is called a shuffling round. Internally, the machine has a fixed shuffling function f that describes the machine’s behavior: if a card is currently in slot i, then after one shuffling round it will be placed in slot f(i), for 1 ≤ i ≤ N. For example, consider a machine with the shuffling function defined by the following mapping (with N = 5): 1 7→ 4 2 7→ 2 3 7→ 1 4 7→ 5 5 7→ 3 This means that after one shuffling round this machine simultaneously moves the cards in slots 1, 3, 4, 5 to slots 4, 1, 5, 3, respectively; card in slot 2 is not moved. Of course, this is not very random because after exactly one round the machine always produces the same result. However, different results can be obtained by activating the machine over and over again without unloading the cards after each intermediate round. For example, after two shuffling rounds, the aforementioned shuffling machine would simultaneously move the cards in slots 1, 3, 4, 5 to slots 5, 4, 3, 1, respectively; card in slot 2 would still not be moved. ACM is lucky to have you as an employee because it is getting quite confusing to verify if the new shuffling machine is working correctly. Your task is to write a program that, given the description of a shuffling function, predicts which slot each card will occupy after R shuffling rounds. Input The input contains several test cases; each one comprising two lines. The first line of each test case contains two blank-separated integers N and R, where N is the number of slots that the shuffling machine has (1 ≤ N ≤ 1040) and R is the number of shuffling rounds to be performed (0 ≤ R < 263). The second line of each test case contains a permutation of the numbers 1, 2, . . . , N , separated by exactly one blank, describing the shuffling function f (the i-th number corresponds to f(i), for 1 ≤ i ≤ N). Output For each test case, output a line with a blank-separated permutation of the numbers 1, 2, . . . , N , where the i-th number is the slot that the card that starts in slot i will occupy after exactly R shuffling rounds, for 1 ≤ i ≤ N. Do not print a blank after the last number in each line. Sample Input 50 42153 51

2/2 42153 52 42153 Sample Output 12345 42153 52431