Problem: Count of distinct coprime pairs product of which divides all elements in index [L, R] for Q queries
Given an array arr[] of N integers and Q queries of the form (l, r). The task is to find the number of distinct pairs of coprime integers for each query such that all integers in index range [l, r] are divisible by the product of the coprime integers.

Sample Input: arr[] = {20, 10, 15}, queries[] = {{2, 3}, {1, 3}, {1, 2}}
Sample Output: 3 3 9

Solution (cpp):
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;

#define MAXN 200001
int table[1001][1001];

// Function to build sparse table
void buildSparseTable(vector<int> arr, int n)
{
    // GCD of single element is
    // the element itself
    for (int i = 0; i < n; i++)
        table[i][0] = arr[i];

    // Build sparse table
    for (int j = 1; j <= log2(n); j++)
        for (int i = 0; i <= n - (1 << j);
             i++)
            table[i][j]
                = __gcd(table[i][j - 1],
                        table[i + (1 << (j - 1))][j - 1]);
}

// Function to return the GCD of
// all elements in range [L, R]
int find_gcd(int L, int R)
{
    // Highest power of 2 that is not
    // more than count of elements
    int j = (int)log2(R - L + 1);

    // Return GCD in range
    return __gcd(table[L][j],
                 table[R - (1 << j) + 1][j]);
}

// Smallest prime factors array
int spf[MAXN];

// Function to build the smallest
// prime factor array using Sieve
void build_spf()
{
    spf[1] = 1;
    for (int i = 2; i < MAXN; i++)
        spf[i] = i;
    for (int i = 4; i < MAXN; i += 2)
        spf[i] = 2;

    for (int i = 3; i * i < MAXN; i++) {
        if (spf[i] == i) {
            for (int j = i * i; j < MAXN;
                 j += i)
                if (spf[j] == j)
                    spf[j] = i;
        }
    }
}

// Function to find the count of
// distinct prime factors of x
int getFactorization(int x)
{
    // Stores the required count
    int ctr = 0;

    while (x != 1) {
        ctr++;

        // Stores smallest prime
        // factor of x
        int p = spf[x];

        while (x % p == 0)
            x = x / p;
    }
    // Return count
    return ctr;
}
// Function to count of coprime pairs such
// that the product of the pair divides
// all integers of subarray in given range
void solveQueries(vector<int> a, int n,
                  vector<vector<int> > q)
{
    // Loop to iterate over queries
    for (int i = 0; i < q.size(); i++) {
        int l = q[i][0];
        int r = q[i][1];
        l--;
        r--;

        // Stores gcd in the range
        int gcd = find_gcd(l, r);

        // Stores the required count
        int ans = 0;

        // Count the pairs of co-primes
        // integers in given format
        for (int i = 1; i * i <= gcd; i++) {

            // If i is a factor of gcd
            if (gcd % i == 0) {
                ans = ans + (1 << getFactorization(i));
                if (gcd / i != i)
                    ans += (1
                            << getFactorization(gcd / i));
            }
        }
        // Print answer
        cout << ans << " ";
    }
}

// Function to perform precomputation
void preProcess(vector<int> a, int n)
{
    build_spf();
    buildSparseTable(a, n);
}

// Driver Code
int main()
{
    vector<int> arr = { 1, 2, 2, 4, 5 };
    vector<vector<int> > queries = {
        { 2, 3 }, { 2, 4 }, { 3, 4 }, { 4, 4 }, { 4, 5 }
    };

    preProcess(arr, arr.size());
    solveQueries(arr, arr.size(), queries);

    return 0;
}