33OJ
登录 注册

#P17897. [ICPC 2026 Nanchang I] Journey of GCD

    ID: 20633 远端评测题 3000ms 1024MiB 尝试: 0 已通过: 0 显示难度提高 上传者: 标签>数学图论贪心倍增并查集Kruskal 重构树江西生成树最近公共祖先 LCA树链剖分最大公约数 gcdICPC2026省赛/邀请赛

[ICPC 2026 Nanchang I] Journey of GCD

Problem Description

In the magical kingdom of Numerian, there are nn cities, each with its own magical power level aia_i. The king has built bidirectional roads between every pair of cities. For the road connecting city uu and city vv, its Guardian Charm Degree (GCD) equals gcd⁡(au,av)\gcd(a_u,a_v), which is the greatest common divisor of their magical power levels.

For a traveler moving from city xx to yy, the Magical Immunity Number (MIN) of the journey is the minimum GCD among all roads taken. The royal advisors consider a journey “well-immune” if and only if its MIN is maximized.

You are tasked with answering qq queries from the king. For each query (x,y)(x,y), output the maximum possible MIN among all paths from city xx to city yy.

In particular, if x=yx = y, the traveler never leaves the city, so the MIN is simply the city's own magical power axa_x.

Input Format

The first line contains two integers nn and qq (1≤n,q≤1061 \le n, q \le 10^6).

The second line contains nn integers a1,a2,⋯ ,ana_1, a_2, \cdots, a_n (1≤ai≤1061 \le a_i \le 10^6), representing the magical power of each city.

Each of the next qq lines contains two integers xx and yy (1≤x,y≤n1 \le x, y \le n), representing a query.

Output Format

For each query, output a single integer representing the maximum possible MIN.

6 3
6 15 22 33 35 63
5 6
3 5
2 2
7
3
15

Hint

For the sample, the magical power levels are a=[6,15,22,33,35,63]a=[6,15,22,33,35,63].

Query 1: The optimal path is 5→65 \to 6, and the answer is gcd⁡(35,63)=7\gcd(35,63)=7.

Query 2: The optimal path is 3→4→6→53 \to 4 \to 6 \to 5. The edge values are gcd⁡(22,33)=11\gcd(22,33)=11, gcd⁡(33,63)=3\gcd(33,63)=3, and gcd⁡(63,35)=7\gcd(63,35)=7, so the answer is min⁡(11,3,7)=3\min(11,3,7)=3.

Query 3: Since x=yx=y, the traveler never leaves the city, so the answer is a2=15a_2=15.