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
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.
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.
We can convert the memoization in Solution 1 into iterative dynamic programming.
Define \(f[j][k]\) as the number of ways where the current GCD of the first subsequence is \(j\) and that of the second subsequence is \(k\). Initially, \(f[0][0] = 1\).
Enumerate each element \(x\) in the array in order, and transfer using a new array \(g\):
Skip: \(g[j][k] \mathrel{+}= f[j][k]\);
Put into the first subsequence: \(g[\gcd(x, j)][k] \mathrel{+}= f[j][k]\);
Put into the second subsequence: \(g[j][\gcd(x, k)] \mathrel{+}= f[j][k]\).
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.