# 808. Soup Servings

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:

1. Serve 100 ml of soup A and 0 ml of soup B
2. Serve 75 ml of soup A and 25 ml of soup B
3. Serve 50 ml of soup A and 50 ml of soup B
4. 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.

1. 供应100毫升A汤，0毫升B汤。

2. 供应75毫升A汤，25毫升B汤。

3. 供应50毫升A汤，50毫升B汤。

4. 供应25毫升A汤，75毫升B汤。

1. 供应4份A汤，0份B汤。

2. 供应3份A汤，1份B汤。

3. 供应2份A汤，2份B汤。

4. 供应1份A汤，3份B汤。

``````class Solution {
public:
double memo[200][200];
double soupServings(int N) {
return N >= 4800 ? 1.0 : f((N + 24) / 25, (N + 24) / 25);
}
double f(int a, int b) {
if (a <= 0 && b <= 0) return 0.5;
if (a <= 0) return 1.0;
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];
}
};
``````

``````class Solution {
public:
unordered_map<string, double> m;
double soupServings(int N) {
return N >= 4800 ? 1.0 : f(N, N);
}
double f(int a, int b) {
if (a <= 0 && b <= 0) return 0.5;
if (a <= 0) return 1.0;
if (b <= 0) return 0;
string spoon = to_string(a) + ":" + to_string(b);
if (!m.count(spoon)) {
m[spoon] = 0.25 * (f(a - 100, b) + f(a - 75, b - 25) + f(a - 50, b - 50) + f(a - 25, b - 75));
}
return m[spoon];
}
};
``````

https://leetcode.com/problems/soup-servings/discuss/125809/Java-soup-(spoon-included)

https://leetcode.com/problems/soup-servings/discuss/121711/C++JavaPython-When-N-greater-4800-just-return-1

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation