Count of distinct tuples(P, Q, R, S) such that P/Q = R/S or P⋅Q = R⋅S
Last Updated :
21 Dec, 2022
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++
#include <bits/stdc++.h>
using namespace std;
#define N 1000001
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;
}
int main()
{
int n = 3;
cout << numOfTuples(n) << "\n" ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
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;
}
public static void main(String[] args)
{
int n = 3 ;
System.out.println(numOfTuples(n));
}
}
|
Python3
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;
if __name__ = = "__main__" :
n = 3 ;
print (numOfTuples(n));
|
C#
using System;
public class GFG {
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;
}
public static void Main( string [] args)
{
int n = 3;
Console.WriteLine(numOfTuples(n));
}
}
|
Javascript
let N = 1000001;
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;
}
let n = 3;
console.log(numOfTuples(n));
|
Time Complexity: O(N*logN)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...