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

#DSU0000026. Trao đổi sách – bản khó (Books Exchange (Hard 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 khó (Books Exchange (Hard Version))

Books Exchange (Hard Version)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn students, numbered from 11 to nn. Student ii initially holds book ii. At the end of every day, all students simultaneously pass their current book onward: student ii gives it to student pip_i.

Because pp 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 ii, determine the minimum positive number of days before book ii returns to student ii for the first time. Output one answer per student, in order.

Input

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

Output

Print nn first-return days.

Subtasks

  • Subtask 1 (20 points): 1≤n≤251\le n\le25; pp is a permutation of 1,2,…,n1,2,\ldots,n.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le2000; pp is a permutation of 1,2,…,n1,2,\ldots,n.
  • Subtask 3 (50 points): 1≤n≤2⋅1051\le n\le2\cdot10^5; pp is a permutation of 1,2,…,n1,2,\ldots,n.

Examples

Input

6
4 6 2 1 5 3

Output

2 3 3 2 1 3

Explanation

The permutation consists of the cycles (1,4)(1,4) of length 22, (2,6,3)(2,6,3) of length 33, and (5)(5) of length 11. A person receives books only from their own cycle. The result is 2 3 3 2 1 3.