#P16161. [ICPC 2016 NAIPC] Tourists

[ICPC 2016 NAIPC] Tourists

题目描述

In Tree City, there are nn tourist attractions uniquely labeled 11 to nn. The attractions are connected by a set of n1n - 1 bidirectional roads in such a way that a tourist can get from any attraction to any other using some path of roads.

You are a member of the Tree City planning committee. After much research into tourism, your committee has discovered a very interesting fact about tourists: they LOVE number theory! A tourist who visits an attraction with label xx will then visit another attraction with label yy if y>xy > x and yy is a multiple of xx. Moreover, if the two attractions are not directly connected by a road the tourist will necessarily visit all of the attractions on the path connecting xx and yy, even if they aren’t multiples of xx. The number of attractions visited includes xx and yy themselves. Call this the length of a path.

Consider this city map:

:::align{center} :::

Here are all the paths that tourists might take, with the lengths for each:
12=41 \to 2 = 4, 13=31 \to 3 = 3, 14=21 \to 4 = 2, 15=21 \to 5 = 2, 16=31 \to 6 = 3, 17=41 \to 7 = 4,
18=31 \to 8 = 3, 19=31 \to 9 = 3, 110=21 \to 10 = 2, 24=52 \to 4 = 5, 26=62 \to 6 = 6, 28=22 \to 8 = 2,
210=32 \to 10 = 3, 36=33 \to 6 = 3, 39=33 \to 9 = 3, 48=44 \to 8 = 4, 510=35 \to 10 = 3

To take advantage of this phenomenon of tourist behavior, the committee would like to determine the number of attractions on paths from an attraction xx to an attraction yy such that y>xy > x and yy is a multiple of xx. You are to compute the sum of the lengths of all such paths. For the example above, this is: $4 + 3 + 2 + 2 + 3 + 4 + 3 + 3 + 2 + 5 + 6 + 2 + 3 + 3 + 3 + 4 + 3 = 55$.

输入格式

Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. The first line of input will consist of an integer nn (2n200,0002 \leq n \leq 200,000) indicating the number of attractions. Each of the following n1n - 1 lines will consist of a pair of space-separated integers ii and jj (1i<jn1 \leq i < j \leq n), denoting that attraction ii and attraction jj are directly connected by a road. It is guaranteed that the set of attractions is connected.

输出格式

Output a single integer, which is the sum of the lengths of all paths between two attractions xx and yy such that y>xy > x and yy is a multiple of xx.

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