You are given a positive integer 0-indexed array nums.
A subset of the array nums is square-free if the product of its elements is a square-free integer.
A square-free integer is an integer that is divisible by no square number other than 1.
Return the number of square-free non-empty subsets of the arraynums.
Since the answer may be too large, return it modulo10^9 + 7.
A non-emptysubset of nums is an array that can be obtained by deleting some (possibly none but not all) elements from nums. Two subsets are different if and only if the chosen indices to delete are different.
Input: nums =[3,4,4,5]Output: 3Explanation: There are 3 square-free subsets inthis example:- The subset consisting of the 0th element [3]. The product of its elements is3, which is a square-free integer.- The subset consisting of the 3rd element [5]. The product of its elements is5, which is a square-free integer.- The subset consisting of 0th and 3rd elements [3,5]. The product of its elements is15, which is a square-free integer.It can be proven that there are no more than 3 square-free subsets in the given array.
Input: nums =[1]Output: 1Explanation: There is1 square-free subset inthis example:- The subset consisting of the 0th element [1]. The product of its elements is1, which is a square-free integer.It can be proven that there is no more than 1 square-free subset in the given array.
A square-free integer is not divisible by any perfect square > 1. For numbers up to 30, we can precompute which numbers are square-free and their prime factors. We use bitmask DP to count valid subsets, ensuring no two numbers in a subset share a prime factor (which would introduce a square factor).
classSolution {
funcountSquareFreeSubsets(nums: IntArray): Int {
val MOD = 1_000_000_007
val primes = intArrayOf(2,3,5,7,11,13,17,19,23,29)
val numMask = IntArray(31)
for (i in2..30) {
var x = i
var mask = 0var ok = truefor (j in primes.indices) {
var cnt = 0while (x % primes[j] ==0) {
x /= primes[j]
cnt++ }
if (cnt > 1) ok = falseif (cnt > 0) mask = mask or (1 shl j)
}
if (ok) numMask[i] = mask
}
val freq = IntArray(31)
for (x in nums) freq[x]++val dp = LongArray(1 shl 10)
dp[0] = 1Lfor (i in2..30) {
if (numMask[i] ==0|| freq[i] ==0) continuefor (mask in (1 shl 10) - 1 downTo 0) {
if ((mask and numMask[i]) ==0) {
dp[mask or numMask[i]] = (dp[mask or numMask[i]] + dp[mask] * freq[i]) % MOD
}
}
}
var ans = 0Lfor (mask in1 until (1 shl 10)) ans = (ans + dp[mask]) % MOD
val ones = freq[1]
var pow2 = 1L repeat(ones) { pow2 = pow2 * 2 % MOD }
ans = ans * pow2 % MOD
ans = (ans + pow2 - 1 + MOD) % MOD
return ans.toInt()
}
}
classSolution:
defcountSquareFreeSubsets(self, nums: list[int]) -> int:
MOD =10**9+7 primes = [2,3,5,7,11,13,17,19,23,29]
num_mask = [0] *31for i in range(2, 31):
x, mask, ok = i, 0, Truefor j, p in enumerate(primes):
cnt =0while x % p ==0:
x //= p
cnt +=1if cnt >1:
ok =Falseif cnt:
mask |=1<< j
if ok:
num_mask[i] = mask
freq = [0] *31for x in nums:
freq[x] +=1 dp = [1] + [0] * ((1<<10) -1)
for i in range(2, 31):
ifnot num_mask[i] ornot freq[i]:
continuefor mask in range((1<<10) -1, -1, -1):
if mask & num_mask[i] ==0:
dp[mask | num_mask[i]] = (dp[mask | num_mask[i]] + dp[mask] * freq[i]) % MOD
ans = sum(dp[1:]) % MOD
ones = freq[1]
pow2 = pow(2, ones, MOD)
ans = ans * pow2 % MOD
ans = (ans + pow2 -1+ MOD) % MOD
return ans