【codeforces986E】 Prince's Problem

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 wPgcd(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 (1n105) ( 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 (1av107) ( 1 ≤ a v ≤ 10 7 ) .

In the next line there is one integer q (1q105) ( 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 (1ui,vin,1xi107) ( 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 ) ) )
其中需要区间加和单点查询,线段树或树状数组都行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值