A11828 | Prince's Problem
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $a_{v}$ .
You have to answer $q$ queries.
Each query has a from $u$ $v$ $x$ .
You have to calculate $\prod_{w \in 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, a_{w})$ for all vertices $w$ on the path from $u$ to $v$ . As it might be large, compute it modulo $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.
You are given a tree with $n$ vertices.
In each vertex $v$ there is positive integer $a_{v}$ .
You have to answer $q$ queries.
Each query has a from $u$ $v$ $x$ .
You have to calculate $\prod_{w \in 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, a_{w})$ for all vertices $w$ on the path from $u$ to $v$ . As it might be large, compute it modulo $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.
输入格式
In the first line of input there is one integer $n$ ( $1 \le n \le 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 $u_{i}$ and $v_{i}$ ( $1 \le u_{i}, v_{i} \le n$ ) and it connects the vertices $u_{i}$ and $v_{i}$ . It is guaranteed that graph with these edges is a tree.
In the next line there are $n$ integers $a_1, a_2, \ldots, a_n$ ( $1 \le a_{v} \le 10^{7}$ ).
In the next line there is one integer $q$ ( $1 \le q \le 10^{5}$ ) — the number of queries.
And in the next $q$ lines the queries are described. Each query is described with three integers $u_{i}$ , $v_{i}$ and $x_{i}$ ( $1 \le u_{i}, v_{i} \le n$ , $1 \le x_{i} \le 10^{7}$ ).
In the next $n-1$ lines the edges of the tree are described. The $i$ -th edge is described with two integers $u_{i}$ and $v_{i}$ ( $1 \le u_{i}, v_{i} \le n$ ) and it connects the vertices $u_{i}$ and $v_{i}$ . It is guaranteed that graph with these edges is a tree.
In the next line there are $n$ integers $a_1, a_2, \ldots, a_n$ ( $1 \le a_{v} \le 10^{7}$ ).
In the next line there is one integer $q$ ( $1 \le q \le 10^{5}$ ) — the number of queries.
And in the next $q$ lines the queries are described. Each query is described with three integers $u_{i}$ , $v_{i}$ and $x_{i}$ ( $1 \le u_{i}, v_{i} \le n$ , $1 \le x_{i} \le 10^{7}$ ).
输出格式
Print $q$ numbers — the answers to the queries in the order they are given in the input. Print each answer modulo $10^9+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
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted