#GL0000358. Đếm GCD trên đường đi cây (GCD Counting)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đếm GCD trên đường đi cây (GCD Counting)
GCD Counting
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a tree with vertices, where vertex has positive label . Define as the GCD of all labels on the unique simple path from to , including both endpoints; in particular, .
For each integer , count the vertex pairs with and . These are unordered pairs, including single-vertex paths. Print only GCD values with positive counts, in ascending order.
Input
- First line: .
- Second line: labels .
- Next lines: tree edges .
Output
For each occurring GCD, print d count on its own line, sorted by increasing . Counts require 64-bit integers.
Subtasks
- Subtask 1 (20 points): .
- Subtask 2 (30 points): .
- Subtask 3 (50 points): .
Examples
Example 1
Input
3
1 2 3
1 2
2 3
Output
1 4
2 1
3 1
Explanation
There are six pairs x<=y. The three singleton paths contribute gcd 1,2,3; the three longer paths all have gcd 1. Counts are 4,1,1.
Example 2
Input
4
9 16 144 6
1 3
2 3
4 3
Output
1 1
2 1
3 1
6 2
9 2
16 2
144 1
Explanation
All four singleton paths count. For example, gcd on path 1–3 is 9, on path 2–3 is 16, and on path 4–3 is 6.