This page is still under construction.

Parts of this page are still being built. What you see may change.

Tourists

Time limit5sMemory limit512 MB

Summary
Given a tree on n nodes, sum the number of nodes on the path from x to y over all pairs where y is a larger multiple of x.
Level

Hard8 of 10

Topics
Tree, Math, DFS, Number theory
Solved
No attempts yet

Problem

Tree City has nn tourist attractions, labeled 1 to nn. The attractions are joined by n−1n - 1 bidirectional roads, and a tourist can reach any attraction from any other along those roads.

You sit on the Tree City planning committee. After a long study of tourism, the committee found an unusual habit: tourists love number theory. A tourist who visits the attraction labeled xx then visits the attraction labeled yy whenever y>xy > x and yy is a multiple of xx. If the two attractions are not joined by a road, the tourist passes through every attraction on the path from xx to yy, including the attractions whose labels are not multiples of xx. The length of a path is the number of attractions the tourist visits, counting xx and yy themselves.

Consider this city map.

Its roads join 3 and 4, 3 and 7, 1 and 4, 4 and 6, 1 and 10, 8 and 10, 2 and 8, 1 and 5, 4 and 9. Here are the paths tourists might take, with the length of each.

1 -> 2 = 4, 1 -> 3 = 3, 1 -> 4 = 2, 1 -> 5 = 2, 1 -> 6 = 3, 1 -> 7 = 4,
1 -> 8 = 3, 1 -> 9 = 3, 1 -> 10 = 2, 2 -> 4 = 5, 2 -> 6 = 6, 2 -> 8 = 2,
2 -> 10 = 3, 3 -> 6 = 3, 3 -> 9 = 3, 4 -> 8 = 4, 5 -> 10 = 3

The lengths add up to 4 + 3 + 2 + 2 + 3 + 4 + 3 + 3 + 2 + 5 + 6 + 2 + 3 + 3 + 3 + 4 + 3 = 55.

The committee wants that total for the whole city. Take every pair of attractions (x,y)(x, y) with y>xy > x and yy a multiple of xx, and compute the sum of the lengths of the paths from xx to yy.

Input

The first line contains an integer nn, the number of attractions. (2≤n≤200 0002 \le n \le 200\,000)

Each of the next n−1n - 1 lines contains two space separated integers ii and jj (1≤i<j≤n1 \le i < j \le n), meaning that attraction ii and attraction jj are joined by a road. The attractions are connected.

Output

Print one integer on a single line: the sum of the lengths of the paths from xx to yy over every pair of attractions (x,y)(x, y) with y>xy > x and yy a multiple of xx.

Examples4

  1. Example 1

    Input
    10
    3 4
    3 7
    1 4
    4 6
    1 10
    8 10
    2 8
    1 5
    4 9
    
    Expected output
    55
    
  2. Example 2

    Input
    2
    1 2
    
    Expected output
    2
    
  3. Example 3

    Input
    3
    1 2
    2 3
    
    Expected output
    5
    
  4. Example 4

    Input
    4
    1 2
    1 3
    1 4
    
    Expected output
    9