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++
#include <bits/stdc++.h>
using namespace std;
int nCr( int n)
{
if (n < 4)
return 0;
int answer = n * (n - 1) * (n - 2) * (n - 3);
answer /= 24;
return answer;
}
int countQuadruples( int N, int K)
{
int M = N / K;
int answer = nCr(M);
for ( int i = 2; i < M; i++) {
int j = i;
int temp2 = M / i;
int count = 0;
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 (check)
continue ;
else {
if (count % 2 == 1) {
answer -= nCr(temp2);
}
else {
answer += nCr(temp2);
}
}
}
return answer;
}
int main()
{
int N = 10, K = 2;
cout << countQuadruples(N, K);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int nCr( int n)
{
if (n < 4 )
return 0 ;
int answer = n * (n - 1 ) * (n - 2 ) * (n - 3 );
answer /= 24 ;
return answer;
}
static int countQuadruples( int N, int K)
{
int M = N / K;
int answer = nCr(M);
for ( int i = 2 ; i < M; i++)
{
int j = i;
int temp2 = M / i;
int count = 0 ;
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 (check== 1 )
continue ;
else
{
if (count % 2 == 1 )
{
answer -= nCr(temp2);
}
else
{
answer += nCr(temp2);
}
}
}
return answer;
}
public static void main(String[] args)
{
int N = 10 , K = 2 ;
System.out.println(countQuadruples(N, K));
}
}
|
Python3
from math import sqrt
def nCr(n) :
if (n < 4 ) :
return 0 ;
answer = n * (n - 1 ) * (n - 2 ) * (n - 3 );
answer / / = 24 ;
return answer;
def countQuadruples(N, K) :
M = N / / K;
answer = nCr(M);
for i in range ( 2 , M) :
j = i;
temp2 = M / / i;
count = 0 ;
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 (check) :
continue ;
else :
if (count % 2 = = 1 ) :
answer - = nCr(temp2);
else :
answer + = nCr(temp2);
return answer;
if __name__ = = "__main__" :
N = 10 ; K = 2 ;
print (countQuadruples(N, K));
|
C#
using System;
class GFG
{
static int nCr( int n)
{
if (n < 4)
return 0;
int answer = n * (n - 1) * (n - 2) * (n - 3);
answer /= 24;
return answer;
}
static int countQuadruples( int N, int K)
{
int M = N / K;
int answer = nCr(M);
for ( int i = 2; i < M; i++)
{
int j = i;
int temp2 = M / i;
int count = 0;
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 (check==1)
continue ;
else
{
if (count % 2 == 1)
{
answer -= nCr(temp2);
}
else
{
answer += nCr(temp2);
}
}
}
return answer;
}
public static void Main(String[] args)
{
int N = 10, K = 2;
Console.WriteLine(countQuadruples(N, K));
}
}
|
Javascript
<script>
function nCr(n)
{
if (n < 4)
return 0;
let answer = n * (n - 1) * (n - 2) * (n - 3);
answer = parseInt(answer / 24);
return answer;
}
function countQuadruples(N, K)
{
let M = parseInt(N / K);
let answer = nCr(M);
for (let i = 2; i < M; i++) {
let j = i;
let temp2 = parseInt(M / i);
let count = 0;
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 (check)
continue ;
else {
if (count % 2 == 1) {
answer -= nCr(temp2);
}
else {
answer += nCr(temp2);
}
}
}
return answer;
}
let N = 10, K = 2;
document.write(countQuadruples(N, K));
</script>
|
Time Complexity: O(sqrt(n)*n)
Auxiliary Space: O(1)
Last Updated :
31 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...