We need to understand the transformation f(p, k). It says: For k>1 integer, transformation f on permutation p (length n). Let r be maximum integer such that rk ≤ n. So r = floor(n/k). Then we have: "the described transformation of permutation p cyclically shifts to the left each consecutive block of length k and the last block with the length equal to the remainder after dividing n by k." But the image is not shown. However, the description likely: We break p into blocks: first block of size k (indices 1..k), second block of size k (k+1..2k), ..., up to r blocks of size k, and then a final block of size n - rk (the remainder). Then each block is cyclically shifted to the left by one position? Or maybe something else? The text: "cyclically shifts to the left each consecutive block of length k and the last block with the length equal to the remainder after dividing n by k." But we need to know how many positions? Typically such transformations might be "rotate each block left by 1" i.e., move first element to the end of the block? However the sample for n=4 clarifies.
Given n=4, they compute:
- f([1,2,3,4], 2) = [2,1,4,3]
So we break into blocks of size 2: [1,2] and [3,4]. Then shift left each block cyclically? If we shift left by 1: [1,2] -> [2,1]; [3,4] -> [4,3]. So indeed f(p,2) does a left cyclic shift by 1 on each block of size k (and also for remainder block? But here remainder=0). So f(p,2)=[2,1,4,3].
Then f([2,1,4,3], 3) = [1,4,2,3].
Now n=4, k=3. r = floor(4/3)=1, remainder = 1. So blocks: one block of size 3: [2,1,4] and last block of size 1: [3]. We apply left cyclic shift to each block? For block of size 3, shift left by 1: [2,1,4] -> [1,4,2] (move first element to end). For block of size 1, shifting left does nothing, stays [3]. So resulting permutation: [1,4,2,3]. That matches.
Then f([1,4,2,3], 4) = [4,2,3,1].
n=4, k=4, r=1, remainder=0? Actually floor(4/4)=1, remainder=0? Wait n=4, k=4 => r=1, remainder=0? Actually n - r*k = 0, so no remainder block; just one block of size 4. Shift left: [1,4,2,3] -> [4,2,3,1] (move first to end). That matches.
Thus transformation f(p,k) is: Partition p into blocks: first r blocks of exactly length k, and possibly one final block of length rem = n % k (if rem>0). For each block, perform a left cyclic shift by 1 (i.e., move the first element to the end). Then concatenate the results in order.
So the beautiful permutation is defined as: Starting with initial permutation p0 = [1,2,...,n]. Then for k=2 to n, sequentially apply f(•, k). That is: p1 = f(p0,2), p2 = f(p1,3), ..., p_{n-1} = f(p_{n-2}, n). The final p_{n-1} is the beautiful permutation.
We need to compute that efficiently for n up to 10^6. Direct simulation would be O(n^2) if we apply each transformation naively. We need to understand the net effect. It's a sequence of transformations, each is a left cyclic shift on each block of size k.
Observation: These transformations are permutations of indices. For each k, f_k: permutation of n indices, defined as: for each index i (1-indexed), where does the element at position i move to? Actually, it's a map of positions. Since we apply to a permutation, we need to track how the elements move. Equivalent: we have initial array a[i]=i. After applying f_k, we reassign positions: new array b[j] = a[ mapping^{-1}(j) ]? But maybe easier: each f_k is a permutation on positions. We want to compose f_n ∘ f_{n-1} ∘ ... ∘ f_2 applied to identity permutation. The result is the permutation of numbers.
Thus we can compute the overall permutation of positions: Let π be the composition (apply f2 then f3 ... fn) to the identity. Then the beautiful permutation is: for each position i (1..n), the element originally at position π(i) ends up at i? Wait careful.
If we start with identity array A0: A0[j] = j. Then apply transformation T (a permutation on positions) to get new array A1, defined by: A1[T(i)] = A0[i]? Or typical: we take the array and rearrange elements according to block shifts. The operation described: break A into blocks, then rotate each block left by 1. So it's like: for each block, element at position first becomes last. So the new array B satisfies: B[block_start+ i-1] = A[block_start + i] for i=1..L-1, and B[block_end] = A[block_start]. So it's a mapping from old index to new index: old index i (in block) moves to new index = i-1 within block, except first moves to last. So we can define permutation σ_k on positions: for each index i (1-indexed), its new position after applying f_k is:
Let block number = floor((i-1)/k) (0-indexed) but careful about remainder block? Since we have blocks of possibly different sizes? Actually the block sizes are: first r full blocks of size k, then maybe a last block of size rem. But the rule is applied to each block: left cyclic shift. So the mapping depends on which block i lies in and that block's length L (which is k for first r blocks, and rem for last block if rem>0). For a block of length L, positions within block (0-indexed from 0 to L-1) map as: old pos j -> new pos (j-1 mod L) i.e., shift left by 1.
But is the mapping consistent across the whole permutation? Yes. So we can define σ_k(i) = new position (1-indexed) of element originally at position i after applying f_k.
Alternatively, we might think about applying the transformations in order to the identity, and we want to know where each initial number ends up. That's easier: track the final position of each number, or directly compute final array.
We can simulate the process efficiently if we represent the current permutation as something like a list and apply block rotations. The block rotations are basically for each block, we take first element and move it to the end. If we have a data structure that can support splitting and concatenating quickly, we could use a treap or splay tree. But n up to 10^6, and we have to do n-1 operations (k from 2 to n). Each operation does O(number of blocks) modifications, but number of blocks per operation is about n/k (floor). Sum_{k=2}^{n} floor(n/k) = O(n log n). That's about 10^6 * log 10^6 ~ 20e6, which might be acceptable if each block operation is O(1). But careful: each block rotation is a "rotate left by 1" operation on a contiguous segment. That operation can be done in O(1) if we represent the array as a linked list or using a balanced BST with ability to cut and join. However, implementing a treap for 10^6 with O(n log n) operations might be heavy but perhaps okay in C++ but in Python? 10^6 * log2(10^6) ~ 20e6 operations, but Python might be too slow for a treap with many allocations. Need more clever.
We need to derive a pattern or formula for the final permutation.
Maybe the beautiful permutation has a simple closed form.
Let's compute small n manually to see pattern:
n=2:
Start: [1,2]
k=2: one block [1,2] -> rotate left: [2,1] => final: [2,1].
n=3:
Start: [1,2,3]
k=2: blocks: [1,2] [3]? Wait n=3, k=2 => r = floor(3/2)=1, rem=1. Blocks: size2: [1,2], size1: [3]. Rotate left each: [1,2]->[2,1], [3]->[3] => [2,1,3]
k=3: now apply to [2,1,3] with k=3: one block size3 -> rotate left: [1,3,2]? Let's compute: block [2,1,3] rotate left => [1,3,2]? Actually shift left: first element 2 goes to end: becomes [1,3,2]? Wait initial: [2,1,3] => after left shift: indices: 1:2,2:1,3:3 -> new: positions:1 gets old2=1, pos2 gets old3=3, pos3 gets old1=2 => [1,3,2]. Yes final: [1,3,2]. That matches sample output: 1 3 2.
n=4: as sample: final [4,2,3,1].
n=5: Let's compute to see pattern.
We need to simulate manually. Start: [1,2,3,4,5].
Apply k=2:
Blocks: two full of size2? floor(5/2)=2 blocks of size2: [1,2], [3,4]; remainder=1: [5].
Rotate each: [2,1], [4,3], [5] -> [2,1,4,3,5].
Apply k=3:
n=5, current = [2,1,4,3,5].
k=3: floor(5/3)=1 full block of size3: [2,1,4]; remainder=2: [3,5].
Rotate left: [2,1,4] -> [1,4,2]; [3,5] -> [5,3]? Wait rotate left a block of length2: [3,5] -> [5,3] (move first to end). So result: [1,4,2,5,3].
Apply k=4:
n=5, current = [1,4,2,5,3].
k=4: floor(5/4)=1 block of size4: [1,4,2,5]; remainder=1: [3].
Rotate: size4 -> [4,2,5,1]; size1 -> [3] => [4,2,5,1,3].
Apply k=5:
n=5, current = [4,2,5,1,3].
k=5: one block of size5: rotate left => [2,5,1,3,4]? Actually: [4,2,5,1,3] rotate left: first to end: [2,5,1,3,4].
So final: [2,5,1,3,4] for n=5.
But is that correct? Let's verify if we made mistakes. Check: after k=4 we had [4,2,5,1,3]. Then k=5 left shift: becomes [2,5,1,3,4]. So final: 2,5,1,3,4.
Maybe there's known pattern. Let's compute n=6:
We can try to compute quickly perhaps with programmatic reasoning. But we can also derive formula.
Observation: The transformations for k from 2 to n can be composed. Another viewpoint: The operation f(p,k) is like applying a specific permutation: for each block, it's a cyclic shift left by 1. That's a product of disjoint cycles (each cycle is a cyclic permutation of the block). Actually within each block, the left shift is a full cycle: For block of length L, it's the cyclic permutation (1 2 ... L) in positions relative to block? Actually left shift by 1: element at position 1 goes to L, element at 2 goes to 1, ..., element at L goes to L-1. That's a cycle: (1 L L-1 ... 2) maybe? But it's a cyclic permutation of length L: mapping pos -> pos-1 mod L (with 0->L-1). So it's an L-cycle. So f_k is a permutation that consists of cycles: each block of length L is a cycle of length L. So it's a product of cycles, each moving elements within block.
Now composition of many such permutations may lead to something like a specific ordering.
Maybe the final permutation is just the reversal? Not exactly: n=4 gives [4,2,3,1] not reversed (which would be [4,3,2,1]). n=5 gives [2,5,1,3,4] not reversal.
Let's compute n=6 manually to see more pattern.
We'll simulate step by step using reasoning or write small script mentally? Possibly we can derive recurrence.
Let A_n denote the beautiful permutation of length n (after applying f for k=2..n to identity). Compute n=6:
Start: [1,2,3,4,5,6]
k=2: blocks size2: [1,2],[3,4],[5,6]; rotate each -> [2,1,4,3,6,5] -> state S2.
k=3: n=6, k=3: r=2 blocks of size3: [2,1,4] and [3,6,5]? Wait careful: current array: indices: 1:2,2:1,3:4,4:3,5:6,6:5. Actually from previous: [2,1,4,3,6,5]. Now apply k=3. Partition into blocks of size3: first block indices1-3: [2,1,4]; second block indices4-6: [3,6,5]. Rem? floor(6/3)=2, rem=0. So both blocks size3. Rotate left each: [2,1,4] -> [1,4,2]; [3,6,5] -> [6,5,3] (since first3 goes to end: [3,6,5] -> [6,5,3]? Wait: [3,6,5] shift left: index1-> end: becomes [6,5,3]? Actually: new first = old second =6, new second = old third =5, new third = old first =3 => [6,5,3]. Yes). So state after k=3: [1,4,2,6,5,3].
k=4: n=6, k=4: r = floor(6/4)=1 block size4: indices1-4: [1,4,2,6]; remainder block size2: indices5-6: [5,3]. Rotate left: size4: [1,4,2,6] -> [4,2,6,1]; size2: [5,3] -> [3,5]. So state: [4,2,6,1,3,5].
k=5: n=6, k=5: r=1 block size5: indices1-5: [4,2,6,1,3]; remainder size1: index6: [5]. Rotate left: size5 -> [2,6,1,3,4]; size1 stays [5]. So state: [2,6,1,3,4,5].
k=6: n=6, k=6: one block size6: [2,6,1,3,4,5] rotate left -> [6,1,3,4,5,2]? Wait shift left: first element 2 goes to end: becomes [6,1,3,4,5,2]. So final: [6,1,3,4,5,2].
Thus n=6 final: [6,1,3,4,5,2].
Check n=5 we got [2,5,1,3,4]. n=4: [4,2,3,1]. n=3: [1,3,2]. n=2: [2,1]. There is a pattern maybe relating to factorials? Possibly the permutation is such that position i gets element something like (i * something mod (n+1))? Not obvious.
Let's list final arrays for n=2..6:
n=2: [2,1]
n=3: [1,3,2]
n=4: [4,2,3,1]
n=5: [2,5,1,3,4]
n=6: [6,1,3,4,5,2]
What about n=7? Might be computed quickly? Could try to derive formula.
Another perspective: The process is reminiscent of "Josephus" or "shuffling" operations. Maybe the beautiful permutation is actually the inverse of the permutation generated by something like: for i from 1 to n, the element at position i ends up being something like (i * (i+1)/2?) Not.
Let's attempt to think of it as composition of block rotations. Perhaps we can compute final permutation by iterating from n down to 2? Or we might find that the final permutation is simply: for each i from 1 to n, final[i] = something like (i * 2) mod (n+1) if n+1 prime? For n=4, n+1=5, (i2 mod5) yields: i=1->2,2->4,3->1,4->3 => [2,4,1,3] not correct. For n=5, n+1=6: (i2 mod6): 1->2,2->4,3->0? mod6 not in 1..5? Not.
Maybe it's the permutation that reverses order of binary representation? No.
Let's compute more values programmatically (in mind) but maybe we can derive recurrence from process.
Define F_n = permutation obtained after applying f_k for k=2..n to identity. We can try to find relationship between F_n and F_{n-1}? Possibly by analyzing the effect of adding one element at the end? But operations involve k up to n, and the last k=n rotates entire array left. So F_n = rotate_left( G_n ) where G_n is permutation after applying f_k for k=2..n-1? Actually final step is k=n, which rotates the whole array left by 1. So F_n = L( H_n ), where H_n is the state after k=2..(n-1) applied to identity, and L is full left rotation. So F_n[i] = H_n[i+1] for i=1..n-1, and F_n[n] = H_n[1]. So we can relate.
But we need to compute H_n more directly. Could we find that H_n (before final rotate) has some structure? For n=5, H_5 was state after k=2,3,4: we had [4,2,5,1,3]? Actually after k=4 we had [4,2,5,1,3]. Then apply k=5 gave final. So H_5 = [4,2,5,1,3]. For n=4, H_4 = after k=2,3 gave [1,4,2,3]? Actually after k=2,3 we had [1,4,2,3]? Wait earlier: for n=4, after k=2: [2,1,4,3]; after k=3: [1,4,2,3]; then after k=4: final [4,2,3,1]. So H_4 = [1,4,2,3]. For n=6, H_6 = after k=2,3,4,5: after k=5 we had [2,6,1,3,4,5]. Then final after k=6: [6,1,3,4,5,2]. So H_6 = [2,6,1,3,4,5].
Look at H_n:
n=3: after k=2 we had [2,1,3]? Actually final before k=3? For n=3, after k=2: [2,1,3], then k=3 final. So H_3 = [2,1,3]. But final is [1,3,2]. So H_3 = [2,1,3].
n=4: H_4 = [1,4,2,3]
n=5: H_5 = [4,2,5,1,3]? Wait earlier we computed after k=4: [4,2,5,1,3] indeed. So H_5 = [4,2,5,1,3].
n=6: H_6 = [2,6,1,3,4,5]
Look at these H_n. Not obvious.
Maybe there is known result: The beautiful permutation is the order of numbers after performing the "restaurant" shuffle? Could be related to "fracturing" algorithm. Another approach: Represent the process as applying permutations from k=2 to n. Since each f_k is product of disjoint cycles (each block is a cycle). The composition may be expressed as a single permutation that can be computed in O(n log n) using a segment tree? But need to be efficient for n up to 1e6.
We could simulate using an array and performing rotations on each block. However, directly rotating each block (by shifting elements) would be O(L) per block, where L is block length. Total cost sum_{k=2}^{n} sum over blocks (L) = sum_{k=2}^{n} n = (n-1)*n = O(n^2) which is too large.
But we can use a data structure that supports "cut and concatenate" O(log n) per operation, but since we have O(n log n) blocks, total O(n log n * log n) might be heavy but still maybe okay if constants low? Actually the number of blocks across all k is sum_{k=2}^{n} ceil(n/k) ≈ n * H_n ≈ n log n (about 14e6 for n=1e6). If each block operation costs O(log n), that would be ~14e6 * log 1e6 ~ 14e6 * 20 = 280 million operations, too much in Python.
But we can perhaps implement the rotations more cleverly: Since each block rotation is just moving the first element of the block to the end, it's essentially: For a contiguous segment [l, r], we want to do: new segment = old[l+1..r] + old[l]. That is like extracting element at l and inserting it at r. This can be done with a balanced binary tree (like a treap) that supports split and merge. But per block we would need: split into three parts: prefix before block, block, suffix after block. Then split block into first element and rest, then merge rest + first, then merge back. That's constant number of split/merge per block, each O(log n). So total O(number_of_blocks * log n). That's about 14e6 * ~20 = 280 million treap operations, which is too many in Python.
But maybe we can do better: There's a pattern allowing O(n) solution.
Let's examine the effect of the whole process on each element's position. Perhaps we can compute final position of each initial element in O(1) each, or compute final array via a simple iterative algorithm.
Another idea: The transformations for different k are independent and commute? Not exactly because they are applied sequentially, but maybe the composition simplifies.
Consider the effect on positions. Let’s denote by σ_k the permutation on indices that f_k applies: for a given index i (1-indexed) in the array before transformation, after transformation it moves to new index j. But it's easier to think forward: If we have array A, after applying f_k we get B where B[j] = A[?]. The operation: for each block of length L (starting at s, positions s..s+L-1), B[s] = A[s+1], B[s+1] = A[s+2], ..., B[s+L-2] = A[s+L-1], B[s+L-1] = A[s]. So mapping from old index to new index: old index x (with s ≤ x ≤ s+L-1) goes to:
- if x < s+L-1: new index = x+1? Actually B[x+1? Wait careful: B[new_index] gets A[old_index]. For x not the last in block: A[x] becomes B[x-?]. Let's derive: For block, the element at old position s goes to new position s+L-1. Old position s+1 goes to new position s. Old position s+2 goes to new position s+1, etc. So mapping: old position p -> new position:
if p == s: newpos = s+L-1
if p > s: newpos = p-1 (within block)
But note: p ranges s..s+L-1. So we can write: newpos = (p == s ? s+L-1 : p-1). Or equivalently, newpos = (p-2 mod L) + s? Actually if we treat positions within block as offset 0..L-1: offset o = p - s. Then new offset = (o-1 mod L). Because o=0 => -1 mod L = L-1. So newpos = s + ((o-1) mod L). So σ_k is defined as: For each i, let block_start =? Actually blocks are of variable lengths: first r blocks have length k, last block length rem (if rem>0). So we need to determine which block i lies in. But it's a simple function: i belongs to block number b = floor((i-1)/k) if b < r else last block (if rem>0). But since r = floor(n/k), and last block has length L = (n - r*k) if >0 else k? Actually if b < r, then block length = k. If b == r and rem>0, then block length = rem. And if rem=0, then b ranges 0..r-1, all blocks length k.
Thus the transformation depends on i and n and k.
Now we have a composition σ = σ_n ∘ σ_{n-1} ∘ ... ∘ σ_2. Starting from identity array A0 where A0[j] = j. After applying σ_2, we get A1 where A1[σ_2(i)] = A0[i]? Actually careful: If we apply transformation T to array A to produce B, then B[ T(i) ] = A[i] if T maps old index i to new index? Usually when we say "permutation σ on positions", we often define new position of element that was at i is σ(i). In our case, element at old index i moves to new index = σ(i) (with σ as defined above). So after transformation, B[σ(i)] = A[i]. Then next transformation applies another permutation τ, giving C[τ(j)] = B[j]. So overall C[τ(σ(i))] = A[i]. So the final array final[pos] = A0[ π^{-1}(pos) ]? Actually if we set π = σ_n ∘ ... ∘ σ_2, then final[π(i)] = initial[i] = i. So final[pos] = (π^{-1}(pos)). Because if final[π(i)] = i, then for any position p, let i = π^{-1}(p), then final[p] = i. So final permutation is the inverse of π. So we could compute π (the composition of all σ_k) and then invert to get final array. But π maps initial positions to final positions. Alternatively, we could compute directly the final positions of each number.
Maybe easier: compute final positions of each initial element by simulating the effect on its index through each σ_k. That is: start with position x = i (1-indexed), then for k from 2 to n, apply σ_k to the position: x := σ_k(x) (since element moves to new position). After all, final position of number i is x. Then the final array at position x is i. So we can compute pos[i] = final position of i, then fill array accordingly. So we need to compute, for each i, the composition of σ_k applied to i. But we have up to 1e6 i's, and each i would go through n-1 steps => O(n^2). Not good.
But perhaps σ_k have a structure that allows us to update all positions collectively? Or we can reverse the process: start from final positions? Another angle: The entire sequence of operations might be equivalent to a single known permutation like the "bit-reversal" or something.
Let's compute final positions for some numbers for small n to see pattern. For n=5, final array = [2,5,1,3,4]. So final positions: number1 at pos3, 2 at pos1, 3 at pos4, 4 at pos5, 5 at pos2. So pos[1]=3, pos[2]=1, pos[3]=4, pos[4]=5, pos[5]=2. Check if these follow some formula: maybe pos[i] = (i * something) mod (n+1)? For n=5, n+1=6, (i*? mod6) values 1..5? For i=1: maybe 2? not 3. Could be something like: pos[i] = (2i) mod (n+1) but for i=1:2, not 3. Or pos[i] = (3i) mod 6: 3,0? no.
For n=4: final [4,2,3,1] => pos[1]=4, pos[2]=2, pos[3]=3, pos[4]=1. So pos[1]=4, pos[2]=2, pos[3]=3, pos[4]=1. For n=3: final [1,3,2] => pos[1]=1, pos[2]=3, pos[3]=2. For n=2: [2,1] => pos[1]=2, pos[2]=1.
Pattern: For n prime? Not.
Maybe it's related to the order of permutation: This might be the "reverse of the deck after performing a sequence of out-shuffles"? Not.
Let's compute n=6 final: [6,1,3,4,5,2] => pos[1]=2, pos[2]=6, pos[3]=3, pos[4]=4, pos[5]=5, pos[6]=1.
Observing:
- For n=5: pos[1]=3, pos[2]=1, pos[3]=4, pos[4]=5, pos[5]=2.
- For n=6: pos[1]=2, pos[2]=6, pos[3]=3, pos[4]=4, pos[5]=5, pos[6]=1.
Seems like many numbers stay in place? For n=5, 3 stays at 3? Actually pos[3]=4, not same. For n=6, pos[3]=3, pos[4]=4, pos[5]=5. So numbers 3,4,5 are fixed? For n=6, 3,4,5 are at positions 3,4,5 respectively. For n=5, only? 4 at 5? Actually 4 at 5, not fixed. For n=4, 3 at 3 fixed, 2 at 2 fixed? Actually 2 at 2, 3 at 3. For n=4, numbers 2 and 3 fixed? yes. For n=5, numbers? 2 at 1, 3 at 4, 4 at 5, 5 at 2, 1 at 3. None fixed. For n=6, 3,4,5 fixed. n=7? Let's compute n=7 quickly to see.
We'll try to compute n=7 manually or with reasoning.
Simulate n=7 step by step perhaps using a systematic approach.
Start: [1,2,3,4,5,6,7]
k=2: blocks size2: [1,2],[3,4],[5,6]; remainder size1: [7] -> rotate each: [2,1],[4,3],[6,5],[7] -> [2,1,4,3,6,5,7]
k=3: n=7, k=3: r=2 (2*3=6), rem=1. Blocks: first size3: indices1-3: [2,1,4] -> rotate -> [1,4,2]; second size3: indices4-6: [3,6,5] -> rotate -> [6,5,3]? Actually careful: current array after k=2: positions:1:2,2:1,3:4,4:3,5:6,6:5,7:7.
Block2: indices4-6: [3,6,5] -> rotate left: first 3 goes to end => [6,5,3]. remainder block size1: index7: [7] stays. So state after k=3: [1,4,2,6,5,3,7].
k=4: n=7, k=4: r=1 (1*4=4), rem=3. Blocks: size4: indices1-4: [1,4,2,6] -> rotate -> [4,2,6,1]; size3: indices5-7: [5,3,7] -> rotate left -> [3,7,5]? Actually [5,3,7] -> shift left: first 5 goes to end => [3,7,5]. So state: [4,2,6,1,3,7,5].
k=5: n=7, k=5: r=1 (5), rem=2. Blocks: size5: indices1-5: [4,2,6,1,3] -> rotate -> [2,6,1,3,4]; size2: indices6-7: [7,5] -> rotate left -> [5,7] (since [7,5] -> shift left: first7 to end -> [5,7]? Wait: block [7,5] -> positions: first=7, second=5; after left shift: new first = old second =5, new second = old first =7 => [5,7]). So state: [2,6,1,3,4,5,7].
k=6: n=7, k=6: r=1 (6), rem=1. Blocks: size6: indices1-6: [2,6,1,3,4,5] -> rotate -> [6,1,3,4,5,2]; size1: index7: [7] stays. So state: [6,1,3,4,5,2,7].
k=7: n=7, k=7: one block size7: [6,1,3,4,5,2,7] -> rotate left -> [1,3,4,5,2,7,6]? Let's compute: shift left: first element 6 moves to end: becomes [1,3,4,5,2,7,6]. So final: [1,3,4,5,2,7,6].
Thus n=7 final: [1,3,4,5,2,7,6]. So pos[1]=1? Actually final[1]=1, pos[1]=1; pos[2]=5; pos[3]=2; pos[4]=3; pos[5]=4; pos[6]=7; pos[7]=6.
Interesting: 1,4,5? Actually numbers 1,3,4,5? Wait 3 is at position2 not fixed. Let's list positions:
1 at 1,
2 at 5,
3 at 2,
4 at 3,
5 at 4,
6 at 7,
7 at 6.
Only 1 is fixed? Not many.
So pattern not obvious.
Maybe we can compute final permutation more directly using a data structure that can handle the operations efficiently if we realize that the number of operations (block rotations) sum to about n log n, and each rotation is just moving first element of block to end. Could we simulate using a doubly linked list? If we maintain the permutation as a linked list, rotating a block of length L left by 1: that is, take the first element of the block and move it to the end of the block. In a linked list representation, if we have pointers to the start and end of each block, we could do: remove first node of block, insert after last node. That would be O(1) per block, provided we can quickly locate the boundaries of each block. However, locating block boundaries requires knowing where the blocks start and end. Since blocks are consecutive segments of the list, we need to split the list into segments of length k (except maybe last). That would require skipping k nodes each time to find block boundaries. With a linked list, skipping k nodes would cost O(k) per block, leading to O(n) per k again. But we could use a data structure like a balanced tree that can index by position (order-statistic tree). Then each block can be located by split at positions. But that would be O(log n) per split/merge.
But perhaps there is an even simpler O(n) algorithm using the fact that the transformations are independent and we could compute final positions via a formula.
Let's try to derive formula mathematically.
We have permutations σ_k: For each i in 1..n, let block index b = floor((i-1)/k) (0-indexed). Let r = floor(n/k). If b < r, then block length L = k, otherwise L = n - rk (could be 0 if n%k==0, but then b ranges 0..r-1 only). However for b = r and rem > 0, then L = rem. For b < r (full blocks), offset o = i - bk - 1. Then new position = bk + ((o - 1) mod k) + 1 = bk + (o - 1 + k) % k + 1. That simplifies to: if o == 0 => newpos = bk + k; else newpos = bk + o. Actually because ((o-1) mod k) +1 yields: if o>0: (o-1)+1 = o; if o=0: (k-1)+1 = k. So for full blocks: newpos = i if i mod k != 1? Let's check: i is first element of block if (i-1) % k == 0. So if i is not first in a full block, then newpos = i-1? Wait, i>1 within block: o>0, newpos = bk + o = i-1. Indeed: i-1 gives previous index. If i is first, newpos = bk + k = i + k -1. So for indices in full blocks:
- if (i-1) mod k != 0 (i not start of block) then σ_k(i) = i-1.
- if (i-1) mod k == 0 (i is first of block) then σ_k(i) = i + k - 1.
But careful: This is for positions i that are inside a full block (i <= rk). For the last partial block (if rem > 0), the indices i > rk: there we have block length L = rem. Let base = r*k. offset o = i - base - 1 (0-indexed within last block). Then newpos = base + ((o - 1) mod L) + 1. So if o == 0 (first element of last block) then newpos = base + L; else newpos = base + o = i-1. But note that base+L = base+rem = n. So first element of last block goes to position n, and others go to i-1. However note: if rem = 0, then no last block.
Thus σ_k can be described succinctly: For each i from 1 to n,
- if i <= floor(n/k)*k (i.e., i is in a complete block of size k) then:
- if i mod k == 1: σ_k(i) = i + k - 1
- else: σ_k(i) = i - 1
- else (i in the final partial block):
- if i == floor(n/k)*k + 1 (i.e., first of that block): σ_k(i) = n
- else: σ_k(i) = i - 1
Check with earlier examples: n=5, k=3. floor(5/3)=1 => r=1, r*k=3. So i<=3: full blocks (only one block of size3). For i=1: i mod 3 ==1? 1 mod3=1 => σ(1)=1+2=3. For i=2: σ=2-1=1. For i=3: σ=3-1=2? But wait i=3, i mod3==0 not 1, so σ=2. For i>3: partial block of size2 (since rem=2). base=3. i=4: i == base+1? base+1=4 => first of partial => σ(4)=n=5. i=5: σ=5-1=4. Let's see if this matches earlier mapping? We computed f([2,1,4,3,5],3) gave new array [1,4,2,5,3]. According to mapping from old to new positions: we had old indices and their new positions after transformation. In that example, after k=2 we had array: [2,1,4,3,5]. Then apply k=3. Let's verify mapping: Old array: index1=2, index2=1, index3=4, index4=3, index5=5. According to σ, newpos of old index1 (i=1) is 3, so element 2 goes to position3. Index2->1 => 1 goes to pos1. Index3->2 => 4 goes to pos2. Index4->5 => 3 goes to pos5. Index5->4 =>5 goes to pos4. So new array: position1:1, pos2:4, pos3:2, pos4:5, pos5:3 => [1,4,2,5,3]. matches. Good.
So we have a simple formula for σ_k.
Now we need to apply σ_k sequentially for k=2..n. So overall mapping π = σ_n ∘ ... ∘ σ_2.
We need to compute final positions pos[i] = π(i) (if we define as new position of element originally at i). Then final array ans[π(i)] = i. Or we can compute inverse.
We can attempt to compute π(i) for each i by iterating k from 2 to n and updating i = σ_k(i). But that would be O(n^2) if done per i.
But perhaps we can compute π for all i collectively by noticing that σ_k is nearly the identity except for moving the first element of each block to the end of the block. This is reminiscent of the "blocked rotation" that can be seen as: For each block, we perform a cyclic shift of that block left by 1. The composition over k from 2 to n might produce a known permutation: maybe it's the inverse of the permutation that places numbers in "bit-reversed" order? Let's test on n=5: π(i) = final position of i? We computed earlier final array = [2,5,1,3,4] meaning ans[pos] = number. That is ans[1]=2, ans[2]=5, ans[3]=1, ans[4]=3, ans[5]=4. So final position of number i: for i=1, position=3; i=2 pos1; i=3 pos4; i=4 pos5; i=5 pos2. So π(1)=3, π(2)=1, π(3)=4, π(4)=5, π(5)=2. That is π = (1→3,2→1,3→4,4→5,5→2). As permutation cycles: (1 3 4 5 2) maybe? 1→3,3→4,4→5,5→2,2→1 gives a 5-cycle. For n=4, π from final [4,2,3,1]: positions: number1 at pos4, 2 at pos2, 3 at pos3, 4 at pos1. So π(1)=4, π(2)=2, π(3)=3, π(4)=1. That's (1 4)(2)(3). Not a single cycle. For n=3, π(1)=1? Wait final [1,3,2] => number1 at pos1, 2 at pos3, 3 at pos2. So π(1)=1, π(2)=3, π(3)=2 => (1)(2 3). For n=2, π(1)=2, π(2)=1 => (1 2). For n=6, π from [6,1,3,4,5,2]: number1 pos2, 2 pos6, 3 pos3, 4 pos4, 5 pos5, 6 pos1 => π(1)=2, π(2)=6, π(3)=3, π(4)=4, π(5)=5, π(6)=1 => (1 2 6)(3)(4)(5). So many fixed points for larger n? n=6 fixed 3,4,5; n=4 fixed 2,3? Actually n=4 fixed 2,3; n=5 no fixed; n=7 we got final [1,3,4,5,2,7,6] => π(1)=1? Actually number1 at pos1 => π(1)=1; number2 at pos5 => π(2)=5; number3 at pos2 => π(3)=2; number4 at pos3 => π(4)=3; number5 at pos4 => π(5)=4; number6 at pos7 => π(6)=7; number7 at pos6 => π(7)=6. So π has fixed points 1,? Actually 1 fixed, 5? Wait π(5)=4 not fixed; π(6)=7 not fixed; π(7)=6 not fixed; so only 1 is fixed? Also maybe 2? no; 3? π(3)=2; 4->3; 5->4; So cycle (2 5 4 3)? Let's see: 2→5,5→4,4→3,3→2 gives a 4-cycle. And 6→7,7→6 gives a 2-cycle. And 1 fixed. So cycles of various lengths.
So not simple.
We need to compute the composition efficiently. Maybe we can find that the transformation for all k corresponds to sorting by something like the least prime factor? Unlikely.
Alternate viewpoint: The operation f(p,k) is essentially applying a specific permutation that is a product of cycles. If we think of the array as a list, we could simulate using a data structure that allows fast block rotations. Could we use a "rope" or "deque" with ability to split? Python's list slicing may be efficient for moderate sizes but for n up to 1e6 and many operations, it would be too slow if we slice and concatenate each block.
But we could implement using an array representation with lazy shifts? For each block rotation, we just need to move the first element to the end. This is similar to performing a rotation on a segment. Could we use a Fenwick tree or segment tree to represent the array with the ability to move an element from one position to another? But moving many elements across blocks might be done with a technique like "implicit treap" which can support split/merge in O(log n). In Python, a treap might be borderline but perhaps feasible if optimized with iterative loops? 10^6 nodes and 14 million operations (splits/merges) is too much for Python.
We need a more clever mathematical insight.
Let's try to see if the composition can be inverted or expressed as a single formula for the final position of each element.
Let’s denote by x_i(k) the position of element i after applying transformations for k=2..K. We want x_i(n). The update rule for each k: if x is current position, then new position = σ_k(x). So we need to compute iteratively for each x.
But perhaps we can compute final position directly as some function of i and n. Could be related to the representation of i-1 in factorial number system? Because the sequence of operations uses increasing k, which is reminiscent of the process to generate the "Lehmer code" or "factorial number system" for permutations. Indeed, there is known algorithm to generate a random permutation by taking the identity and for i from 2 to n, swap a random position etc. But here we are doing deterministic rotations.
Alternatively, maybe the beautiful permutation is the "inverse" of the permutation that results from the "Floyd's algorithm" for shuffling? Not.
Let's try to see if we can derive recurrence: Let P_n be the beautiful permutation of length n (final array). Could we relate P_n to P_{n-1}? Consider the effect of adding a new element n at the end and performing the operations for k=2..n. Since the last operation (k=n) rotates the whole array, maybe we can derive P_n from P_{n-1} with some insertion.
Let's attempt to compute P_n from P_{n-1} by analyzing the process.
Define process: start with [1,2,...,n]. Apply f(·,2), f(·,3), ..., f(·,n). Let A_n be the final permutation. We want A_n.
Consider A_{n-1} (for numbers 1..n-1). Does A_n relate to A_{n-1}? When we start with n elements, the operations for k from 2 to n-1 are similar but with extra element at the end. And finally k=n does a full rotation. Maybe we can find recurrence.
Let's try to compute A_5 and A_4: A_4 = [4,2,3,1]; A_5 = [2,5,1,3,4]. Not obvious.
Maybe we can compute the permutation of positions for each element i as something like: π(i) = (i * (n) / something) mod something.
Let's compute π(i) for n=5: π(1)=3, π(2)=1, π(3)=4, π(4)=5, π(5)=2. That's like mapping: 1->3, 3->4, 4->5, 5->2, 2->1. Could be expressed as π(i) = (i * 2) mod (n+1)? For n=5, (12)%6=2 not 3; (i3)%6: 13=3->3, 23=6%6=0 invalid; (i4)%6: 14=4 not 3. So no.
Maybe it's related to the order of i in the cyclic group modulo n+1? For n=4, n+1=5, maybe powers of 2? 1->? Not.
Let's compute π^{-1} maybe simpler: final array A_n gives A_n[j] = i means that after process, position j contains i. So we could try to see formula for A_n directly.
For n=2: [2,1]
n=3: [1,3,2]
n=4: [4,2,3,1]
n=5: [2,5,1,3,4]
n=6: [6,1,3,4,5,2]
n=7: [1,3,4,5,2,7,6]
Maybe we can generate A_n by a simple rule: It seems that for even n, the first element is n? n=2 -> 2, n=4 -> 4, n=6 -> 6. For odd n, first element is 1? n=3 -> 1, n=5 -> 2? Actually n=5 first is 2, not 1. n=7 first is 1. So not consistent.
n=5 first is 2, n=3 first is 1, n=7 first is 1. n=1? not considered.
Look at last element: n=2 last=1, n=3 last=2, n=4 last=1, n=5 last=4, n=6 last=2, n=7 last=6. Not obvious.
Maybe there is a pattern: A_n seems to be like the result of repeatedly applying "move first to end of block". Could it be that A_n is the permutation that sorts numbers by something like the largest power of 2 dividing? Let's list for n=6: [6,1,3,4,5,2]. That looks like 6 then 1 then 3,4,5 then 2. It's as if the numbers are arranged in order of decreasing something? Not.
Alternate approach: Since the transformation is essentially a left rotation on each block, maybe we can simulate using a data structure that supports "range rotation by 1" quickly. Could we use a "deque" for the whole array and do something like splitting into blocks? Actually the blocks are contiguous and of varying sizes. But note that the operations are performed sequentially with increasing k. Could we process from k=n down to 2? That might simplify because the last operation (k=n) is a simple left rotation of the whole array. But earlier operations are more complex.
Idea: Use a Fenwick tree or BIT with order statistics to simulate moving elements? Maybe we can represent the array as a linked list and maintain an array next[] and prev[]. For each k, we need to process each block: take the first element of the block and move it to the end of the block. This is like: for each block starting at position s (the first element of the block), we need to cut that node and insert it after the last node of the block. If we have the linked list and we know the head of the list, we can traverse block by block using a pointer. For each block, we can locate its first and last nodes by walking k steps from the start. Since we do this for many k, total walking steps might sum to O(n^2) if we traverse each block entirely. But maybe we can avoid walking by using a technique like "array of positions" and "skip pointers"? Or we can use a data structure like a "balanced binary tree" that can split by index.
But maybe we can simulate all operations in O(n) total time using a clever trick: The operation for a given k: For each block, we take its first element and move it to the end. This can be reinterpreted as: we effectively remove the first element of each block and then append them after the corresponding block. This is similar to performing a "rotation" on each block. But if we maintain the array as a list and we process blocks in order, we might be able to use a "queue" or "buffer" to shift elements without physically moving them, by representing the array as a sequence of segments with offsets (like a piecewise linear function). Since each operation only moves the first element of each block to the end, the number of elements that change position relative to the array may be O(number of blocks) per k, but total moved elements across all k might be O(n log n) (since each element might be moved many times). Perhaps we can simulate by tracking each element's position using a kind of "displacement" that we can update in O(1) per block? But we need to know the order of all elements at the end.
Given n up to 1e6, O(n log n) with a reasonable constant may be acceptable in C++ but Python might struggle if it's > 20 million operations. But we can perhaps implement a custom data structure in Python that is fast enough if we use arrays and simple operations. For example, we could use a "list" and for each block we perform a rotation by 1 using slice assignment, but that would copy O(block size) per block, leading to O(n^2). Not good.
But maybe we can use a "deque" and for each block we can rotate by 1 using a combination of popleft and append? That would be O(k) per block, again O(n^2).
We need a better way.
Let's think about the effect of these transformations collectively. Could there be an invariant that after all transformations, the permutation is the one that lists numbers in order of something like the "binary representation" reversed? Let's test for n=5: numbers 1..5 in binary: 001,010,011,100,101. If we sort by reversed bits? Not.
Maybe the final permutation corresponds to the order of numbers after performing a "perfect shuffle" with varying block sizes. Could be related to the "bit-reversal permutation" used in FFT, but for n=4, bit-reversal (2 bits) gives [1,3,2,4] if starting from 0-index? Not.
Let's compute A_n for n=8 perhaps to see more pattern. Could attempt to simulate quickly with code mentally? Might be long.
Another idea: Since the operations are applied sequentially, we can try to compute the final permutation using a simulation but with a clever representation. For example, we could maintain an array "pos" of positions of each number, and update positions when we apply a transformation. For each k, we need to update positions of elements based on σ_k. However, updating each element individually would be O(n) per k, leading to O(n^2). But maybe we can update only those elements that actually move. According to σ_k, which elements move? Only the first element of each block moves to a different position; all other elements simply shift one left within their block. Actually, for a block of length L, the rotation moves the first element to the end, and all other elements shift left by one. So each element either stays in the same relative order? Actually each element (except the first of each block) moves to position-1. So many elements move by -1. But the first moves to + (L-1). So many elements have their positions decremented by 1. Over many ks, each element may be decremented many times.
If we maintain positions, for each k we could apply a global shift for ranges? But the blocks are interleaved: elements that are not first in their block get position decremented by 1 (i.e., their index decreases by 1). The first elements get their position increased by L-1 (i.e., move to the end of block). So the transformation can be expressed as: For each block [l, r] (where r = l+L-1), we set new position for element originally at l to r, and for elements originally at l+1..r, new position = old position - 1. This is a piecewise linear transformation on positions. However, applying this across all blocks simultaneously is equivalent to: For each i, if i is not the first element of a block, then σ_k(i) = i-1; if i is first element, σ_k(i) = i+L-1. But note that L depends on which block i belongs to.
Thus σ_k(i) = i-1 for all i except those i such that i ≡ 1 (mod k) and i ≤ rk, or i = rk+1 if rem>0. Actually, first elements of full blocks: i = 1, 1+k, 1+2k, ..., 1+(r-1)k. And first element of last partial block (if exists): i = r*k+1. For those, σ_k(i) = i+L-1, where L = k for full blocks, L = rem for partial.
But note that for full blocks, i+L-1 = i + k - 1 = i + k -1. For partial block, i+L-1 = (rk+1) + rem - 1 = rk + rem = n. So the first element of the last partial block moves to position n.
Thus we have:
σ_k(i) =
- if i = r*k+1 (and rem>0): n
- else if i ≤ r*k and i ≡ 1 mod k: i + k - 1
- else: i - 1
Now, consider the composition of these for k from 2 to n. Could we compute final positions by iterating backwards? For each element i, we could simulate its movement, but maybe we can use the property that many steps just decrement the index by 1 unless it's a block start.
Maybe we can think of the process as: For each k from 2 to n, we perform a "rotation" on each block: effectively, we take the first element of each block and move it to the end. This is similar to a stable sort by something like the "excess" of i modulo something.
Consider the trajectory of a particular element. It starts at position i. For each k, its position changes according to the rule. The effect when it's at the start of a block (position congruent to 1 modulo k and within full block, or exactly at r*k+1) it jumps forward; otherwise it moves left by 1. So over the whole process, an element's position tends to move left many times, but occasionally jumps forward when it becomes the first element of a block at some step.
Maybe we can compute the final position analytically: It might be that the final permutation is the inverse of the permutation generated by the "factorial number system" representation of i-1? Because the rule resembles the process of converting a number to factorial base by successive divisions. Indeed, consider the process of repeatedly taking the remainder when dividing by increasing numbers. For example, to compute the Lehmer code of a permutation, you take the number of elements less than the current and so on. But here we have movement.
Alternatively, maybe the beautiful permutation is known as the "permutation generated by the Greedy Approach to the Pancake Problem"? Not.
Let's attempt to derive recurrence by analyzing the process from the end. Another thought: The transformation f(p,k) is invertible. Its inverse is a right cyclic shift on each block (i.e., move last element to front). Perhaps we can compute the beautiful permutation by applying inverses in reverse order to identity? But that might not simplify.
Maybe we can find that the final permutation equals the permutation that sorts numbers by the function: "largest power of 2 dividing the number"? Not.
Let's compute A_n for more n (like n=8) to see pattern; maybe we can guess formula. Since n ≤ 1e6, we could perhaps simulate using an efficient data structure in C++ and it would be fine, but we are in Python. However, we could implement in Python with some optimized approach, like using an array and performing rotations via swapping pointers? Perhaps we can use a "linked list with array of next pointers" and for each block we need to locate block boundaries. If we can quickly jump to the start of each block using a "skip list" that maintains positions? But the blocks are defined by consecutive indices in the current permutation, not original indices. The permutation changes, so the blocks are defined on the current order of elements. So we need to know the current order to partition into blocks of length k. That's tricky: we cannot just compute based on original indices; we need to process the actual sequence.
But maybe we can simulate the process using a "doubly linked list" but also maintain an auxiliary array that gives the position of the "nodes" in order. For each k, we need to process each block: we need to take the first node of the block and move it after the last node of the same block. If we have a linked list, we can traverse from the head, and for each block, we need to know the first and last nodes of that block. We can walk k steps to find the last node. That walk would be O(k) per block, leading to total O(n^2) again because sum over all blocks of k is O(n * number_of_blocks?) Actually for a given k, we have about n/k blocks, and for each block we walk k steps (to find its end). So total steps per k is n (since we walk through the entire list once). Because we process each block by walking through its elements; we might as well just perform the rotation while traversing. For each block, we can take its first node, detach it, then when we reach the end of the block, we reinsert it after the last node. If we traverse the whole list sequentially for each k, we would walk through all n nodes for each k, and total steps = n * (n-1) = O(n^2). That's too much.
But perhaps we can combine traversals? Since we have to apply many ks, we cannot avoid at least O(n log n) total operations if we are clever.
Maybe we can use a data structure that supports "range cyclic shift by 1" in O(log n) time per block. Then total O(n log n) operations, each block being O(log n). That's about 14e6 * log n ~ 280e6, too high for Python.
But maybe we can reduce the number of blocks: many blocks are of size 1? Actually for large k, blocks are few. For k > n/2, floor(n/k)=1, so there are at most 2 blocks (one full of size k and a possible remainder). For k close to n, number of blocks is small. For small k, number of blocks is large (n/k). The total number of blocks is sum_{k=2}^{n} ceil(n/k). This sum is n * H_n - n + something. For n=1e6, H_n ≈ 14.4, so total blocks ≈ 14.4e6 - 1e6 ≈ 13.4e6. That's still large but maybe manageable if each block operation is O(1) with low constant? But can we make each block operation O(1) without needing to split? Possibly we could represent the array as a list and for each block we only need to move the first element to the end. If we could quickly identify the start of each block without scanning the entire list for each k, we might use some kind of "jump table" that maps block boundaries to positions in the array. But the array changes after each operation, so block boundaries move.
Alternative: Instead of simulating the transformations, maybe we can compute the final permutation directly via a formula that uses the "Chinese Remainder Theorem" or something like that. Let's attempt to derive the final position of element 1. Starting at position 1. For k=2, check if it's block start? Since position 1 is always first of the first block, so at k=2, is it considered a block start? For k=2, r = floor(n/2), i=1 ≤ r*k? depends on n. For n≥2, 1 ≤ floor(n/2)2? For n=5, floor(5/2)=2, rk=4, so i=1 ≤4, and i mod 2 =1? 1 mod2 =1, so it is first of a full block. So σ_