Problem Summary Input: A permutation 𝑃 = ( 𝑃 1 , 𝑃 2 , … , 𝑃 𝑁 ) P=(P 1 ,P 2 …
- User
Problem Summary Input:
A permutation 𝑃
( 𝑃 1 , 𝑃 2 , … , 𝑃 𝑁 ) P=(P 1 ,P 2 ,…,P N ) of integers ( 1 , 2 , … , 𝑁 ) (1,2,…,N). An integer 𝐾 K which denotes the number of times to apply a specific operation. Operation:
Perform the following operation K times: For each 𝑖
1 , 2 , … , 𝑁 i=1,2,…,N, simultaneously update 𝑃 𝑖 P i to 𝑃 𝑃 𝑖 P P i
. Objective:
Output the resulting permutation 𝑃 P after performing the operation 𝐾 K times. Constraints 1 ≤ 𝑁 ≤ 2 × 1 0 5 1≤N≤2×10 5
1 ≤ 𝐾 ≤ 1 0 18 1≤K≤10 18
1 ≤ 𝑃 𝑖 ≤ 𝑁 1≤P i ≤N 𝑃 𝑖 ≠ 𝑃 𝑗 P i
=P j for all 1 ≤ 𝑖 < 𝑗 ≤ 𝑁 1≤i<j≤N (ensuring 𝑃 P is a valid permutation) Observations and Approach High Value of K:
Since 𝐾 K can be as large as 1 0 18 10 18 , a direct simulation would be infeasible due to time limits. Permutation Cycle Detection:
Since we're dealing with a permutation, applying the operation multiple times will eventually repeat the permutation structure in cycles. By identifying cycles within the permutation, we can leverage modular arithmetic to find the effect of performing the operation 𝐾 K times by simulating only as many steps as the length of each cycle (since each cycle would repeat). Binary Exponentiation on Permutations:
Using a technique similar to binary exponentiation, we can efficiently compute the permutation after 𝐾 K operations by squaring transformations. This allows us to "jump" in powers of 2, reducing the number of computations required to achieve the final permutation. Plan Parse Input: Read 𝑁 N, 𝐾 K, and the permutation 𝑃 P. Apply Binary Exponentiation on Permutations: Precompute powers of the permutation transformations. Use these precomputed powers to determine the final state of 𝑃 P after 𝐾 K operations. Output the Result: Print the final permutation. C++でコードを書いて
- Assistant