#P16222. [ECUSTPC 2025] 荷塘月色

    ID: 18237 远端评测题 3000ms 1024MiB 尝试: 0 已通过: 0 显示难度提高 上传者: 标签>并查集2025最大公约数 gcd组合数学容斥原理高校校赛

[ECUSTPC 2025] 荷塘月色

Problem Description

Maddy encountered many blooming lotus flowers, but there was also something eerie among them...
In this lotus pond, there is a tree with nn nodes, which is an undirected, acyclic, connected graph formed by n1n-1 edges.
Each node ii (1in1 \le i \le n) has a weight aia_i, and a kk-pair (x,y)(x, y) on a tree is defined as follows:

  • 1x<yn1 \le x < y \le n, and xx and yy are integers representing nodes on the tree.
  • Let the weights of the nodes on the path from node xx to node yy form a multiset SS. Note that axa_x and aya_y are also included on the path.
  • It is required that gcdS=minS=k\gcd S = \min S = k, i.e. the greatest common divisor of the elements in SS equals the minimum value in SS, and both are kk.

Maddy now randomly chooses two distinct nodes (x,y)(x, y) on the tree. If there exists a kk such that (x,y)(x, y) is a kk-pair, then she will get kk lanterns.
Please help Maddy compute the expected number of lanterns she gets. Output ansans, which is the value of the expectation qq taken modulo 998244353998244353. See the Hint for the specific output requirement.

Input Format

The first line contains an integer TT (1T1051 \le T \le 10^5), denoting the number of test cases.
For each test case, the first line contains an integer nn (2n1052 \le n \le 10^5), denoting the number of vertices in the graph.
Then follow n1n-1 lines, each containing two integers uu and vv (1u,vn,uv1 \le u, v \le n, u \ne v), indicating that there is an edge between uu and vv in the tree TT.
Then one line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ain1 \le a_i \le n), representing the weights of the nodes in the tree.
It is guaranteed that n3×105\sum n \le 3 \times 10^5 over all testdata, and that the graph in each test case forms a tree.

Output Format

For each test case, output one line with an integer ansans, representing the expected number of lanterns Maddy gets modulo 998244353998244353.

1
6
1 2
1 3
2 4
2 5
3 6
6 2 3 4 2 1
332748119

Hint

Explanation for Sample 1

The given tree has 6 nodes, with edges 12,13,24,25,36,1-2, 1-3, 2-4, 2-5, 3-6,

The weights are $a_1 = 6, a_2 = 2, a_3 = 3, a_4 = 4, a_5 = 2, a_6 = 1.$

We enumerate all (62)=15\binom{6}{2} = 15 pairs of nodes, and check whether gcd(S)\gcd(S) equals min(S)\min(S) for the multiset SS of weights on the path.
For example:

  • Pair (1,2)(1,2): the path is [6,2][6,2], min=2\min = 2, gcd=2\gcd = 2, so it belongs to k=2k = 2.
  • Pair (1,3)(1,3): the path is [6,3][6,3], min=3\min = 3, gcd=3\gcd = 3, so it belongs to k=3k = 3.
  • Pair (1,6)(1,6): the path is [6,3,1][6,3,1], min=1\min = 1, gcd=1\gcd = 1, so it belongs to k=1k = 1.
  • Pair (3,4)(3,4): the path is [3,6,2,4][3,6,2,4], min=2\min = 2, gcd=1\gcd = 1, which does not satisfy the condition.

The final counts are:
k=1k = 1: 5 pairs (1,6),(2,6),(3,6),(4,6),(5,6)(1,6), (2,6), (3,6), (4,6), (5,6),
k=2k = 2: 6 pairs (1,2),(1,4),(1,5),(2,4),(2,5),(4,5)(1,2), (1,4), (1,5), (2,4), (2,5), (4,5),
k=3k = 3: 1 pair (1,3)(1,3),
k=4,5,6k = 4,5,6: 0 pairs.

Thus the expected number of lanterns is

$$\frac{5 \times 1 + 6 \times 2 + 1 \times 3 + 0 \times 4 + 0 \times 5 + 0 \times 6}{15} = \frac{4}{3}.$$

Taking it modulo gives the answer 332748119332748119.

Hint

It can be proven that the answer of this problem is a rational number. Let it be pq\frac{p}{q} where pp and qq are coprime. The number you output, ansans, must satisfy 0ans<9982443530 \le ans < 998244353 and

qansp(mod998244353).q \cdot ans \equiv p \pmod{998244353}.

It can be proven that such an ansans must exist under the meaning of this problem.

Translated by ChatGPT 5