time limit:per test 3 seconds
memory limit:per test 512 megabytes
standard input
standard output
Description
Let the main characters of this problem be personages from some recent movie. New Avengers seem to make a lot of buzz. I didn’t watch any part of the franchise and don’t know its heroes well, but it won’t stop me from using them in this problem statement. So, Thanos and Dr. Strange are doing their superhero and supervillain stuff, but then suddenly they stumble across a regular competitive programming problem.
You are given a tree with n vertices.
In each vertex v there is positive integer av a v .
You have to answer q queries.
Each query has a from u v x.
You have to calculate ∏w∈Pgcd(x,aw)mod(109+7) ∏ w ∈ P gcd ( x , a w ) mod ( 10 9 + 7 ) , where P is a set of vertices on path from u to v. In other words, you are to calculate the product of gcd(x,aw) gcd ( x , a w ) for all vertices w on the path from u to v. As it might be large, compute it modulo 109+7 10 9 + 7 . Here gcd(s,t) denotes the greatest common divisor of s and t.
Note that the numbers in vertices do not change after queries.
I suppose that you are more interested in superhero business of Thanos and Dr. Strange than in them solving the problem. So you are invited to solve this problem instead of them.
Input
In the first line of input there is one integer n
(1≤n≤105)
(
1
≤
n
≤
10
5
)
— the size of the tree.
In the next n−1 lines the edges of the tree are described. The i-th edge is described with two integers ui and vi (1≤ui,vi≤n) and it connects the vertices ui and vi. It is guaranteed that graph with these edges is a tree.
In the next line there are n integers a1,a2,…,an (1≤av≤107) ( 1 ≤ a v ≤ 10 7 ) .
In the next line there is one integer q (1≤q≤105) ( 1 ≤ q ≤ 10 5 ) — the number of queries.
And in the next q lines the queries are described. Each query is described with three integers ui, vi and xi (1≤ui,vi≤n,1≤xi≤107) ( 1 ≤ u i , v i ≤ n , 1 ≤ x i ≤ 10 7 ) .
Output
Print q numbers — the answers to the queries in the order they are given in the input. Print each answer modulo 1000000007. Print each number on a separate line.
Examples
input1
4
1 2
1 3
1 4
6 4 9 5
3
2 3 6
2 3 2
3 4 7
output1
36
4
1
input2
6
1 2
2 3
2 4
1 5
5 6
100000 200000 500000 40000 800000 250000
3
3 5 10000000
6 2 3500000
4 1 64000
output2
196000
12250
999998215
题目大意
一棵树,n个节点,每个节点有一个权值ai,q个询问,每个询问查询vi到ui路径上权值和xi的gcd的乘积
Analysis
不用多想先分解质因数,分不同质因数进行计算
考虑同一个质因数p,将询问的x和节点的a含有的p的最高幂次k放在一起,对于一个询问
x=pk
x
=
p
k
当路径上某个节点幂次小于k时,贡献为它自己,如果大于或等于k,贡献为k
那么按照k排序,对于每一个k,维护点到根路径上的小于k的幂次总和,和大于k的幂次个数,那么对于一个询问即查询
query(u)+query(v)−query(lca(u,v))−query(fa(lca(u,v)))
q
u
e
r
y
(
u
)
+
q
u
e
r
y
(
v
)
−
q
u
e
r
y
(
l
c
a
(
u
,
v
)
)
−
q
u
e
r
y
(
f
a
(
l
c
a
(
u
,
v
)
)
)
其中需要区间加和单点查询,线段树或树状数组都行