CF986E.Prince's Problem

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:512MB

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 nn vertices.

In each vertex vv there is positive integer ava_{v}.

You have to answer qq queries.

Each query has a from uu vv xx.

You have to calculate ∏w∈Pgcd(x,aw)mod  (109+7)\prod_{w \in P} gcd(x, a_{w}) \mod (10^{9} + 7), where PP is a set of vertices on path from uu to vv. In other words, you are to calculate the product of gcd(x,aw)gcd(x, a_{w}) for all vertices ww on the path from uu to vv. As it might be large, compute it modulo 109+710^9+7. Here gcd(s,t)gcd(s, t) denotes the greatest common divisor of ss and tt.

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.

本题的主角来自某部近期上映的电影。新复仇者联盟似乎引起了广泛关注。我并未观看过该系列电影的任何一部,也不太了解其中的英雄人物,但这并不会妨碍我在本题题面中使用他们。于是,灭霸(Thanos)与奇异博士(Dr. Strange)正在各自履行超级英雄与超级反派的职责,却突然意外撞上了一道普通的算法竞赛题目。

给你一棵包含 nn 个顶点的树。

每个顶点 vv 上有一个正整数 ava_{v}。

你需要回答 qq 个询问。

每个询问形如 uu vv xx。

你需要计算 ∏w∈Pgcd⁡(x,aw) mod (109+7)\prod_{w \in P} \gcd(x, a_{w}) \bmod (10^{9} + 7),其中 PP 表示从顶点 uu 到顶点 vv 的路径上所有顶点构成的集合。换言之,你需要计算路径 u→vu \to v 上每个顶点 ww 对应的 gcd⁡(x,aw)\gcd(x, a_{w}) 的乘积,并对 109+710^9+7 取模。此处 gcd⁡(s,t)\gcd(s, t) 表示 ss 与 tt 的最大公约数。

注意:顶点上的数值在所有询问过程中均不发生改变。

我们猜想,你可能更关心灭霸与奇异博士的超级英雄事务,而非他们解这道题的过程。因此,诚邀你代替他们来解决本题。

输入格式

In the first line of input there is one integer nn (1≤n≤1051 \le n \le 10^{5}) — the size of the tree.

In the next n−1n-1 lines the edges of the tree are described. The ii-th edge is described with two integers uiu_{i} and viv_{i} (1≤ui,vi≤n1 \le u_{i}, v_{i} \le n) and it connects the vertices uiu_{i} and viv_{i}. It is guaranteed that graph with these edges is a tree.

In the next line there are nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤av≤1071 \le a_{v} \le 10^{7}).

In the next line there is one integer qq (1≤q≤1051 \le q \le 10^{5}) — the number of queries.

And in the next qq lines the queries are described. Each query is described with three integers uiu_{i}, viv_{i} and xix_{i} (1≤ui,vi≤n1 \le u_{i}, v_{i} \le n, 1≤xi≤1071 \le x_{i} \le 10^{7}).

输入的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^{5})——树的大小。

接下来的 n−1n-1 行描述了树的边。第 ii 条边由两个整数 uiu_{i} 和 viv_{i}(1≤ui,vi≤n1 \le u_{i}, v_{i} \le n)描述,表示连接顶点 uiu_{i} 和 viv_{i} 的一条边。保证由这些边构成的图是一棵树。

接下来的一行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤av≤1071 \le a_{v} \le 10^{7})。

再下一行包含一个整数 qq(1≤q≤1051 \le q \le 10^{5})——查询的数量。

接下来的 qq 行描述了各个查询。每个查询由三个整数 uiu_{i}、viv_{i} 和 xix_{i}(1≤ui,vi≤n1 \le u_{i}, v_{i} \le n,1≤xi≤1071 \le x_{i} \le 10^{7})描述。

输出格式

Print qq numbers — the answers to the queries in the order they are given in the input. Print each answer modulo 109+7=100000000710^9+7 = 1000000007. Print each number on a separate line.

输出 qq 个数字——即按输入中给出的顺序对应的查询答案。每个答案需对 109+7=100000000710^9+7 = 1000000007 取模。每个数字单独占一行。

输入输出样例

  • 输入#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

输入解题思路,AI测评打分。不知道怎么写?

首页