<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <code>true</code> if it is possible to divide this array into <code>k</code> non-empty subsets whose sums are all equal.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [4,3,2,3,5,2,1], k = 4
<strong>Output:</strong> true
<strong>Explanation:</strong> It is possible to divide it into 4 subsets (5), (1, 4), (2,3), (2,3) with equal sums.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4], k = 3
<strong>Output:</strong> false
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= k &lt;= nums.length &lt;= 16</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
	<li>The frequency of each element is in the range <code>[1, 4]</code>.</li>
</ul>

class Solution:
    def canPartitionKSubsets(self, nums: List[int], k: int) -> bool:
        s = sum(nums)
        n = len(nums)
        if s % k:
            return False
        nums.sort(reverse=True)

        @cache
        def backtrack(start, piles, curr_sum, bitmask, target):
            if curr_sum == target:
                return backtrack(0, piles + 1, 0, bitmask, target)
            if piles == k:
                return True
            if start == n:
                return False

            last = -1
            for i in range(start, n):
                if bitmask & (1 << i) or curr_sum + nums[i] > target or nums[i] == last:
                    continue
                bitmask |= 1 << i
                if backtrack(i + 1, piles, curr_sum + nums[i], bitmask, target):
                    return True
                bitmask ^= 1 << i
                last = nums[i]
            return False

        return backtrack(0, 0, 0, 0, s // k)


class Solution:
    def canPartitionKSubsets(self, nums: List[int], k: int) -> bool:
        s = sum(nums)
        if s % k != 0:
            return False
        target = s // k
        nums.sort(reverse=True)
        if nums[0] > target:
            return False

        # Initial states
        n = len(nums)

        @cache
        def backtrack(index, k, current_sum, bitmask):
            if k == 1:  # If one subset remains, the rest must also be equal to target
                return True
            if current_sum == target:
                return backtrack(0, k - 1, 0, bitmask)  # Start new subset
            for i in range(index, n):
                if bitmask & (1 << i) == 0 and current_sum + nums[i] <= target:
                    bitmask |= (1 << i)
                    if backtrack(i + 1, k, current_sum + nums[i], bitmask):
                        return True
                    bitmask ^= (1 << i)
                    # Early exit: If this item was too big for an empty subset, it will be too big for others
                    if current_sum == 0:
                        break
            return False

        return backtrack(0, k, 0, 0)

18916