Find the Number of Subsequences With Equal GCD - Complete Solution Guide
Find the Number of Subsequences With Equal GCD is LeetCode problem 3336, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an integer array nums . Your task is to find the number of pairs of non-empty subsequences (seq1, seq2) of nums that satisfy the following conditions: The subsequences seq1 and seq2 are disjoint , meaning no index of nums is common between them. The GCD of the elements of seq1 is equal to the GCD of the elements of seq2 . Return the total number of such pairs. Since the answer may be very large, return it modulo 10 9 + 7 . Example 1: Input: nums = [1,2,3,4] Output: 10 Explanation:
Detailed Explanation
The problem asks us to find the number of pairs of non-empty, disjoint subsequences from a given array `nums` such that the greatest common divisor (GCD) of the elements in the first subsequence is equal to the GCD of the elements in the second subsequence. The subsequences must be disjoint, meaning they cannot share any indices from the original `nums` array. We need to return the count of these pairs modulo 10^9 + 7.
Solution Approach
The solution uses the Möbius inversion formula to calculate the number of disjoint subsequence pairs with equal GCDs. It iterates through all possible GCD values from 1 to 200. For each GCD `g`, it identifies elements in `nums` that are multiples of `g`. Then, it employs the Möbius function and inclusion-exclusion to count the number of such pairs. The Möbius function is precomputed to optimize the calculation. The disjoint subsequences requirement adds complexity, leading to the power of 3 and power of 2 calculations within the inclusion-exclusion part.
Step-by-Step Algorithm
- Step 1: Precompute the Möbius function (mu) for all values from 1 to MAX_VAL. This is done using the sieve method.
- Step 2: Iterate through each possible GCD value `g` from 1 to MAX_VAL.
- Step 3: For each `g`, create a map `T_freq` that stores the frequency of `j/g` for all elements `j` in `nums` that are multiples of `g`.
- Step 4: Create an array `count_multiples_t` such that `count_multiples_t[m]` stores the count of elements in `T_freq` which are multiples of `m`.
- Step 5: Calculate the contribution of the current GCD `g` to the final answer using inclusion-exclusion. Iterate through all possible divisors d1 and d2 of elements in T_freq and calculate C_T.
- Step 6: Accumulate the contributions from each GCD value `g` into `total_pairs`.
- Step 7: Return `total_pairs` modulo 10^9 + 7.
Key Insights
- Insight 1: Precompute the Möbius function (mu) to efficiently calculate inclusion-exclusion.
- Insight 2: Iterate through possible GCD values from 1 to MAX_VAL (200), and for each possible GCD 'g', consider only numbers in the input array that are multiples of 'g'.
- Insight 3: For a given GCD 'g', the count of subsequences with GCD 'g' can be determined using inclusion-exclusion principle and the Möbius function.
Complexity Analysis
Time Complexity: O(MAX_VAL^3)
Space Complexity: O(MAX_VAL)
Topics
This problem involves: Array, Math, Dynamic Programming, Number Theory.
Companies
Asked at: Infosys.