[LeetCode] The Number of Good Subsets

1994. The Number of Good Subsets

You are given an integer array nums. We call a subset of nums good if its product can be represented as a product of one or more distinct prime numbers.

  • For example, if nums = [1, 2, 3, 4]:
  • [2, 3], [1, 2, 3], and [1, 3] are good subsets with products 6 = 23, 6 = 23, and 3 = 3 respectively.
  • [1, 4] and [4] are not good subsets with products 4 = 22 and 4 = 22 respectively.

Return the number of different good subsets in nums modulo 109 + 7.

A subset of nums is any array that can be obtained by deleting some (possibly none or all) elements from nums. Two subsets are different if and only if the chosen indices to delete are different.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Solution {
unordered_map<int, unordered_map<int, long long>> dp;
int ones = 1;
int mod = 1e9 + 7;
unordered_map<int, int> table{{2, 0}, {3, 1}, {5, 2}, {7, 3}, {11, 4}, {13, 5}, {17, 6}, {19, 7}, {23, 8}, {29, 9}};
int convert(int n) {
int mask = 0;
for(int i = 2; i <= 30 and n > 1; i++) {
if(n % i == 0) {
mask |= 1 << table[i];
n /= i;
}
if(n % i == 0) return -1;
}
return mask;
}
vector<int> convert(vector<int>& nums) {
vector<int> res;
unordered_map<int, int> cache;
for(auto& n : nums) {
if(n == 1) {
ones++;
} else if(cache.count(n)) {
if(cache[n] != -1) res.push_back(cache[n]);
} else {
int mask = convert(n);
cache[n] = mask;
if(mask != -1) res.push_back(mask);
}
}
return res;
}
long long helper(int mask, int pos, vector<pair<int, int>>& A) {
if(pos == A.size()) return (mask > 0);
if(dp.count(pos) and dp[pos].count(mask)) return dp[pos][mask];
long long& res = dp[pos][mask] = helper(mask, pos + 1, A);
if(!(mask & A[pos].first)) {
res = (res + A[pos].second * helper(mask | A[pos].first, pos + 1, A)) % mod;
}
return res;
}
long long modpow(int x, int n) {
if(n == 0) return 1;
long long res = modpow(x, n>>1);
res = (res * res) % mod;
if(n & 1) res = (res * x) % mod;
return res;
}
public:
int numberOfGoodSubsets(vector<int>& nums) {
vector<int> A = convert(nums);
unordered_map<int, int> freq;
for(auto& a : A) freq[a]++;
vector<pair<int, int>> B;
for(auto& [m, c] : freq) B.push_back({m, c});

return modpow(2, ones - 1) * helper(0, 0, B) % mod;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/25/PS/LeetCode/the-number-of-good-subsets/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.