CF986E.Prince's Problem
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 .
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) , 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) for all vertices w on the path from u to v . As it might be large, compute it modulo 109+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.
输入格式
In the first line of input there is one integer n ( 1≤n≤105 ) — 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 ).
In the next line there is one integer q ( 1≤q≤105 ) — 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 ).
输出格式
Print q numbers — the answers to the queries in the order they are given in the input. Print each answer modulo 109+7=1000000007 . Print each number on a separate line.
输入输出样例
输入#1
4 1 2 1 3 1 4 6 4 9 5 3 2 3 6 2 3 2 3 4 7
输出#1
36 4 1
输入#2
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
输出#2
196000 12250 999998215