#P17897. [ICPC 2026 Nanchang I] Journey of GCD
[ICPC 2026 Nanchang I] Journey of GCD
Problem Description
In the magical kingdom of Numerian, there are cities, each with its own magical power level . The king has built bidirectional roads between every pair of cities. For the road connecting city and city , its Guardian Charm Degree (GCD) equals , which is the greatest common divisor of their magical power levels.
For a traveler moving from city to , 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 queries from the king. For each query , output the maximum possible MIN among all paths from city to city .
In particular, if , the traveler never leaves the city, so the MIN is simply the city's own magical power .
Input Format
The first line contains two integers and ().
The second line contains integers (), representing the magical power of each city.
Each of the next lines contains two integers and (), 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 .
Query 1: The optimal path is , and the answer is .
Query 2: The optimal path is . The edge values are , , and , so the answer is .
Query 3: Since , the traveler never leaves the city, so the answer is .