#GL0000367. Tích GCD trên đường đi của cây (Prince's Problem — Product of GCD on a Tree Path)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tích GCD trên đường đi của cây (Prince's Problem — Product of GCD on a Tree Path)
Prince's Problem — Product of GCD on a Tree Path
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an undirected tree of vertices. Vertex has a fixed positive integer label .
For each query , consider the unique simple path from to , including both endpoints. Let be its vertices. Compute
$$F(u,v,x)=\prod_{w\in P(u,v)}\gcd(x,a_w)\pmod{10^9+7}.$$If , the product has exactly one factor, . No labels are updated.
Input
- Line 1: integer .
- Next lines: edges .
- Next line: labels .
- Next line: integer .
- Next lines: queries .
Output
Print one answer per line, in query order.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; .
Every input describes a tree; all vertex indices lie in and edge endpoints differ.
Examples
Example 1
Input
4
1 2
1 3
1 4
6 4 9 5
3
2 3 6
2 3 2
3 4 7
Output
36
4
1
Explanation
For the path is and the labels are , giving $\gcd(6,4)\cdot\gcd(6,6)\cdot\gcd(6,9)=2\cdot6\cdot3=36$. With , the same path yields . For , all three labels on the path are coprime to , giving .
Example 2
Input
1
12
3
1 1 18
1 1 5
1 1 12
Output
6
1
12
Explanation
The only path consists of a single vertex with label . The answers are respectively , , and .