Description
Evirir the dragon found n positive integers a1,a2,…,an. They want to answer q queries of the form (l,r) (1≤l≤r≤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 b, say bi and bi+1 (1≤i<r−l+1), and replace them with one integer gcd(bi,bi+1). What is the minimum number of operations needed to make all elements in b equal?
Can you help them answer the queries fast?
Note: gcd(x,y) denotes the greatest common divisor (GCD) of integers x and y. For example, gcd(18,12)=6, gcd(14,6)=2, and gcd(9,14)=1. See https://en.wikipedia.org/wiki/Greatest_common_divisor
The first line contains two space-separated integers n and q (1≤n≤60000, 1≤q≤100000).
The second line contains n space-separated integers a1,a2,…,an (1≤ai≤60000).
Each of the following q lines contains two integers l and r (1≤l≤r≤n), representing a query.
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
For the first query (1 7), b=[36,24,120,24,36,60,48]. One optimal solution is (chosen elements are underlined, the new element from the last operation is bolded):
- [36,24,120,24,36,60,48]: gcd(120,24)=24
- [36,24,24,36,60,48]: gcd(60,48)=12
- [36,24,24,36,12]: gcd(36,24)=12
- [12,24,36,12]: gcd(24,36)=12
- [12,12,12]
It can be proven that one cannot make all elements equal in fewer than 4 operations.
For the second query (2 5), b=[24,120,24]. One optimal solution is [24,120,24]→[24,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 b are already equal.
Scoring
Subtask 1 (8 points): n≤15, q≤120.
Subtask 2 (10 points): n,q≤300.
Subtask 3 (13 points): n,q≤3000.
Subtask 4 (17 points): For all 1≤i≤n, ai≤2.
Subtask 5 (9 points): For all 1≤i≤n, ai=2k for some integer k≥0.
Subtask 6 (7 points): q=n. For all 1≤i≤q, the i-th query is (1,i).
Subtask 7 (26 points): For all 1≤i≤n, ai≤36.
Subtask 8 (10 points): No additional constraints.