3336. Find the Number of Subsequences With Equal GCD
</> Solution
/**
* Problem: LeetCode 3336 - Find the Number of Subsequences With Equal GCD
* Difficulty: Hard
* Language: Java
* Author: CodeByTushu
*/
import java.util.*;
/**
* Problem: LeetCode 3336 - Find the Number of Subsequences With Equal GCD
* Difficulty: Hard
* Language: Java
* Author: CodeByTushu
*/
class Solution {
private static final int MOD = 1_000_000_007;
private static final int MAX = 200;
public int subsequencePairCount(int[] nums) {
long[][] dp = new long[MAX + 1][MAX + 1];
dp[0][0] = 1;
for (int num : nums) {
long[][] next = new long[MAX + 1][MAX + 1];
for (int g1 = 0; g1 <= MAX; g1++) {
for (int g2 = 0; g2 <= MAX; g2++) {
if (dp[g1][g2] == 0) continue;
// Ignore current number
next[g1][g2] = (next[g1][g2] + dp[g1][g2]) % MOD;
// Put in first subsequence
int ng1 = (g1 == 0) ? num : gcd(g1, num);
next[ng1][g2] = (next[ng1][g2] + dp[g1][g2]) % MOD;
// Put in second subsequence
int ng2 = (g2 == 0) ? num : gcd(g2, num);
next[g1][ng2] = (next[g1][ng2] + dp[g1][g2]) % MOD;
}
}
dp = next;
}
long ans = 0;
for (int g = 1; g <= MAX; g++) {
ans = (ans + dp[g][g]) % MOD;
}
return (int) ans;
}
private int gcd(int a, int b) {
while (b != 0) {
int t = a % b;
a = b;
b = t;
}
return a;
}
}
"""
Problem: LeetCode 3336 - Find the Number of Subsequences With Equal GCD
Difficulty: Hard
Language: Python
Author: CodeByTushu
"""
import math
class Solution:
def subsequencePairCount(self, nums: list[int]) -> int:
MOD = 1_000_000_007
max_val = max(nums)
# dp[g1][g2] stores the number of disjoint subsequence pairs
# with running GCD of the first subsequence g1 (0 if empty)
# and running GCD of the second subsequence g2 (0 if empty).
dp = [[0] * (max_val + 1) for _ in range(max_val + 1)]
dp[0][0] = 1
for num in nums:
next_dp = [row[:] for row in dp]
for g1 in range(max_val + 1):
for g2 in range(max_val + 1):
val = dp[g1][g2]
if val == 0:
continue
# Option 1: Place num into the first subsequence
ng1 = num if g1 == 0 else math.gcd(g1, num)
next_dp[ng1][g2] = (next_dp[ng1][g2] + val) % MOD
# Option 2: Place num into the second subsequence
ng2 = num if g2 == 0 else math.gcd(g2, num)
next_dp[g1][ng2] = (next_dp[g1][ng2] + val) % MOD
dp = next_dp
ans = 0
for g in range(1, max_val + 1):
ans = (ans + dp[g][g]) % MOD
return ans
/**
* Problem: LeetCode 3336 - Find the Number of Subsequences With Equal GCD
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;
/**
* Problem: LeetCode 3336 - Find the Number of Subsequences With Equal GCD
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
class Solution {
private:
static constexpr int MOD = 1'000'000'007;
public:
int subsequencePairCount(vector<int>& nums) {
int max_val = *max_element(nums.begin(), nums.end());
// dp[g1][g2] stores the count of pairs of disjoint subsequences
// with running GCD g1 for the first subsequence and g2 for the second (0 means empty).
vector<vector<int>> dp(max_val + 1, vector<int>(max_val + 1, 0));
dp[0][0] = 1;
for (int num : nums) {
vector<vector<int>> next_dp = dp;
for (int g1 = 0; g1 <= max_val; ++g1) {
for (int g2 = 0; g2 <= max_val; ++g2) {
int val = dp[g1][g2];
if (val == 0) continue;
// Option 1: Place num into the first subsequence
int ng1 = (g1 == 0) ? num : std::gcd(g1, num);
next_dp[ng1][g2] = (next_dp[ng1][g2] + val) % MOD;
// Option 2: Place num into the second subsequence
int ng2 = (g2 == 0) ? num : std::gcd(g2, num);
next_dp[g1][ng2] = (next_dp[g1][ng2] + val) % MOD;
}
}
dp = std::move(next_dp);
}
long long ans = 0;
for (int g = 1; g <= max_val; ++g) {
ans = (ans + dp[g][g]) % MOD;
}
return static_cast<int>(ans);
}
};
/**
* Problem: LeetCode 3336 - Find the Number of Subsequences With Equal GCD
* Difficulty: Hard
* Language: JavaScript
* Author: CodeByTushu
*/
function gcd(a, b) {
while (b !== 0) {
const t = a % b;
a = b;
b = t;
}
return a;
}
/**
* @param {number[]} nums
* @return {number}
*/
var subsequencePairCount = function(nums) {
const MOD = 1_000_000_007;
let maxVal = 0;
for (const x of nums) {
if (x > maxVal) maxVal = x;
}
// dp[g1][g2] stores the count of pairs of disjoint subsequences
// with running GCD g1 for the first subsequence and g2 for the second (0 means empty).
let dp = Array.from({ length: maxVal + 1 }, () => new Int32Array(maxVal + 1));
dp[0][0] = 1;
for (const num of nums) {
const next = Array.from({ length: maxVal + 1 }, (_, i) => new Int32Array(dp[i]));
for (let g1 = 0; g1 <= maxVal; g1++) {
for (let g2 = 0; g2 <= maxVal; g2++) {
const val = dp[g1][g2];
if (val === 0) continue;
// Option 1: Place num into the first subsequence
const ng1 = g1 === 0 ? num : gcd(g1, num);
next[ng1][g2] = (next[ng1][g2] + val) % MOD;
// Option 2: Place num into the second subsequence
const ng2 = g2 === 0 ? num : gcd(g2, num);
next[g1][ng2] = (next[g1][ng2] + val) % MOD;
}
}
dp = next;
}
let ans = 0;
for (let g = 1; g <= maxVal; g++) {
ans = (ans + dp[g][g]) % MOD;
}
return ans;
};