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

#GL0000358. Đếm GCD trên đường đi cây (GCD Counting)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đế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 nn vertices, where vertex vv has positive label ava_v. Define g(x,y)g(x,y) as the GCD of all labels on the unique simple path from xx to yy, including both endpoints; in particular, g(x,x)=axg(x,x)=a_x.

For each integer d≥1d\ge1, count the vertex pairs (x,y)(x,y) with 1≤x≤y≤n1\le x\le y\le n and g(x,y)=dg(x,y)=d. These are unordered pairs, including single-vertex paths. Print only GCD values with positive counts, in ascending order.

Input

  • First line: nn.
  • Second line: nn labels a1,…,ana_1,\ldots,a_n.
  • Next n−1n-1 lines: tree edges u,vu,v.

Output

For each occurring GCD, print d count on its own line, sorted by increasing dd. Counts require 64-bit integers.

Subtasks

  • Subtask 1 (20 points): 1<=n<=12;1<=ai<=2001 <= n <= 12; 1 <= a_i <= 200.
  • Subtask 2 (30 points): 1<=n<=2000;1<=ai<=20001 <= n <= 2000; 1 <= a_i <= 2000.
  • Subtask 3 (50 points): 1<=n<=200000;1<=ai<=2000001 <= n <= 200000; 1 <= a_i <= 200000.

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.