<p>There are two types of soup: <strong>type A</strong> and <strong>type B</strong>. Initially, we have <code>n</code> ml of each type of soup. There are four kinds of operations:</p>

<ol>
	<li>Serve <code>100</code> ml of <strong>soup A</strong> and <code>0</code> ml of <strong>soup B</strong>,</li>
	<li>Serve <code>75</code> ml of <strong>soup A</strong> and <code>25</code> ml of <strong>soup B</strong>,</li>
	<li>Serve <code>50</code> ml of <strong>soup A</strong> and <code>50</code> ml of <strong>soup B</strong>, and</li>
	<li>Serve <code>25</code> ml of <strong>soup A</strong> and <code>75</code> ml of <strong>soup B</strong>.</li>
</ol>

<p>When we serve some soup, we give it to someone, and we no longer have it. Each turn, we will choose from the four operations with an equal probability <code>0.25</code>. If the remaining volume of soup is not enough to complete the operation, we will serve as much as possible. We stop once we no longer have some quantity of both types of soup.</p>

<p><strong>Note</strong> that we do not have an operation where all <code>100</code> ml&#39;s of <strong>soup B</strong> are used first.</p>

<p>Return <em>the probability that <strong>soup A</strong> will be empty first, plus half the probability that <strong>A</strong> and <strong>B</strong> become empty at the same time</em>. Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.</p>

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

<pre>
<strong>Input:</strong> n = 50
<strong>Output:</strong> 0.62500
<strong>Explanation:</strong> If we choose the first two operations, A will become empty first.
For the third operation, A and B will become empty at the same time.
For the fourth operation, B will become empty first.
So the total probability of A becoming empty first plus half the probability that A and B become empty at the same time, is 0.25 * (1 + 1 + 0.5 + 0) = 0.625.
</pre>

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

<pre>
<strong>Input:</strong> n = 100
<strong>Output:</strong> 0.71875
</pre>

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

<ul>
	<li><code>0 &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:

    def soupServings(self, n: int) -> float:
        m = ceil(n / 25)
        dp = collections.defaultdict(dict)

        def calculate_dp(i: int, j: int) -> float:
            if i <= 0 and j <= 0:
                return 0.5
            if i <= 0:
                return 1.0
            if j <= 0:
                return 0.0
            if i in dp and j in dp[i]:
                return dp[i][j]

            dp[i][j] = (
                calculate_dp(i - 4, j)
                + calculate_dp(i - 3, j - 1)
                + calculate_dp(i - 2, j - 2)
                + calculate_dp(i - 1, j - 3)
            ) / 4.0
            return dp[i][j]

        for k in range(1, m + 1):
            if calculate_dp(k, k) > 1 - 1e-5:
                return 1.0
        return calculate_dp(m, m)
    def soupServingsd(self, n: int) -> float:
        m=ceil(n/5)
        dp={}

        def calculate(i,j):

            if i<=0 and j<=0:
                return 0.5
            if i<=0:
                return 1.0
            if j<=0:
                return 0.0
            
            if (i,j) in dp:
                return dp[(i,j)]

            dp[(i,j)]=0.25*(calculate(i-4,j)+\
            calculate(i-3,j-1)+calculate(i-2,j-2)+calculate(i-1,j-3))

            return dp[(i,j)]

        for i in range(1,m+1):
            if calculate(i,i)>1-1e-5:
                return 1.0

        return calculate(m,m)

    def soupServingssd(self, n: int) -> float:
        m = ceil(n / 25)
        dp = {}

        def calculate_dp(i, j):
            return (dp[max(0, i - 4)][j] + dp[max(0, i - 3)][j - 1] +
                    dp[max(0, i - 2)][max(0, j - 2)]
                    + dp[i - 1][max(0, j - 3)]) / 4

        dp[0] = {0: 0.5}
        for k in range(1, m + 1):
            dp[0][k] = 1
            dp[k] = {0: 0}
            for j in range(1, k + 1):
                dp[j][k] = calculate_dp(j, k)
                dp[k][j] = calculate_dp(k, j)
            if dp[k][k] > 1 - 1e-5:
                return 1
        return dp[m][m]

    def soupServings_09(self, n: int) -> float:

        def dfs(node_a,node_b,stage):
            #nonlocal sum_v

            if node_a==0 and node_b==0:
                list_v.append(0.5*(0.25)**stage)
                return
            elif node_a==0:
                list_v.append(0.25**stage)
                return
            elif node_b==0:
                return 

            #if (node_a,node_b) in dict_v:
            #    return dict_v[(node_a,node_b)]

            for dx,dy in [(-100,0),(-75,-25),(-50,-50),(-25,-75)]:
                x=max(node_a+dx,0)
                y=max(node_b+dy,0)
                val+=dfs(x,y,stage+1)
                #sum_v+=val
            #dict_v[(node_a,node_b)]=sum_v
        
        dict_v={}
        sum_v=0
        list_v=[]
        dfs(n,n,0)
        
        for val in list_v:
            sum_v+=val

        return sum_v
    def soupServings_d(self, n: int) -> float:




        queue=collections.deque()
        queue.append((n,n,1))
        sum_v=0
        dict_v={}
        while queue:
            node_a,node_b,prob=queue.pop()
            #sum_v+=prob
            if node_a==0 and node_b==0:
                sum_v+=0.5*prob 
                continue
            elif node_a==0:
                sum_v+=prob 
                continue
            elif node_b==0:
                continue
            
            #if (node_a,node_b) in dict_v:
            #    sum_v+=dict_v[(node_a,node_b)]
            #    continue
            
            #counter=0
            for dx,dy in [(-100,0),(-75,-25),(-50,-50),(-25,-75)]:
                x=max(node_a+dx,0)
                y=max(node_b+dy,0)
                queue.append((x,y,0.25*prob))

        return sum_v


        
25200