#abc248g. [abc248_g]GCD cost on the tree

[abc248_g]GCD cost on the tree

题目描述

给定一个具有 NN 个顶点的无向树。
将顶点标记为 Vertex 11,Vertex 22ldots\\ldots,Vertex NN。对于每个 1leqileqN11\\leq i\\leq N-1,第 ii 条边连接了 Vertex UiU_i 和 Vertex ViV_i
此外,每个顶点都被赋予一个正整数:Vertex ii 被赋值为 AiA_i

两个不同的顶点 sstt 之间的代价 C(s,t)C(s,t) 定义如下。

假设 p1(=s)p_1(=s)p2p_2ldots\\ldotspk(=t)p_k(=t) 是连接 Vertex ss 和 Vertex tt 的简单路径上的顶点,其中 kk 是该路径中顶点的数量(包括端点)。
那么,令 $C(s,t)=k\\times \\gcd (A_{p_1},A_{p_2},\\ldots,A_{p_k})$,
其中 gcd(X1,X2,ldots,Xk)\\gcd (X_1,X_2,\\ldots, X_k) 表示 X1,X2,ldots,XkX_1,X_2,\\ldots, X_k 的最大公约数。

计算 $\\displaystyle\\sum_{i=1}^{N-1}\\sum_{j=i+1}^N C(i,j)$ 对 998244353998244353 取模后的结果。

约束条件

  • 2N1052 \leq N \leq 10^5
  • 1Ai1051 \leq A_i \leq 10^5
  • 1Ui<ViN1 \leq U_i < V_i \leq N
  • 输入中的所有值都是整数。
  • 给定的图是一棵树。

输入

输入数据从标准输入获得,格式如下:

NN
A1A_1 A2A_2 cdots\\cdots ANA_N
U1U_1 V1V_1
U2U_2 V2V_2
vdots\\vdots
UN1U_{N-1} VN1V_{N-1}

输出

打印 $\\displaystyle\\sum_{i=1}^{N-1}\\sum_{j=i+1}^N C(i,j)$ 对 998244353998244353 取模后的结果。


示例输入 1

4
24 30 28 7
1 2
1 3
3 4

示例输出 1

47

直接连接顶点 1122、顶点 1133,以及顶点 3344。因此,代价计算如下。

  • C(1,2)=2timesgcd(24,30)=12C(1,2)=2\\times \\gcd(24,30)=12
  • C(1,3)=2timesgcd(24,28)=8C(1,3)=2\\times \\gcd(24,28)=8
  • C(1,4)=3timesgcd(24,28,7)=3C(1,4)=3\\times \\gcd(24,28,7)=3
  • C(2,3)=3timesgcd(30,24,28)=6C(2,3)=3\\times \\gcd(30,24,28)=6
  • C(2,4)=4timesgcd(30,24,28,7)=4C(2,4)=4\\times \\gcd(30,24,28,7)=4
  • C(3,4)=2timesgcd(28,7)=14C(3,4)=2\\times \\gcd(28,7)=14

因此,所求值为 $\\displaystyle\\sum_{i=1}^{3}\\sum_{j=i+1}^4 C(i,j)=(12+8+3)+(6+4)+14=47$ 对 998244353998244353 取模后等于 4747


示例输入 2

10
180 168 120 144 192 200 198 160 156 150
1 2
2 3
2 4
2 5
5 6
4 7
7 8
7 9
9 10

示例输出 2

1184