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

#GL0000340. Đường đi dài nhất có GCD lớn hơn 1 (GCD Counting)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đường đi dài nhất có GCD lớn hơn 1 (GCD Counting)

GCD Counting

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a tree with nn vertices, numbered from 11 to nn. Vertex ii stores a positive integer aia_i.

For vertices u,vu,v, consider their unique simple path. Let g(u,v)g(u,v) be the GCD of every vertex value on that path, including the endpoints. Path length means the number of vertices, so a path from a vertex to itself has length 11.

Find the longest path with g(u,v)>1g(u,v)>1, or print 0 if no such path exists.

Input

The first line contains nn. The second line contains nn values a1,…,ana_1,\dots,a_n. Then n−1n-1 lines describe edges u,vu,v.

Output

Print the greatest length measured in vertices, or 0 if no valid path exists.

Subtasks

  • Subtask 1 (20 points): 1≤n≤101\le n\le10, 1≤ai≤1001\le a_i\le100, các cạnh tạo thành cây.

  • Subtask 2 (30 points): 1≤n≤10001\le n\le1000, 1≤ai≤50001\le a_i\le5000, các cạnh tạo thành cây.

  • Subtask 3 (50 points): 1≤n≤2⋅1051\le n\le2\cdot10^5, 1≤ai≤2⋅1051\le a_i\le2\cdot10^5, các cạnh tạo thành cây.

Examples

Example 1

Input

3
2 3 4
1 2
2 3

Output

1

Explanation

Each adjacent pair has gcd 1, so only a single vertex with value greater than 1 qualifies.

Example 2

Input

3
2 3 4
1 3
2 3

Output

2

Explanation

The path from vertex 1 to 3 has values 2,4 and gcd 2; its length is two vertices.

Example 3

Input

3
1 1 1
1 2
2 3

Output

0

Explanation

Every path, including single-vertex paths, has gcd 1.