The greatest common divisor (gcd) of two or more integers (at least one of which is not zero) is the largest positive integer that divides the numbers without a remainder.
Now I will give you a simple problem about gcd again.
Given a sequence of N integers, A = {a1, a2, ..., aN }.
For every pair of < l, r >( 1 ≤ l ≤ r ≤ N ), defined a function F (l, r) = gcd(ai)( l ≤ i ≤ r ) that is the greatest common divisor of all the integers in the subsequence {al, al+1, ..., ar }
Obviously, There are N * (N + 1)/2 pair of < l, r >( 1 ≤ l ≤ r ≤ N ). We can get the rank of pair < l, r > through the following code.
1 pair<int,int> get_RANK(int l,int r)
2 {
3 map<int,int>mp;
4 int k1 = 1, k2 = 1;
5 for(int i = 1;i <= N;i++)
6 for(int j = i;j <= N;j++)
7 {
8 if(i == l && j == r)continue;
9 if(F(i,j) < F(l,r))
10 {
11 if(mp.find(F(i,j)) != mp.end())continue;
12 k1++;
13 mp[F(i,j)] = 1;
14 }
15 else if(F(i,j) == F(l,r))
16 {
17 if(i < l || (i == l && j < r))k2++;
18 }
19 }
20 return make_pair(k1,k2);
21 }
(If you don’t know C++, what a sad story! Sorry!)
There are Q queries, you need to answer the following two queries:
● SELECT k1 k2: ask for the pair < l, r > which is rank < k1, k2 >.If there is no such pair output -1.
● RANK l r: ask for the rank < k1, k2 > of the pair < l, r >