#DSU0000026. Trao đổi sách – bản khó (Books Exchange (Hard Version))
Bài tập thuộc lộ trình
Mục lục đề ⌄
Trao đổi sách – bản khó (Books Exchange (Hard Version))
Books Exchange (Hard Version)
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
There are students, numbered from to . Student initially holds book . At the end of every day, all students simultaneously pass their current book onward: student gives it to student .
Because is a permutation, every student receives exactly one book after each round. Each book continues to move according to the same rule on subsequent days. Books are neither renumbered nor removed during the process.
For every , determine the minimum positive number of days before book returns to student for the first time. Output one answer per student, in order.
Input
The first line contains . The second contains the permutation .
Output
Print first-return days.
Subtasks
- Subtask 1 (20 points): ; is a permutation of .
- Subtask 2 (30 points): ; is a permutation of .
- Subtask 3 (50 points): ; is a permutation of .
Examples
Input
6
4 6 2 1 5 3
Output
2 3 3 2 1 3
Explanation
The permutation consists of the cycles of length , of length , and of length . A person receives books only from their own cycle. The result is 2 3 3 2 1 3.