#GL0000340. Đường đi dài nhất có GCD lớn hơn 1 (GCD Counting)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đườ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 vertices, numbered from to . Vertex stores a positive integer .
For vertices , consider their unique simple path. Let 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 .
Find the longest path with , or print 0 if no such path exists.
Input
The first line contains . The second line contains values . Then lines describe edges .
Output
Print the greatest length measured in vertices, or 0 if no valid path exists.
Subtasks
-
Subtask 1 (20 points): , , các cạnh tạo thành cây.
-
Subtask 2 (30 points): , , các cạnh tạo thành cây.
-
Subtask 3 (50 points): , , 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.