PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000257. Chi phí GCD trên cây (GCD cost on the tree)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Chi phí GCD trên cây (GCD cost on the tree)

GCD cost on the tree

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an undirected tree with NN vertices numbered from 11 to NN. Vertex ii has an associated positive integer AiA_i. Any pair of vertices is connected by exactly one simple path, which never visits a vertex twice.

For two distinct vertices s,ts,t, let p1=s,p2,…,pk=tp_1=s,p_2,\ldots,p_k=t denote the vertices on their simple path, in order. Here kk counts both endpoints. Define

$$C(s,t)=k\cdot\gcd(A_{p_1},A_{p_2},\ldots,A_{p_k}).$$

The greatest common divisor is taken over every vertex value on the path. Since the tree is undirected, C(s,t)=C(t,s)C(s,t)=C(t,s).

Find the total cost over all unordered pairs of distinct vertices, each counted once:

S=∑1≤s<t≤NC(s,t).S=\sum_{1\le s<t\le N} C(s,t).

Print SS modulo 998244353998244353.

Input

  • The first line contains the integer NN, the number of vertices.
  • The second line contains exactly NN positive integers A1,A2,…,ANA_1,A_2,\ldots,A_N, the values assigned to vertices 11 through NN.
  • Each of the next N−1N-1 lines contains integers Ui,ViU_i,V_i, denoting an undirected edge between these vertices. It is guaranteed that Ui<ViU_i<V_i and all edges form a tree.

Output

Print one integer: the remainder of SS upon division by 998244353998244353.

Subtasks

  • Subtask 1 (20 points): 2≤N≤122\le N\le12; 1≤Ai≤1051\le A_i\le10^5 for every 1≤i≤N1\le i\le N; 1≤Ui<Vi≤N1\le U_i<V_i\le N for every 1≤i<N1\le i<N; the edges form an undirected tree.
  • Subtask 2 (30 points): 2≤N≤20002\le N\le2000; 1≤Ai≤1051\le A_i\le10^5 for every 1≤i≤N1\le i\le N; 1≤Ui<Vi≤N1\le U_i<V_i\le N for every 1≤i<N1\le i<N; the edges form an undirected tree.
  • Subtask 3 (50 points): 2≤N≤1052\le N\le10^5; 1≤Ai≤1051\le A_i\le10^5 for every 1≤i≤N1\le i\le N; 1≤Ui<Vi≤N1\le U_i<V_i\le N for every 1≤i<N1\le i<N; the edges form an undirected tree.

Example 1

Input

4
24 30 28 7
1 2
1 3
3 4

Output

47

Explanation

The edges are (1,2)(1,2), (1,3)(1,3) and (3,4)(3,4). We enumerate all six unordered pairs:

  • (1,2)(1,2): two vertices and gcd⁡(24,30)=6\gcd(24,30)=6 give C(1,2)=12C(1,2)=12.
  • (1,3)(1,3): two vertices and gcd⁡(24,28)=4\gcd(24,28)=4 give C(1,3)=8C(1,3)=8.
  • (1,4)(1,4): the path 1→3→41\to3\to4 has three vertices; gcd⁡(24,28,7)=1\gcd(24,28,7)=1, giving 33.
  • (2,3)(2,3): the path 2→1→32\to1\to3 has three vertices; gcd⁡(30,24,28)=2\gcd(30,24,28)=2, giving 66.
  • (2,4)(2,4): the path 2→1→3→42\to1\to3\to4 has four vertices; gcd⁡(30,24,28,7)=1\gcd(30,24,28,7)=1, giving 44.
  • (3,4)(3,4): two vertices and gcd⁡(28,7)=7\gcd(28,7)=7 give C(3,4)=14C(3,4)=14.

Their sum is 12+8+3+6+4+14=4712+8+3+6+4+14=47, which is already smaller than 998244353998244353.

Example 2

Input

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

Output

1184

Explanation

There are 1010 vertices and 99 edges forming a tree, with associated values 180,168,120,144,192,200,198,160,156,150180,168,120,144,192,200,198,160,156,150.

For instance, the path between 11 and 33 is 1→2→31\to2\to3: k=3k=3 and gcd⁡(180,168,120)=12\gcd(180,168,120)=12, so its cost is 3636. The path between 77 and 1010 is 7→9→107\to9\to10: k=3k=3 and gcd⁡(198,156,150)=6\gcd(198,156,150)=6, so its cost is 1818. The edge path between 11 and 22 contributes 2gcd⁡(180,168)=242\gcd(180,168)=24.

Applying the definition to all (102)=45\binom{10}{2}=45 unordered pairs yields the total 11841184. Since 11841184 is below the modulus, the output is 1184.