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

#LCA0000099. Khách du lịch (Tourists)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Khách du lịch (Tourists)

Tourists

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

Tree City has nn attractions labeled 11 through nn. The attractions are connected by n−1n-1 bidirectional roads forming a tree.

For every pair of attractions (x,y)(x,y) with x<yx<y and yy divisible by xx, a tourist travels from xx to yy along the unique path. The path length is the number of attractions visited along the path, including both endpoints (not the number of edges).

Compute the sum of the lengths of all such tourist paths.

Input

  • The first line contains nn.
  • Each of the next n−1n-1 lines contains u,vu,v describing an undirected road.

Output

Output the sum of the lengths of all valid tourist paths.

Subtasks

  • Subtask 1 (20 points): 2≤n≤102\le n\le 10, 1≤u<v≤n1\le u<v\le n; the graph is a tree.
  • Subtask 2 (30 points): 2≤n≤20002\le n\le 2000, 1≤u<v≤n1\le u<v\le n; the graph is a tree.
  • Subtask 3 (50 points): 2≤n≤2000002\le n\le 200000, 1≤u<v≤n1\le u<v\le n; the graph is a tree.

Examples

Input

10
3 4
3 7
1 4
4 6
1 10
8 10
2 8
1 5
4 9

Output

55

Explanation

There are 1717 valid pairs. For instance, the path from 11 to 22 visits 1,10,8,21,10,8,2 and contributes 44, whereas the path from 11 to 44 consists of 1,41,4 and contributes 22. Summing lengths over all valid pairs gives 5555.