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
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Khách du lịch (Tourists)
Tourists
Source: Kattis
Version: Phuoc Hung OJ Extended
Problem Statement
Tree City has attractions labeled through . The attractions are connected by bidirectional roads forming a tree.
For every pair of attractions with and divisible by , a tourist travels from to 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 .
- Each of the next lines contains describing an undirected road.
Output
Output the sum of the lengths of all valid tourist paths.
Subtasks
- Subtask 1 (20 points): , ; the graph is a tree.
- Subtask 2 (30 points): , ; the graph is a tree.
- Subtask 3 (50 points): , ; 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 valid pairs. For instance, the path from to visits and contributes , whereas the path from to consists of and contributes . Summing lengths over all valid pairs gives .