The Number of Good Subsets Problem
Description
LeetCode Problem 1994.
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 products6 = 2*3,6 = 2*3, and3 = 3respectively.[1, 4]and[4]are not good subsets with products4 = 2*2and4 = 2*2respectively.
Return the number of different good subsets in nums modulo 10^9 + 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.
Example 1:
Input: nums = [1,2,3,4]
Output: 6
Explanation: The good subsets are:
- [1,2]: product is 2, which is the product of distinct prime 2.
- [1,2,3]: product is 6, which is the product of distinct primes 2 and 3.
- [1,3]: product is 3, which is the product of distinct prime 3.
- [2]: product is 2, which is the product of distinct prime 2.
- [2,3]: product is 6, which is the product of distinct primes 2 and 3.
- [3]: product is 3, which is the product of distinct prime 3.
Example 2:
Input: nums = [4,2,3,15]
Output: 5
Explanation: The good subsets are:
- [2]: product is 2, which is the product of distinct prime 2.
- [2,3]: product is 6, which is the product of distinct primes 2 and 3.
- [2,15]: product is 30, which is the product of distinct primes 2, 3, and 5.
- [3]: product is 3, which is the product of distinct prime 3.
- [15]: product is 15, which is the product of distinct primes 3 and 5.
Constraints:
1 <= nums.length <= 10^51 <= nums[i] <= 30
Solution
Approach: Bitmask DP over Primes
A good subset has a product of distinct primes, so none of its numbers may be divisible by a square (4, 8, 9, 12, ...) and no two numbers may share a prime. Only 10 primes are at most 30, so the primes used by a subset fit in a 10-bit mask.
Let f[state] be the number of subsets (of the numbers handled so far, ignoring 1s) whose product uses exactly the primes in state; f[0] starts as 2^(number of 1s), because any number of 1s can be added to any subset. For each square-free x from 2 to 30 that occurs cnt[x] times, compute its prime mask and, going through the states from high to low, add cnt[x] * f[state ^ mask] to f[state] whenever state contains mask. The answer is the sum of f[state] over all non-empty states, modulo 10^9 + 7.
Complexity: time O(n + 30 * 2^10), space O(2^10).
from collections import Counter
class Solution:
def numberOfGoodSubsets(self, nums: List[int]) -> int:
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
cnt = Counter(nums)
mod = 10**9 + 7
n = len(primes)
f = [0] * (1 << n)
f[0] = pow(2, cnt[1], 10**9 + 7)
for x in range(2, 31):
if cnt[x] == 0 or x % 4 == 0 or x % 9 == 0 or x % 25 == 0:
continue
mask = 0
for i, p in enumerate(primes):
if x % p == 0:
mask |= 1 << i
for state in range((1 << n) - 1, 0, -1):
if state & mask == mask:
f[state] = (f[state] + cnt[x] * f[state ^ mask]) % mod
return sum(f[i] for i in range(1, 1 << n)) % mod