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:
- 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 subarrays with GCD equal to 1
- Sum of digits equal to a given number in PL/SQL
- 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
- Find a Number X whose sum with its digits is equal to N
- Minimum number of power terms with sum equal to n
- Smallest number greater than or equal to N divisible by K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.