Master Zhu and Rikka
Time limit3sMemory limit512 MB
Given a rooted tree with values on vertices, answer queries that ask for the GCD of sums of values occurring exactly a times and exactly b times in a subtree or on a path.
- Level
Hard9 of 10
- Topics
- Tree, DFS, Prefix sum, Number theory
- Solved
- No attempts yet
Problem
As everyone knows, Master Zhu is the most powerful man in the universe. He has infinite power to protect the world.
A magical rooted tree grows in Master Zhu's garden. The tree has vertices and edges. The root of the tree is vertex . Each vertex holds an amount of Zhu power equal to .
Little Rikka is a curious girl: she has questions to ask Master Zhu. But Master Zhu is busy protecting our world right now, so he wants you to help him answer Rikka's questions.
Each of Rikka's questions has the format " ".
- If is , then equals , and Rikka looks at the subtree rooted at vertex and wants to know the GCD (greatest common divisor) of and , where is the sum of the numbers that appear exactly times in this subtree and is the sum of the numbers that appear exactly times in this subtree.
- If is , Rikka looks at the simple path between vertices and and wants to know the GCD of and , where is the sum of the numbers that appear exactly times on this path and is the sum of the numbers that appear exactly times on this path.
Here, for any , we define .
Input
The first line of input contains an integer , the number of test cases ().
The first line of each test case contains two integers and : the number of vertices in the magical tree and the number of Rikka's questions ().
The next line contains integers , , , : the amount of Zhu power in each vertex ().
Each of the next lines contains two integers and and denotes an edge connecting vertices and (). It is guaranteed that these edges form a tree.
Each of the next lines contains one of Rikka's questions in the format described above (, ).
Output
For each of Rikka's questions, print the answer on a separate line.