Open In App

Count of distinct tuples(P, Q, R, S) such that P/Q = R/S or P⋅Q = R⋅S

Given an integer N, the task is to find the total number of distinct tuples(P, Q, R, S) formed by four integers with 1 ? P, Q, R, S ? N such that P/Q = R/S or P?Q = R?S. Two tuples (P, Q, R, S) and (E, F, G, H) are considered to be different if at least one of the following conditions is satisfied:

 Examples:



Input: N = 3
Output: 15
?Explanation: Following tuples satisfy the given condition:
(1, 1, 1, 1), (1, 2, 2, 1), (1, 3, 3, 1), (2, 1, 1, 2), (2, 2, 2, 2), (2, 3, 3, 2), (3, 1, 1, 3), (3, 2, 2, 3), (3, 3, 3, 3), (1, 2, 1, 2), (2, 1, 2, 1), (1, 3, 1, 3), (3, 1, 3, 1), (2, 3, 2, 3), (3, 2, 3, 2). Hence there are total 15 number of distinct tuples.

Input: N = 55
Output: 13503



Approach: The idea to solve this problem is: 

  • The number of fractions P/Q for which P = Q  will be N. They are 1/1, 2/2, 3/3,… N/N. Therefore, the number of tuples (P, Q, R, S) where P = Q and P/Q = R/S will be the number of pairs we can form with these N fractions which is N?N.
    Let us solve the case for P?Q. Let this be x. Then, by symmetry, the number of tuples (P, Q, R, S)  for P ? Q will also be x. Therefore, our final answer will then be x+x?( number of tuples (P, Q, R, S) where P = Q and P/Q = R/S)
    = x + x ? (N?N)
    = 2x ? (N?N), Therefore, we could easily find the final answer if we have the answer for the case P ? Q.
  • The most crucial property of P/Q = R/S is that both P/Q and R/S are reduced into a unique irreducible fraction X/Y i.e, GCD(X, Y)=1.
  • Thus if we go through every irreducible fraction X/Y where X ? Y and calculate the number of tuples (P, Q, R, S) where  P/Q = R/S = X/Y, then we are done.
  • The number of fractions that are equal to X/Y is ?N/X?.They are X/Y, 2?X/2?Y, 3?X/3?Y,… ?N/X??X / ?N/X??Y . The number of pairs we can make from these fractions is ?N/X???N/X?.
  • Let us fix some X, and let tot be the number of possible Y where X ? Y and gcd(X,Y)=1. Clearly, tot=?(X) where ? is the Euler-totient function. We can precompute the values of ?(X) for all 1 ? X ? N.
  • The number of irreducible fractions X/Y where X ? Y is ?(X) and each of them contributes ?N/X???N/X? to the answer.
  • From this information we can calculate the answer for the case P?Q  as follows:
    Iterate over X from 1 to N and add ?(X)??N/X???N/X? to the answer.
  • By calculating the answer for P ? Q, we can easily find the total number of tuples (P, Q, R, S) for which P/Q = R/S as explained above.

Below is the implementation of the above approach:




// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
#define N 1000001
 
// Function to find total number of tuples
long long numOfTuples(int n)
{
    long long dp[N] = {};
    for (int i = 1; i < N; i++) {
        dp[i] = 2 * i - dp[i];
        for (int j = 2 * i; j < N; j += i) {
            dp[j] += dp[i];
        }
    }
 
    long long ans = 0;
    for (int i = 1; i < n + 1; i++) {
        long long k = n / i;
        ans += k * k * dp[i];
    }
    ans -= n * n;
    return ans;
}
 
// Driver Code
int main()
{
    int n = 3;
 
    // Function call
    cout << numOfTuples(n) << "\n";
    return 0;
}




// Java code to implement the approach
import java.io.*;
import java.util.*;
 
public class GFG {
 
  // Function to find total number of tuples
  public static long numOfTuples(int n)
  {
 
    long[] dp = new long[1000001];
    for (int i = 1; i < 1000001; i++) {
      dp[i] = 2 * i - dp[i];
      for (int j = 2 * i; j < 1000001; j += i) {
        dp[j] += dp[i];
      }
    }
 
    long ans = 0;
    for (int i = 1; i < n + 1; i++) {
      long k = n / i;
      ans += k * k * dp[i];
    }
    ans -= n * n;
    return ans;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int n = 3;
 
    // Function call
    System.out.println(numOfTuples(n));
  }
}
 
// This code is contributed by aarohirai2616.




# Python3 code to implement the approach
 
# Function to find total number of tuples
def numOfTuples(n) :
 
    N = 1000001
    dp = [0] * 1000001;
    for i in range(1, N) :
        dp[i] = 2 * i - dp[i];
        for j in range(2 * i, N, i) :
            dp[j] += dp[i];
 
    ans = 0;
    for i in range(1, n + 1) :
        k = n // i;
        ans += k * k * dp[i];
     
    ans -= n * n;
    return ans;
 
# Driver Code
if __name__ == "__main__" :
 
    n = 3;
 
    # Function call
    print(numOfTuples(n));
     
    # This code is contributed by AnkThon




// C# code to implement the approach
using System;
public class GFG {
 
  // Function to find total number of tuples
  public static long numOfTuples(int n)
  {
 
    long[] dp = new long[1000001];
    for (int i = 1; i < 1000001; i++) {
      dp[i] = 2 * i - dp[i];
      for (int j = 2 * i; j < 1000001; j += i) {
        dp[j] += dp[i];
      }
    }
 
    long ans = 0;
    for (int i = 1; i < n + 1; i++) {
      long k = n / i;
      ans += k * k * dp[i];
    }
    ans -= n * n;
    return ans;
  }
 
  // Driver code
  public static void Main(string[] args)
  {
    int n = 3;
 
    // Function call
    Console.WriteLine(numOfTuples(n));
  }
}
 
// This code is contributed by AnkThon




// js code to implement the approach
let N = 1000001;
 
// Function to find total number of tuples
function numOfTuples(n)
{
    let dp = new Array(N);
    for (let i = 0; i < N; ++i) dp[i] = 0;
    for (let i = 1; i < N; i++) {
        dp[i] = 2 * i - dp[i];
        for (let j = 2 * i; j < N; j += i) {
            dp[j] += dp[i];
        }
    }
 
    let ans = 0;
    for (let i = 1; i < n + 1; i++) {
        let k = Math.floor(n / i);
        ans += k * k * dp[i];
    }
    ans -= n * n;
    return ans;
}
 
// driver
    let n = 3;
     
    // Function call
    console.log(numOfTuples(n));
     
// This code is contributed by ksam24000.

Output
15

Time Complexity: O(N*logN)
Auxiliary Space: O(N)


Article Tags :