A15299 | Electrical Efficiency
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
In the country of Dengkleknesia, there are $N$ factories numbered from $1$ to $N$ . Factory $i$ has an electrical coefficient of $A_i$ . There are also $N-1$ power lines with the $j$ -th power line connecting factory $U_j$ and factory $V_j$ . It can be guaranteed that each factory in Dengkleknesia is connected to all other factories in Dengkleknesia through one or more power lines. In other words, the collection of factories forms a tree. Each pair of different factories in Dengkleknesia can use one or more existing power lines to transfer electricity to each other. However, each power line needs to be turned on first so that electricity can pass through it.
Define $f(x, y, z)$ as the minimum number of power lines that need to be turned on so that factory $x$ can make electrical transfers to factory $y$ and factory $z$ . Also define $g(x, y, z)$ as the number of distinct prime factors of $\text{GCD}(A_x, A_y, A_z)$ .
To measure the electrical efficiency, you must find the sum of $f(x, y, z) \times g(x, y, z)$ for all combinations of $(x, y, z)$ such that $1 \leq x < y < z \leq N$ . Because the answer can be very large, you just need to output the answer modulo $998\,244\,353$ .
Note: $\text{GCD}(k_1, k_2, k_3)$ is the greatest common divisor of $k_1$ , $k_2$ , and $k_3$ , which is the biggest integer that simultaneously divides $k_1$ , $k_2$ , and $k_3$ .
Define $f(x, y, z)$ as the minimum number of power lines that need to be turned on so that factory $x$ can make electrical transfers to factory $y$ and factory $z$ . Also define $g(x, y, z)$ as the number of distinct prime factors of $\text{GCD}(A_x, A_y, A_z)$ .
To measure the electrical efficiency, you must find the sum of $f(x, y, z) \times g(x, y, z)$ for all combinations of $(x, y, z)$ such that $1 \leq x < y < z \leq N$ . Because the answer can be very large, you just need to output the answer modulo $998\,244\,353$ .
Note: $\text{GCD}(k_1, k_2, k_3)$ is the greatest common divisor of $k_1$ , $k_2$ , and $k_3$ , which is the biggest integer that simultaneously divides $k_1$ , $k_2$ , and $k_3$ .
输入格式
The first line contains a single integer $N$ ( $1 \le N \le 2 \cdot 10^5$ ) — the number of factories in Dengkleknesia.
The second line contains $N$ integers $A_1, A_2, \dots, A_N$ ( $1 \leq A_i \leq 2 \cdot 10^5$ ) — the electrical coefficients of the factories.
The $j$ -th of the next $N-1$ lines contains two integers $U_j$ and $V_j$ ( $1 \le U_j, V_j \le N$ ) — a power line that connects cities $U_j$ and $V_j$ . The collection of factories forms a tree.
The second line contains $N$ integers $A_1, A_2, \dots, A_N$ ( $1 \leq A_i \leq 2 \cdot 10^5$ ) — the electrical coefficients of the factories.
The $j$ -th of the next $N-1$ lines contains two integers $U_j$ and $V_j$ ( $1 \le U_j, V_j \le N$ ) — a power line that connects cities $U_j$ and $V_j$ . The collection of factories forms a tree.
输出格式
An integer representing the sum of $f(x, y, z) \times g(x, y, z)$ for all combinations of $(x, y, z)$ such that $1 \leq x < y < z \leq N$ , modulo $998\,244\,353$
输入输出样例
输入 #1
3 1 2 3 1 2 2 3
输出 #1
0
输入 #2
4 6 14 6 6 1 2 2 3 2 4
输出 #2
12
In the first example, the only $(x, y, z)$ possible is $(1, 2, 3)$ . Because $\text{GCD}(A_1, A_2, A_3) = \text{GCD}(1, 2, 3) = 1$ has $0$ distinct prime factors, therefore $f(x, y, z) \times g(x, y, z) = 2 \times 0 = 0$ .
In the second example, all triples $(x, y, z)$ that satisfy the condition are as follows:
- $(1, 2, 3) \rightarrow f(1, 2, 3) \times g(1, 2, 3) = 2 \times 1 = 2$
- $(1, 2, 4) \rightarrow f(1, 2, 4) \times g(1, 2, 4) = 2 \times 1 = 2$
- $(1, 3, 4) \rightarrow f(1, 3, 4) \times g(1, 3, 4) = 3 \times 2 = 6$
- $(2, 3, 4) \rightarrow f(2, 3, 4) \times g(2, 3, 4) = 2 \times 1 = 2$
So the electrical efficiency is $2 + 2 + 6 + 2 = 12$ .
In the second example, all triples $(x, y, z)$ that satisfy the condition are as follows:
- $(1, 2, 3) \rightarrow f(1, 2, 3) \times g(1, 2, 3) = 2 \times 1 = 2$
- $(1, 2, 4) \rightarrow f(1, 2, 4) \times g(1, 2, 4) = 2 \times 1 = 2$
- $(1, 3, 4) \rightarrow f(1, 3, 4) \times g(1, 3, 4) = 3 \times 2 = 6$
- $(2, 3, 4) \rightarrow f(2, 3, 4) \times g(2, 3, 4) = 2 \times 1 = 2$
So the electrical efficiency is $2 + 2 + 6 + 2 = 12$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted