PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000021. Trao đổi sách – bản dễ (Books Exchange (Easy Version))

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Trao đổi sách – bản dễ (Books Exchange (Easy Version))

Books Exchange (Easy Version)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn students numbered from 11 to nn. Initially, student ii holds book ii. At the end of each day, every student simultaneously passes the book currently in their possession to student pip_i.

The sequence pp is a permutation of 1,2,…,n1,2,\ldots,n. Hence each student sends exactly one book and receives exactly one book each day. All transfers happen simultaneously; a book received during a day cannot be passed on again that same day.

For each student ii, find the smallest positive number of days after which their original book returns to them. All students keep following the given transfer rule.

Input

The first line contains nn. The second line contains a permutation p1,…,pnp_1,\ldots,p_n.

Output

Print nn integers; the ii-th is the first return day of student ii's original book.

Subtasks

  • Subtask 1 (20 points): 1≤n≤121\le n\le12; pp is a permutation of 1,2,…,n1,2,\ldots,n.
  • Subtask 2 (30 points): 1≤n≤751\le n\le75; pp is a permutation of 1,2,…,n1,2,\ldots,n.
  • Subtask 3 (50 points): 1≤n≤2001\le n\le200; pp is a permutation of 1,2,…,n1,2,\ldots,n.

Examples

Input

5
5 1 2 4 3

Output

4 4 4 1 4

Explanation

The exchanges follow the cycle 1→5→3→2→11\to5\to3\to2\to1, which contains four people. Each person in positions 1,2,3,51,2,3,5 can therefore receive four different books. Position 44 forms a cycle of length one, giving 4 4 4 1 4.