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.
Input: N = 10, K = 2
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
- 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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Find the Number of Maximum Product Quadruples
- Number of quadruples where the first three terms are in AP and last three terms are in GP
- Count number of triplets with product equal to given number with duplicates allowed | Set-2
- Count number of triplets with product equal to given number with duplicates allowed
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Count number of subsets of a set with GCD equal to a given number
- Number of pairs such that their HCF and LCM is equal
- Sum of digits equal to a given number in PL/SQL
- Number of subarrays with GCD equal to 1
- Find a number x such that sum of x and its digits is equal to given n.
- Largest number less than or equal to N/2 which is coprime to N
- Highest power of 2 less than or equal to given number
- Distinct powers of a number N such that the sum is equal to K
- Find a Number X whose sum with its digits is equal to N
- Find all powers of 2 less than or equal to a given number
- Minimum number of Factorials whose sum is equal to N
- Count number of equal pairs in a string
- Smallest number k such that the product of digits of k is equal to n
- Count number of integers less than or equal to N which has exactly 9 divisors
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.