#P15279. [MCO 2025] GCD Equality

[MCO 2025] GCD Equality

Description

Evirir the dragon found nn positive integers a1,a2,,ana_1, a_2, \ldots, a_n. They want to answer qq queries of the form (l,r)(l, r) (1lrn1 \le l \le r \le n), which means:

  • Construct the array $b = [b_1, b_2, \ldots, b_{r-l+1}] = [a_l, a_{l+1}, \ldots, a_r]$. In one operation, Evirir can choose two adjacent integers in bb, say bib_i and bi+1b_{i+1} (1i<rl+11 \le i < r-l+1), and replace them with one integer gcd(bi,bi+1)\gcd(b_i, b_{i+1}). What is the minimum number of operations needed to make all elements in bb equal?

Can you help them answer the queries fast?

Note: gcd(x,y)\gcd(x, y) denotes the greatest common divisor (GCD) of integers xx and yy. For example, gcd(18,12)=6\gcd(18, 12) = 6, gcd(14,6)=2\gcd(14, 6) = 2, and gcd(9,14)=1\gcd(9, 14) = 1. See https://en.wikipedia.org/wiki/Greatest_common_divisor

Input Format

The first line contains two space-separated integers nn and qq (1n600001 \le n \le 60\,000, 1q1000001 \le q \le 100\,000).

The second line contains nn space-separated integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai600001 \le a_i \le 60\,000).

Each of the following qq lines contains two integers ll and rr (1lrn1 \le l \le r \le n), representing a query.

Output Format

For each query in order, output an integer (the answer) on a line.

10 6
36 24 120 24 36 60 48 24 24 9
1 7
2 4
6 10
6 8
8 9
10 10
4
1
4
2
0
0

Hint

Note

Example1\underline{Example 1}

For the first query (1 7\texttt{1 7}), b=[36,24,120,24,36,60,48]b = [36, 24, 120, 24, 36, 60, 48]. One optimal solution is (chosen elements are underlined\underline{underlined}, the new element from the last operation is bolded\textbf{bolded}):

  • [36,24,120,24,36,60,48][36, 24, \underline{120, 24}, 36, 60, 48]: gcd(120,24)=24\gcd(120, 24) = 24
  • [36,24,24,36,60,48][36, 24, \mathbf{24}, 36, \underline{60, 48}]: gcd(60,48)=12\gcd(60, 48) = 12
  • [36,24,24,36,12][\underline{36, 24}, 24, 36, \mathbf{12}]: gcd(36,24)=12\gcd(36, 24) = 12
  • [12,24,36,12][\mathbf{12}, \underline{24, 36}, 12]: gcd(24,36)=12\gcd(24, 36) = 12
  • [12,12,12][12, \mathbf{12}, 12]

It can be proven that one cannot make all elements equal in fewer than 4 operations.

For the second query (2 5\texttt{2 5}), b=[24,120,24]b = [24, 120, 24]. One optimal solution is [24,120,24][24,24][24, \underline{120, 24}] \to [24, \mathbf{24}].

For the third and fourth query, one can keep merging until one element is left.

For the fifth and sixth query, all elements of bb are already equal.

Scoring

Subtask 1 (88 points): n15n \le 15, q120q \le 120.

Subtask 2 (1010 points): n,q300n, q \le 300.

Subtask 3 (1313 points): n,q3000n, q \le 3000.

Subtask 4 (1717 points): For all 1in1 \le i \le n, ai2a_i \le 2.

Subtask 5 (99 points): For all 1in1 \le i \le n, ai=2ka_i = 2^k for some integer k0k \ge 0.

Subtask 6 (77 points): q=nq = n. For all 1iq1 \le i \le q, the ii-th query is (1,i)(1, i).

Subtask 7 (2626 points): For all 1in1 \le i \le n, ai36a_i \le 36.

Subtask 8 (1010 points): No additional constraints.