#LCA0000091. Tổng lũy thừa độ sâu trên đường đi (Sum of Powers of Depths)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tổng lũy thừa độ sâu trên đường đi (Sum of Powers of Depths)
Sum of Powers of Depths
Source: Luogu
Version: Phuoc Hung OJ Extended
Problem Statement
An undirected tree of vertices is rooted at vertex . The depth of vertex is the number of edges on the path from vertex to ; in particular .
For each of queries , sum over all vertices of the unique path from to , including both endpoints. Print the result modulo . Different queries may use different exponents . Since , the root contributes if it lies on the path.
Input
The first line contains ; the next lines contain undirected edges . Next comes , followed by queries .
Output
Print one integer per query: the requested sum modulo .
Subtasks
-
Subtask 1 (20 points): ; ; ; ; edges form an undirected tree rooted at .
-
Subtask 2 (30 points): ; ; ; ; edges form an undirected tree rooted at .
-
Subtask 3 (50 points): ; ; ; ; edges form an undirected tree rooted at .
Examples
Input
5
1 2
1 3
2 4
2 5
2
1 4 5
5 4 45
Output
33
503245989
Explanation
The root has depth ; vertices have depth ; vertices have depth . Path yields . Path yields .