3336. Find the Number of Subsequences With Equal GCD #3347
|
Topics: You are given an integer array Your task is to find the number of pairs of non-empty subsequences1 (
Return the total number of such pairs. Since the answer may be very large, return it modulo Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
Example 6:
Example 7:
Example 8:
Example 9:
Example 10:
Constraints:
Hint:
Similar Questions: Footnotes |
Replies: 1 comment 2 replies
|
We present a dynamic programming solution to count pairs of disjoint non-empty subsequences with equal GCD. Our approach tracks the number of ways to form two subsequences with specific GCD values simultaneously, processing each array element sequentially while deciding whether to skip it or assign it to either subsequence. Approach
Let's implement this solution in PHP: 3336. Find the Number of Subsequences With Equal GCD <?php
/**
* @param Integer[] $nums
* @return Integer
*/
function subsequencePairCount(array $nums): int
{
$mod = 1000000007;
$maxVal = 200;
// dp[g1][g2] = number of ways
$dp = array_fill(0, $maxVal + 1, array_fill(0, $maxVal + 1, 0));
$dp[0][0] = 1; // empty subsequences
foreach ($nums as $x) {
$newDp = $dp; // skip current element
for ($g1 = 0; $g1 <= $maxVal; $g1++) {
for ($g2 = 0; $g2 <= $maxVal; $g2++) {
if ($dp[$g1][$g2] == 0) continue;
$val = $dp[$g1][$g2];
// Add x to first subsequence
$ng1 = $g1 == 0 ? $x : gcd($g1, $x);
$newDp[$ng1][$g2] = ($newDp[$ng1][$g2] + $val) % $mod;
// Add x to second subsequence
$ng2 = $g2 == 0 ? $x : gcd($g2, $x);
$newDp[$g1][$ng2] = ($newDp[$g1][$ng2] + $val) % $mod;
}
}
$dp = $newDp;
}
$result = 0;
for ($g = 1; $g <= $maxVal; $g++) {
$result = ($result + $dp[$g][$g]) % $mod;
}
return $result;
}
/**
* @param $a
* @param $b
* @return mixed
*/
function gcd($a, $b): mixed
{
while ($b != 0) {
$t = $b;
$b = $a % $b;
$a = $t;
}
return $a;
}
// Test cases
echo subsequencePairCount([1,2,3,4]) . "\n"; // Output: 10
echo subsequencePairCount([10,20,30]) . "\n"; // Output: 2
echo subsequencePairCount([1,1,1,1]) . "\n"; // Output: 50
echo subsequencePairCount([5]) . "\n"; // Output: 0
echo subsequencePairCount([2,2]) . "\n"; // Output: 2
echo subsequencePairCount([1,1]) . "\n"; // Output: 2
echo subsequencePairCount([100,200,150]) . "\n"; // Output: 0
echo subsequencePairCount([6,10,15]) . "\n"; // Output: 0
echo subsequencePairCount([2,4,8]) . "\n"; // Output: 0
echo subsequencePairCount([1]) . "\n"; // Output: 0
?>Explanation:
Complexity Analysis
|
We present a dynamic programming solution to count pairs of disjoint non-empty subsequences with equal GCD. Our approach tracks the number of ways to form two subsequences with specific GCD values simultaneously, processing each array element sequentially while deciding whether to skip it or assign it to either subsequence.
Approach
dp[g1][g2]to store the number of ways to build two subsequences where the first has GCDg1and the second has GCDg2dp[0][0] = 1representing empty subsequences for bothxinnums:xto first subsequence: update GCD fromg1togcd(g1, x)x