#SP0000256. Cây thừa số (Factor Tree)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Cây thừa số (Factor Tree)
Factor Tree
Source: CodeChef
Version: Phuoc Hung OJ Extended
Problem Statement
ChefLand consists of cities numbered from to , connected by bidirectional roads forming a tree: there is exactly one simple path between any two cities.
City has a positive integer house value . A query specifies cities (possibly equal). Consider the product of all for cities on the unique simple path from to , including both endpoints.
For each query, output the number of positive divisors of that product, modulo . There are independent queries, with no updates to the tree or its values.
Input
The first line contains , the number of cities.
Each of the next exactly lines contains two integers specifying a bidirectional road. These edges form a tree.
The following line contains exactly positive integers , the city values.
The next line contains , the number of queries.
Each of the last exactly lines contains two city indices , the endpoints of the requested path.
Output
Print exactly lines. Line contains the number of positive divisors of the product of values on the path specified by query , modulo .
Subtasks
- Subtask 1 (10 points): ; ; for all ; ; the edges form a tree.
- Subtask 2 (30 points): ; ; for all ; ; the edges form a tree.
- Subtask 3 (60 points): ; ; for all ; ; the edges form a tree.
Examples
Input
5
1 2
1 3
2 4
2 5
2 6 4 3 5
2
1 4
2 2
Output
9
4
Explanation
The tree edges are , , , , and node values are .
- Query traverses . The product is . Its divisor count is .
- Query contains only node , whose value is , with positive divisors.
Both values are below the modulus, so the output is followed by .