Number of Quadruples with GCD equal to K
Given two integers N and K, the task is to find the count of quadruples of (i, j, k, l) such that 1 ≤ i < j < k < l ≤ N and gcd(i, j, k, l) = K.
Examples:
Input: N = 10, K = 2
Output: 5
Valid quadruples are (2, 4, 6, 8), (2, 4, 6, 10),
(2, 4, 8, 10), (2, 6, 8, 10) and (4, 6, 8, 10)
Input: N = 8, K = 1
Output: 69
Approach:
- If gcd of a sequence is K then when we divide all these numbers by K, the gcd of the left-over numbers will be 1.
- Now in order to fulfil this constraint of quadruples having maximum number N, if we find out the count of all quadruples having maximum number less than or equal to N / K and having gcd 1 then we can simply multiply all the quadruples with K to get the answer.
- To find quadruples count with gcd 1, we must use inclusion and exclusion principle. Take N / K = M.
- MC4 quadruples are possible total. (M/2)C4 quadruples have gcd which is a multiple of 2. (M/2 multiples of 2 are used). Similarly, (M/3)C4 quadruples have gcd which is a multiple of 3. But if we subtract both the quantities then, gcd which are multiple of 6 are subtracted twice so we must include (M/6)C4 to add them once.
- So iterate from 2 to M, and if a number has an odd number of distinct prime divisors (like 2, 3, 5, 11 ) then subtract count of quadruples with gcd multiple of that number, and if it has even number of distinct prime divisors (like 6, 10, 33) then add count of quadruples with gcd multiple of that number. (Number must not have a repetition of prime divisors like 4).
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to calculate NC4 int nCr( int n) { // Base case to calculate NC4 if (n < 4) return 0; int answer = n * (n - 1) * (n - 2) * (n - 3); answer /= 24; return answer; } // Function to return the count of required // quadruples using Inclusion Exclusion int countQuadruples( int N, int K) { // Effective N int M = N / K; int answer = nCr(M); // Iterate over 2 to M for ( int i = 2; i < M; i++) { int j = i; // Number of divisors of i till M int temp2 = M / i; // Count stores the number of prime // divisors occurring exactly once int count = 0; // To prevent repetition of prime divisors int check = 0; int temp = j; while (j % 2 == 0) { count++; j /= 2; if (count >= 2) break ; } if (count >= 2) { check = 1; } for ( int k = 3; k <= sqrt (temp); k += 2) { int cnt = 0; while (j % k == 0) { cnt++; j /= k; if (cnt >= 2) break ; } if (cnt >= 2) { check = 1; break ; } else if (cnt == 1) count++; } if (j > 2) { count++; } // If repetition of prime divisors present // ignore this number if (check) continue ; else { // If prime divisor count is odd // subtract it from answer else add if (count % 2 == 1) { answer -= nCr(temp2); } else { answer += nCr(temp2); } } } return answer; } // Driver code int main() { int N = 10, K = 2; cout << countQuadruples(N, K); return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to calculate NC4 static int nCr( int n) { // Base case to calculate NC4 if (n < 4 ) return 0 ; int answer = n * (n - 1 ) * (n - 2 ) * (n - 3 ); answer /= 24 ; return answer; } // Function to return the count of required // quadruples using Inclusion Exclusion static int countQuadruples( int N, int K) { // Effective N int M = N / K; int answer = nCr(M); // Iterate over 2 to M for ( int i = 2 ; i < M; i++) { int j = i; // Number of divisors of i till M int temp2 = M / i; // Count stores the number of prime // divisors occurring exactly once int count = 0 ; // To prevent repetition of prime divisors int check = 0 ; int temp = j; while (j % 2 == 0 ) { count++; j /= 2 ; if (count >= 2 ) break ; } if (count >= 2 ) { check = 1 ; } for ( int k = 3 ; k <= Math.sqrt(temp); k += 2 ) { int cnt = 0 ; while (j % k == 0 ) { cnt++; j /= k; if (cnt >= 2 ) break ; } if (cnt >= 2 ) { check = 1 ; break ; } else if (cnt == 1 ) count++; } if (j > 2 ) { count++; } // If repetition of prime divisors present // ignore this number if (check== 1 ) continue ; else { // If prime divisor count is odd // subtract it from answer else add if (count % 2 == 1 ) { answer -= nCr(temp2); } else { answer += nCr(temp2); } } } return answer; } // Driver code public static void main(String[] args) { int N = 10 , K = 2 ; System.out.println(countQuadruples(N, K)); } } // This code is contributed by Princi Singh |
Python3
# Python3 implementation of the approach from math import sqrt # Function to calculate NC4 def nCr(n) : # Base case to calculate NC4 if (n < 4 ) : return 0 ; answer = n * (n - 1 ) * (n - 2 ) * (n - 3 ); answer / / = 24 ; return answer; # Function to return the count of required # quadruples using Inclusion Exclusion def countQuadruples(N, K) : # Effective N M = N / / K; answer = nCr(M); # Iterate over 2 to M for i in range ( 2 , M) : j = i; # Number of divisors of i till M temp2 = M / / i; # Count stores the number of prime # divisors occurring exactly once count = 0 ; # To prevent repetition of prime divisors check = 0 ; temp = j; while (j % 2 = = 0 ) : count + = 1 ; j / / = 2 ; if (count > = 2 ) : break ; if (count > = 2 ) : check = 1 ; for k in range ( 3 , int (sqrt(temp)), 2 ) : cnt = 0 ; while (j % k = = 0 ) : cnt + = 1 ; j / / = k; if (cnt > = 2 ) : break ; if (cnt > = 2 ) : check = 1 ; break ; elif (cnt = = 1 ) : count + = 1 ; if (j > 2 ) : count + = 1 ; # If repetition of prime divisors present # ignore this number if (check) : continue ; else : # If prime divisor count is odd # subtract it from answer else add if (count % 2 = = 1 ) : answer - = nCr(temp2); else : answer + = nCr(temp2); return answer; # Driver code if __name__ = = "__main__" : N = 10 ; K = 2 ; print (countQuadruples(N, K)); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; class GFG { // Function to calculate NC4 static int nCr( int n) { // Base case to calculate NC4 if (n < 4) return 0; int answer = n * (n - 1) * (n - 2) * (n - 3); answer /= 24; return answer; } // Function to return the count of required // quadruples using Inclusion Exclusion static int countQuadruples( int N, int K) { // Effective N int M = N / K; int answer = nCr(M); // Iterate over 2 to M for ( int i = 2; i < M; i++) { int j = i; // Number of divisors of i till M int temp2 = M / i; // Count stores the number of prime // divisors occurring exactly once int count = 0; // To prevent repetition of prime divisors int check = 0; int temp = j; while (j % 2 == 0) { count++; j /= 2; if (count >= 2) break ; } if (count >= 2) { check = 1; } for ( int k = 3; k <= Math.Sqrt(temp); k += 2) { int cnt = 0; while (j % k == 0) { cnt++; j /= k; if (cnt >= 2) break ; } if (cnt >= 2) { check = 1; break ; } else if (cnt == 1) count++; } if (j > 2) { count++; } // If repetition of prime divisors present // ignore this number if (check==1) continue ; else { // If prime divisor count is odd // subtract it from answer else add if (count % 2 == 1) { answer -= nCr(temp2); } else { answer += nCr(temp2); } } } return answer; } // Driver code public static void Main(String[] args) { int N = 10, K = 2; Console.WriteLine(countQuadruples(N, K)); } } // This code is contributed by 29AjayKumar |
Javascript
<script> // Javascript implementation of the approach // Function to calculate NC4 function nCr(n) { // Base case to calculate NC4 if (n < 4) return 0; let answer = n * (n - 1) * (n - 2) * (n - 3); answer = parseInt(answer / 24); return answer; } // Function to return the count of required // quadruples using Inclusion Exclusion function countQuadruples(N, K) { // Effective N let M = parseInt(N / K); let answer = nCr(M); // Iterate over 2 to M for (let i = 2; i < M; i++) { let j = i; // Number of divisors of i till M let temp2 = parseInt(M / i); // Count stores the number of prime // divisors occurring exactly once let count = 0; // To prevent repetition of prime divisors let check = 0; let temp = j; while (j % 2 == 0) { count++; j = parseInt(j / 2); if (count >= 2) break ; } if (count >= 2) { check = 1; } for (let k = 3; k <= Math.sqrt(temp); k += 2) { let cnt = 0; while (j % k == 0) { cnt++; j = parseInt(j / k); if (cnt >= 2) break ; } if (cnt >= 2) { check = 1; break ; } else if (cnt == 1) count++; } if (j > 2) { count++; } // If repetition of prime divisors present // ignore this number if (check) continue ; else { // If prime divisor count is odd // subtract it from answer else add if (count % 2 == 1) { answer -= nCr(temp2); } else { answer += nCr(temp2); } } } return answer; } // Driver code let N = 10, K = 2; document.write(countQuadruples(N, K)); </script> |
Output:
5
Time Complexity: O(sqrt(n)*n)
Auxiliary Space: O(1)
Please Login to comment...