6850.Permutation Rounds

Time Limit: 1s Memory Limit: 512MB

There is a sorted array $[1,2,\dots,n]$ and a permutation $p_1,p_2,\dots,p_n$ . On each round, all elements move according to the permutation: the element at position $i$ moves to position $p_i$ .

After how many rounds is the array sorted again for the first time?

Input Format(From the terminal/stdin)

The first line has an integer $n$ .

The next line contains $n$ integers $p_1,p_2,\dots,p_n$ .

  • $1 \le n \le 2 \cdot 10^5$

Output Format(To the terminal/stdout)

Print the number of rounds modulo $10^9+7$ .

Sample Input

Copy
8
5 3 2 6 4 1 8 7
 \n
 · · · · · · · \n

Sample Output

Copy
4
 \n
  • Round 1: $[6,3,2,5,1,4,8,7]$
  • Round 2: $[4,2,3,1,6,5,7,8]$
  • Round 3: $[5,3,2,6,4,1,8,7]$
  • Round 4: $[1,2,3,4,5,6,7,8]$
Source: CSES, Mathematics, 3398

Submit

请先 登录

© 2025 FAQs