Your task is to find the number of pairs of non-emptysubsequences(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 modulo109 + 7.
Example 1:
Input:nums = [1,2,3,4]
Output:10
Explanation:
The subsequence pairs which have the GCD of their elements equal to 1 are:
([1, 2, 3, 4], [1, 2, 3, 4])
([1, 2, 3, 4], [1, 2, 3, 4])
([1, 2, 3, 4], [1, 2, 3, 4])
([1, 2, 3, 4], [1, 2, 3, 4])
([1, 2, 3, 4], [1, 2, 3, 4])
([1, 2, 3, 4], [1, 2, 3, 4])
([1, 2, 3, 4], [1, 2, 3, 4])
([1, 2, 3, 4], [1, 2, 3, 4])
([1, 2, 3, 4], [1, 2, 3, 4])
([1, 2, 3, 4], [1, 2, 3, 4])
Example 2:
Input:nums = [10,20,30]
Output:2
Explanation:
The subsequence pairs which have the GCD of their elements equal to 10 are:
([10, 20, 30], [10, 20, 30])
([10, 20, 30], [10, 20, 30])
Example 3:
Input:nums = [1,1,1,1]
Output:50
Constraints:
1 <= nums.length <= 200
1 <= nums[i] <= 200
Solutions
Solution 1: Memoization
Thinking
We count pairs of disjoint subsequences with equal GCD. With \(n,M \le 200\), memoizing on index and the two running GCDs has about \(n M^2\) states.
Each value is skipped, folded into the first GCD, or folded into the second; an empty subsequence has GCD \(0\).
A finished state is valid when the two GCDs are equal. The initial call includes two empty subsequences, so we subtract \(1\) before reducing modulo \(10^9+7\).
We define a function \(\textit{dfs}(i, j, k)\) as the number of ways when considering elements with indices \(0 \sim i\), where the current GCD of the first subsequence is \(j\) and that of the second subsequence is \(k\). By convention, the GCD of an empty subsequence is \(0\), and \(\gcd(x, 0) = x\).
For the element at index \(i\), there are three choices:
Skip it: transition to \(\textit{dfs}(i - 1, j, k)\);
Put it into the first subsequence: transition to \(\textit{dfs}(i - 1, \gcd(\textit{nums}[i], j), k)\);
Put it into the second subsequence: transition to \(\textit{dfs}(i - 1, j, \gcd(\textit{nums}[i], k))\).
Base case: when \(i \lt 0\), return \(1\) if \(j = k\), otherwise return \(0\).
The initial call \(\textit{dfs}(n - 1, 0, 0)\) counts all pairs with equal GCDs, including the case where both subsequences are empty. Therefore, we subtract \(1\) and take the result modulo \(10^9 + 7\).
The time complexity is \(O(n \times m^2 \times \log m)\), and the space complexity is \(O(n \times m^2)\), where \(n\) is the length of the array and \(m\) is the maximum value in the array.
The memoized recursion pays for call depth and a three-dimensional cache. The same transitions fit a rolling table: a new \(g\) receives the three choices for \(x\).
Space drops from \(O(n M^2)\) to \(O(M^2)\) and there is no recursion. The answer is still the diagonal sum minus one.
Skipping zero cells in \(f\) further reduces the practical constant.
We can convert the memoization in Solution 1 into an iterative DP.
Define \(f[j][k]\) as the number of ways after processing the current elements such that the GCD of the first subsequence is \(j\) and that of the second is \(k\). Initially, \(f[0][0] = 1\).
For each element \(x\), transfer into a new array \(g\):
These correspond to skipping \(x\), putting \(x\) into the first subsequence, and putting \(x\) into the second subsequence, respectively. After processing all elements, the answer is \(\sum_{i=0}^{m} f[i][i] - 1\), taken modulo \(10^9 + 7\).
The time complexity is \(O(n \times m^2 \times \log m)\), and the space complexity is \(O(m^2)\), where \(n\) is the length of the array and \(m\) is the maximum value in the array.