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[] = {1, 2, 2, 4, 5}, queries[] = {{2, 3}, {2, 4}, {3, 4}, {4, 4}, {4, 5}}
Sample Output: 3 3 3 5 1
Explanation: For 1st query [2, 3], the subarray is {2, 2}.
The pairs of coprimes that divide all the integers in the subarray are {1, 1}, {1, 2} and {2, 1}.
For 2nd query [2, 4], the subarray is {2, 2, 4}.
The pairs of coprimes that divide all the integers are {1, 1}, {1, 2} and {2, 1}.
Similarly, proceed for the further queries.

Solution (java):
// Java program for the above approach
import java.util.*;

class InfobayAI{

  static final int MAXN = 200001;
  static int [][]table = new int[1001][1001];

  // Function to build sparse table
  static void buildSparseTable(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 <= Math.log(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]);
  }
  static int __gcd(int a, int b)  
  {  
    return b == 0? a:__gcd(b, a % b);     
  }

  // Function to return the GCD of
  // all elements in range [L, R]
  static int find_gcd(int L, int R)
  {

    // Highest power of 2 that is not
    // more than count of elements
    int j = (int)Math.log(R - L + 1);

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

  // Smallest prime factors array
  static int []spf = new int[MAXN];

  // Function to build the smallest
  // prime factor array using Sieve
  static 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
  static 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
  static void solveQueries(int [] a, int n,
                           int [][] q)
  {

    // Loop to iterate over queries
    for (int i = 0; i < q.length; 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 j = 1; j * j <= gcd; j++) {

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

      // Print answer
      System.out.print(ans+ " ");
    }
  }

  // Function to perform precomputation
  static void preProcess(int [] a, int n)
  {
    build_spf();
    buildSparseTable(a, n);
  }

  // Driver Code
  public static void main(String[] args)
  {
    int[] arr = { 1, 2, 2, 4, 5 };
    int [][]queries = {
      { 2, 3 }, { 2, 4 }, { 3, 4 }, { 4, 4 }, { 4, 5 }
    };

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

  }
}