There are two types of soup: type A and type B. Initially we have N ml of each type of soup. There are four kinds of operations:

Serve 100 ml of soup A and 0 ml of soup B
Serve 75 ml of soup A and 25 ml of soup B
Serve 50 ml of soup A and 50 ml of soup B
Serve 25 ml of soup A and 75 ml of soup B

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 equal probability 0.25. If the remaining volume of soup is not enough to complete the operation, we will serve as much as we can.  We stop once we no longer have some quantity of both types of soup.
Note that we do not have the operation where all 100 ml's of soup B are used first.  
Return the probability that soup A will be empty first, plus half the probability that A and B become empty at the same time.
 
Example:
Input: N = 50
Output: 0.625
Explanation: 
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.


Notes: 

0 <= N <= 10^9. 
Answers within 10^-6 of the true value will be accepted as correct.

Difficulty: MEDIUM
Starter code: class Solution:
    def soupServings(self, N: int) -> float:
        

Solutions:
class Solution:

	def soupServings(self, N: int) -> float:
		if N > 5000:
			return 1

		@lru_cache(None)
		def dp(a, b):
			if a <= 0 and b <= 0:
				return 0.5
			if a <= 0:
				return 1
			if b <= 0:
				return 0
			return (dp(a - 100, b) + dp(a - 75, b - 25) + dp(a - 50, b - 50) + dp(a - 25, b - 75)) / 4
		return dp(N, N)


class Solution:
	mem = {}

	def soupServings(self, N: int) -> float:
		if N > 4800:
			return 1

		def empty(a, b):
			if (a, b) in self.mem:
				return self.mem[a, b]
			if a <= 0 and b <= 0:
				return 0.5
			if a <= 0:
				return 1
			if b <= 0:
				return 0
			ans = 0.25 * (empty(a - 4, b) + empty(a - 3, b - 1) + empty(a - 2, b - 2) + empty(a - 1, b - 3))
			self.mem[a, b] = ans
			return ans
		N = math.ceil(N / 25)
		return empty(N, N)


class Solution:

	def soupServings(self, N: int) -> float:
		seen = {}

		def find_prob(A, B):
			if A == 0 and B == 0:
				return 0.5
			if A == 0:
				return 1
			if B == 0:
				return 0
			if (A, B) in seen:
				return seen[A, B]
			temp = 0
			for (x, y) in [[100, 0], [75, 25], [50, 50], [25, 75]]:
				temp += find_prob(A - x if A > x else 0, B - y if B > y else 0)
			seen[A, B] = 0.25 * temp
			return 0.25 * temp
		if N > 4800:
			return 1.0
		return find_prob(N, N)


class Solution:

	def soupServings(self, N: int) -> float:

		def f(a, b):
			if a <= 0 and b <= 0:
				return 0.5
			if a <= 0:
				return 1
			if b <= 0:
				return 0
			if _memo[a][b] > 0:
				return _memo[a][b]
			_memo[a][b] = 0.25 * (f(a - 4, b) + f(a - 3, b - 1) + f(a - 2, b - 2) + f(a - 1, b - 3))
			return _memo[a][b]
		_memo = [[0] * 200 for i in range(200)]
		if N >= 4800:
			return 1
		else:
			return f((N + 24) // 25, (N + 24) // 25)


class Solution:

	def soupServings(self, N: int) -> float:
		(q, r) = divmod(N, 25)
		N = q + (r > 0)
		if N >= 500:
			return 1
		memo = {}

		def dp(x, y):
			if (x, y) not in memo:
				if x <= 0 or y <= 0:
					res = 0.5 if x <= 0 and y <= 0 else 1.0 if x <= 0 else 0.0
				else:
					res = 0.25 * (dp(x - 4, y) + dp(x - 3, y - 1) + dp(x - 2, y - 2) + dp(x - 1, y - 3))
				memo[x, y] = res
			return memo[x, y]
		return dp(N, N)


class Solution:

	def soupServings(self, N: int) -> float:
		if N > 10000:
			return 1
		rounds = (N + 25 - 1) // 25
		dp = [{(N, N): 1}] + [collections.defaultdict(float) for _ in range(rounds)]
		for i in range(1, rounds + 1):
			for (a, b) in dp[i - 1]:
				if a <= 0 or b <= 0:
					dp[i][a, b] += dp[i - 1][a, b]
				else:
					dp[i][max(a - 100, 0), b] += dp[i - 1][a, b] * 0.25
					dp[i][max(a - 75, 0), max(b - 25, 0)] += dp[i - 1][a, b] * 0.25
					dp[i][max(a - 50, 0), max(b - 50, 0)] += dp[i - 1][a, b] * 0.25
					dp[i][max(a - 25, 0), max(b - 75, 0)] += dp[i - 1][a, b] * 0.25
		res = dp[rounds][0, 0] / 2
		for (a, b) in dp[rounds]:
			if a <= 0 and b > 0:
				res += dp[rounds][a, b]
		return res


class Solution:

	def soupServings(self, N: int) -> float:
		(Q, R) = divmod(N, 25)
		N = Q + (R > 0)
		if N >= 500:
			return 1
		memo = {}

		def dp(x, y):
			if (x, y) not in memo:
				if x <= 0 or y <= 0:
					ans = 0.5 if x <= 0 and y <= 0 else 1.0 if x <= 0 else 0.0
				else:
					ans = 0.25 * (dp(x - 4, y) + dp(x - 3, y - 1) + dp(x - 2, y - 2) + dp(x - 1, y - 3))
				memo[x, y] = ans
			return memo[x, y]
		return dp(N, N)


class Solution:

	def soupServings(self, N: int) -> float:
		if N % 25 == 0:
			N = N // 25
		else:
			N = N // 25 + 1
		solA = {}
		solB = {}

		def solutionA(a, b):
			if a == 0 and b > 0:
				return 1
			elif a == 0 and b == 0:
				return 0
			elif a > 0 and b == 0:
				return 0
			elif (a, b) in solA:
				return solA[a, b]
			else:
				solA[a, b] = 0.25 * (solutionA(max(a - 4, 0), b) + solutionA(max(a - 3, 0), max(b - 1, 0)) + solutionA(max(a - 2, 0), max(b - 2, 0)) + solutionA(max(a - 1, 0), max(b - 3, 0)))
				return solA[a, b]

		def solutionB(a, b):
			if a == 0 and b == 0:
				return 1
			elif a == 0 or b == 0:
				return 0
			elif (a, b) in solB:
				return solB[a, b]
			else:
				solB[a, b] = 0.25 * (solutionB(max(a - 4, 0), b) + solutionB(max(a - 3, 0), max(b - 1, 0)) + solutionB(max(a - 2, 0), max(b - 2, 0)) + solutionB(max(a - 1, 0), max(b - 3, 0)))
				return solB[a, b]
		if N > 300:
			return 1
		else:
			return solutionA(N, N) + 0.5 * solutionB(N, N)


class Solution:

	def soupServings(self, N):
		if N >= 5551:
			return 1
		self.dd = collections.defaultdict(float)
		return self.sub(N, N)

	def sub(self, an, bn):
		if an <= 0 and bn <= 0:
			return 0.5
		if an <= 0:
			return 1
		if bn <= 0:
			return 0
		if (an, bn) in self.dd:
			return self.dd[an, bn]
		c1 = self.sub(an - 100, bn)
		c2 = self.sub(an - 75, bn - 25)
		c3 = self.sub(an - 50, bn - 50)
		c4 = self.sub(an - 25, bn - 75)
		self.dd[an, bn] = 0.25 * sum([c1, c2, c3, c4])
		return self.dd[an, bn]


import math

class Solution:

	def soupServings(self, N: int) -> float:
		N = math.ceil(N / 25)
		if N >= 1000:
			return 1
		dp = [[1 for i in range(N + 1)] for j in range(N + 1)]
		for i in range(0, N + 1):
			dp[i][0] = 0
		dp[0][0] = 0.5
		for i in range(1, N + 1):
			for j in range(1, N + 1):
				M = lambda x: max(0, x)
				dp[i][j] = 0.25 * (dp[M(i - 4)][j] + dp[M(i - 3)][j - 1] + dp[M(i - 2)][M(j - 2)] + dp[i - 1][M(j - 3)])
				if 1 - dp[i][j] < 1e-06:
					break
			if 1 - dp[i][i] < 1e-06:
				break
		return dp[N][N]


class Solution:

	def soupServings(self, N: int) -> float:
		memo = {}
		if N > 4800:
			return 1

		def dp(a, b):
			if (a, b) in memo:
				return memo[a, b]
			if a <= 0 and b > 0:
				return 1
			if a <= 0 and b <= 0:
				return 0.5
			if a > 0 and b <= 0:
				return 0
			memo[a, b] = (dp(a - 100, b) + dp(a - 75, b - 25) + dp(a - 50, b - 50) + dp(a - 25, b - 75)) / 4
			return memo[a, b]
		return dp(N, N)


class Solution:

	def soupServings(self, N: int) -> float:
		if N >= 10000:
			return 1.0
		L = N // 25 + 1 if N % 25 > 0 else N // 25
		res = dict()
		res[0, 0] = 0.5
		for l in range(1, L + 1):
			res[l, 0] = 0.0
			res[0, l] = 1.0
		for m in range(1, L + 1):
			for n in range(1, L + 1):
				res[m, n] = 0.25 * (res[max([m - 4, 0]), n] + res[max([m - 3, 0]), max([n - 1, 0])] + res[max([m - 2, 0]), max(n - 2, 0)] + res[max(m - 1, 0), max(n - 3, 0)])
		return res[L, L]


class Solution:

	def soupServings(self, N: int) -> float:
		N = N // 25 + (N % 25 > 0)
		if N >= 500:
			return 1
		dp = [[0] * (N + 1) for _ in range(N + 1)]
		operations = [(4, 0), (3, 1), (2, 2), (1, 3)]
		dp[-1][-1] = 1
		for i in range(N, 0, -1):
			for j in range(N, 0, -1):
				for (a, b) in operations:
					if i - a < 0 and j - b < 0:
						dp[0][0] += dp[i][j] * 0.25
					elif i - a < 0:
						dp[0][j - b] += dp[i][j] * 0.25
					elif j - b < 0:
						dp[i - a][0] += dp[i][j] * 0.25
					else:
						dp[i - a][j - b] += dp[i][j] * 0.25
		a_total = 0
		for i in range(N + 1):
			a_total += dp[0][i]
		return a_total - dp[0][0] / 2


class Solution:

	def soupServings(self, N: int) -> float:
		n = N // 25 + (N % 25 > 0)
		if n >= 500:
			return 1
		dp = [[0] * (n + 1) for i in range(n + 1)]
		dp[-1][-1] = 1
		x = [-4, -3, -2, -1]
		y = [0, -1, -2, -3]
		for i in range(n, 0, -1):
			for j in range(n, 0, -1):
				for (dx, dy) in zip(x, y):
					if i + dx < 0 and j + dy < 0:
						dp[0][0] += dp[i][j] * 0.25
					elif i + dx < 0:
						dp[0][j + dy] += dp[i][j] * 0.25
					elif j + dy < 0:
						dp[i + dx][0] += dp[i][j] * 0.25
					else:
						dp[i + dx][j + dy] += dp[i][j] * 0.25
		Sum = 0
		for j in range(n + 1):
			Sum += dp[0][j]
		return Sum - dp[0][0] / 2


class Solution:

	def soupServings(self, N: int) -> float:
		import numpy as np
		if N > 5000:
			return 1
		if N == 0:
			return 0.5
		if N % 25 == 0:
			N = N // 25
		else:
			N = N // 25 + 1
		Matrix = [[0.0 for i in range(N + 1)] for j in range(N + 1)]
		Matrix = np.matrix(Matrix)
		Matrix[N, N] = 1
		Explore_List = [(N, N)]
		while Explore_List:
			New_List = set()
			for (x, y) in Explore_List:
				if x != 0 and y != 0:
					Matrix[max(0, x - 2), max(0, y - 2)] += Matrix[x, y] * 1 / 4
					New_List.add((max(0, x - 2), max(0, y - 2)))
					Matrix[max(0, x - 1), max(0, y - 3)] += Matrix[x, y] * 1 / 4
					New_List.add((max(0, x - 1), max(0, y - 3)))
					Matrix[max(0, x - 3), max(0, y - 1)] += Matrix[x, y] * 1 / 4
					New_List.add((max(0, x - 3), max(0, y - 1)))
					Matrix[max(0, x - 4), max(0, y - 0)] += Matrix[x, y] * 1 / 4
					New_List.add((max(0, x - 4), max(0, y - 0)))
			Explore_List = New_List
		return Matrix[0].sum() - 1 / 2 * Matrix[0, 0]


class Solution:

	def soupServings(self, N: int) -> float:
		operations = [(100, 0), (75, 25), (50, 50), (25, 75)]

		@lru_cache(maxsize=None)
		def helper(remainA, remainB):
			if remainA == 0 and remainB == 0:
				return 0.5
			if remainA == 0 or remainB == 0:
				return remainA == 0
			result = 0
			for op in operations:
				result += helper(max(0, remainA - op[0]), max(0, remainB - op[1]))
			return result / 4
		N = min(N, 4801)
		return helper(N, N)


class Solution:

	def soupServings(self, N: int) -> float:

		def prob(A, B, dp):
			if A == 0 and B == 0:
				return 0.5
			if A == 0:
				return 1.0
			if dp[A][B] != -1.0:
				return dp[A][B]
			else:
				total_prob = 0
				total_prob += 1 / 4 * prob(max(0, A - 100), B, dp)
				if B - 25 > 0 or (A - 75 <= 0 and B - 25 <= 0):
					total_prob += 1 / 4 * prob(max(0, A - 75), max(0, B - 25), dp)
				if B - 50 > 0 or (A - 50 <= 0 and B - 50 <= 0):
					total_prob += 1 / 4 * prob(max(0, A - 50), max(0, B - 50), dp)
				if B - 75 > 0 or (A - 25 <= 0 and B - 75 <= 0):
					total_prob += 1 / 4 * prob(max(0, A - 25), max(0, B - 75), dp)
				dp[A][B] = total_prob
				return dp[A][B]
		if N > 4800:
			return 1.0
		dp = [[-1.0 for i in range(N + 1)] for j in range(N + 1)]
		return prob(N, N, dp)


class Solution:

	def soupServings(self, N: int) -> float:
		if N > 5000:
			return 1
		dp = [[-1 for i in range(N + 1)] for j in range(N + 1)]
		return self.helper(N, N, dp)

	def helper(self, A, B, dp):
		if A <= 0 and B <= 0:
			return 0.5
		if A <= 0:
			return 1
		if B <= 0:
			return 0
		if dp[A][B] != -1:
			return dp[A][B]
		dp[A][B] = (self.helper(A - 100, B - 0, dp) + self.helper(A - 75, B - 25, dp) + self.helper(A - 50, B - 50, dp) + self.helper(A - 25, B - 75, dp)) * 0.25
		return dp[A][B]


class Solution:

	def soupServings(self, N: int) -> float:
		compressed_N = N // 25
		N = compressed_N + (1 if N - compressed_N * 25 > 0 else 0)
		mem = {}

		def dp(a, b):
			if (a, b) in mem:
				return mem[a, b]
			else:
				if a <= 0 or b <= 0:
					result = 0.5 if a <= 0 and b <= 0 else 1.0 if a <= 0 else 0.0
				else:
					result = 0.25 * (dp(a - 4, b) + dp(a - 3, b - 1) + dp(a - 2, b - 2) + dp(a - 1, b - 3))
				mem[a, b] = result
				return result
		if N <= 200:
			return dp(N, N)
		else:
			for i in range(200, N + 1):
				current_result = dp(i, i)
				if 1 - current_result < 1e-06:
					return 1.0
			return current_result


class Solution:

	def soupServings(self, N: int) -> float:

		def dfs(a, b):
			if a <= 0 and b <= 0:
				return 0.5
			if a <= 0 and b > 0:
				return 1
			if a > 0 and b <= 0:
				return 0
			if memo[a][b] != -1:
				return memo[a][b]
			memo[a][b] = 0.25 * (dfs(a - 4, b) + dfs(a - 3, b - 1) + dfs(a - 2, b - 2) + dfs(a - 1, b - 3))
			return memo[a][b]
		if N >= 5000:
			return 1
		n = N // 25 + int(N % 25 > 0)
		memo = [[-1] * 200 for _ in range(200)]
		return dfs(n, n)


class Solution:

	def soupServings(self, N: int) -> float:
		if N >= 12500:
			return 1
		self.memory = {}
		return self.helper(N, N)

	def helper(self, A, B):
		if (A, B) in self.memory:
			return self.memory[A, B]
		elif A <= 0 and B <= 0:
			return 0.5
		elif B <= 0:
			return 0
		elif A <= 0:
			return 1
		prob = 0
		prob += 0.25 * self.helper(A - 100, B)
		prob += 0.25 * self.helper(A - 75, B - 25)
		prob += 0.25 * self.helper(A - 50, B - 50)
		prob += 0.25 * self.helper(A - 25, B - 75)
		self.memory[A, B] = prob
		return prob


class Solution:

	def soupServings(self, N: int) -> float:
		if N > 5555:
			return 1
		memo = dict()

		def Pa(A, B):
			if (A, B) in memo:
				return memo[A, B]
			if A <= 0 and B <= 0:
				return 0.5
			if A <= 0:
				return 1
			if B <= 0:
				return 0
			else:
				memo[A, B] = (Pa(A - 100, B) + Pa(A - 75, B - 25) + Pa(A - 50, B - 50) + Pa(A - 25, B - 75)) / 4
				return memo[A, B]
		return Pa(N, N)