<p>Given <code>2n</code> balls of <code>k</code> distinct colors. You will be given an integer array <code>balls</code> of size <code>k</code> where <code>balls[i]</code> is the number of balls of color <code>i</code>.</p>

<p>All the balls will be <strong>shuffled uniformly at random</strong>, then we will distribute the first <code>n</code> balls to the first box and the remaining <code>n</code> balls to the other box (Please read the explanation of the second example carefully).</p>

<p>Please note that the two boxes are considered different. For example, if we have two balls of colors <code>a</code> and <code>b</code>, and two boxes <code>[]</code> and <code>()</code>, then the distribution <code>[a] (b)</code> is considered different than the distribution <code>[b] (a) </code>(Please read the explanation of the first example carefully).</p>

<p>Return<em> the probability</em> that the two boxes have the same number of distinct balls. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted as correct.</p>

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

<pre>
<strong>Input:</strong> balls = [1,1]
<strong>Output:</strong> 1.00000
<strong>Explanation:</strong> Only 2 ways to divide the balls equally:
- A ball of color 1 to box 1 and a ball of color 2 to box 2
- A ball of color 2 to box 1 and a ball of color 1 to box 2
In both ways, the number of distinct colors in each box is equal. The probability is 2/2 = 1
</pre>

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

<pre>
<strong>Input:</strong> balls = [2,1,1]
<strong>Output:</strong> 0.66667
<strong>Explanation:</strong> We have the set of balls [1, 1, 2, 3]
This set of balls will be shuffled randomly and we may have one of the 12 distinct shuffles with equal probability (i.e. 1/12):
[1,1 / 2,3], [1,1 / 3,2], [1,2 / 1,3], [1,2 / 3,1], [1,3 / 1,2], [1,3 / 2,1], [2,1 / 1,3], [2,1 / 3,1], [2,3 / 1,1], [3,1 / 1,2], [3,1 / 2,1], [3,2 / 1,1]
After that, we add the first two balls to the first box and the second two balls to the second box.
We can see that 8 of these 12 possible random distributions have the same number of distinct colors of balls in each box.
Probability is 8/12 = 0.66667
</pre>

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

<pre>
<strong>Input:</strong> balls = [1,2,1,2]
<strong>Output:</strong> 0.60000
<strong>Explanation:</strong> The set of balls is [1, 2, 2, 3, 4, 4]. It is hard to display all the 180 possible random shuffles of this set but it is easy to check that 108 of them will have the same number of distinct colors in each box.
Probability = 108 / 180 = 0.6
</pre>

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

<ul>
	<li><code>1 &lt;= balls.length &lt;= 8</code></li>
	<li><code>1 &lt;= balls[i] &lt;= 6</code></li>
	<li><code>sum(balls)</code> is even.</li>
</ul>

class Solution:
    def getProbabilityOLD(self, balls: List[int]) -> float:
        k = len(balls)
        two_n = sum(balls)
        n = two_n // 2
        balls = tuple(balls)
        scratch = dict()
        return self.prob(balls, n, k, scratch)
    def get_two_removed_tuples(self, balls, k):
        tuples_and_counts = []
        for i in range(k):
            for j in range(i, k):
                if i != j:
                    this_tuple = tuple(balls[index] - (1 if index == i or index == j else 0) for index in range(k))
                    tuples.append(this_tuple)
                elif balls[i] > 1:
                    this_tuple = tuple(balls[index] - (2 if index == i else 0) for index in range(k))
                    tuples.append(this_tuple)
        #What are the ways to choose these?
        return tuples
    def probBad(self, balls : Tuple[int], n, k, scratch):
        two_removed = self.get_two_removed_tuples(balls, n)
        if len(two_removed) == 0: pass
        print(two_removed)
        #TODO! We also need to know the NUMBER OF COMBINATIONS for each two removed!
        return 0
    def prob(self, balls : Tuple[int], n, k, scratch):
        sums = list(accumulate(balls))
        print("SUMS")
        print(sums)
        print("====")
        box_one_counts = self.get_box_one_counts(balls, sums, n, 0, k - 1)
        for thing in box_one_counts:
            print(thing)
            print(f"N: {sum(thing)}")
        return 0
    def get_box_one_counts(self, balls, sums, choices_remaining, start, end):
        counts = []
        max_from_first_column = min(choices_remaining, balls[start])#Don't use up more choices than remaining
        #If we use up at least MIN, there we will be choices_remaining - MIN after. This should equal sums[end] - sums[start]/
        min_from_first_columns = max(choices_remaining + sums[start] - sums[end], 0)
        next_start = start + 1
        for num in range(min_from_first_columns, max_from_first_column + 1):
            if next_start > end: counts.append([num])
            else:
                sub_counts = self.get_box_one_counts(balls, sums, choices_remaining - num, next_start, end)
                for after_counts in sub_counts: counts.append([num] + after_counts)
        return counts
    def get_count_probability(self, balls, counts, n, k):
        total_combinations = reduce(operator.mul, (math.comb(balls[i], counts[i]) for i in range(k)))
        return total_combinations / math.comb(2 * n, n)
    def has_same_number_distinct_balls(self, balls, counts, n, k):
        box_two_counts = [balls[i] - counts[i] for i in range(k)]
        def get_num_distinct_balls(c): return sum((bool(indiv_count) for indiv_count in c))
        one_distinct, two_distinct = get_num_distinct_balls(counts), get_num_distinct_balls(box_two_counts)
        has_same = one_distinct == two_distinct
        #counter_one = Counter(counts); counter_two = Counter(box_two_counts); has_same = counter_one == counter_two
        #print(f"{counts} and {box_two_counts} : {has_same}")
        return has_same
    def getProbability(self, balls: List[int]) -> float:
        k = len(balls)
        two_n = sum(balls)
        n = two_n // 2
        sums = list(accumulate(balls))
        all_box_one_counts = self.get_box_one_counts(balls, sums, n, 0, k - 1)
        probability = 0
        for box_one_counts in all_box_one_counts:
            if self.has_same_number_distinct_balls(balls, box_one_counts, n, k):
                probability += self.get_count_probability(balls, box_one_counts, n, k)
        return probability
79100