# Need for Pink Slips Codeforces Solution

Page Contents

## Need for Pink Slips Solution

After defeating a Blacklist Rival, you get a chance to draw 11 reward slip out of xx hidden valid slips. Initially, x=3x=3 and these hidden valid slips are Cash Slip, Impound Strike Release Marker and Pink Slip of Rival’s Car. Initially, the probability of drawing these in a random guess are cc, mm, and pp, respectively.

See Also: Codeforces Round 730 (Div. 2)

There is also a volatility factor vv. You can play any number of Rival Races as long as you don’t draw a Pink Slip. Assume that you win each race and get a chance to draw a reward slip. In each draw, you draw one of the xx valid items with their respective probabilities. Suppose you draw a particular item and its probability of drawing before the draw was aa. Then,

• If the item was a Pink Slip, the quest is over, and you will not play any more races.
• Otherwise,
1. If a≤va≤v, the probability of the item drawn becomes 00 and the item is no longer a valid item for all the further draws, reducing xx by 11. Moreover, the reduced probability aa is distributed equally among the other remaining valid items.
2. If a>va>v, the probability of the item drawn reduces by vv and the reduced probability is distributed equally among the other valid items.

For example,

• If (c,m,p)=(0.2,0.1,0.7)(c,m,p)=(0.2,0.1,0.7) and v=0.1v=0.1, after drawing Cash, the new probabilities will be (0.1,0.15,0.75)(0.1,0.15,0.75).
• If (c,m,p)=(0.1,0.2,0.7)(c,m,p)=(0.1,0.2,0.7) and v=0.2v=0.2, after drawing Cash, the new probabilities will be (Invalid,0.25,0.75)(Invalid,0.25,0.75).
• If (c,m,p)=(0.2,Invalid,0.8)(c,m,p)=(0.2,Invalid,0.8) and v=0.1v=0.1, after drawing Cash, the new probabilities will be (0.1,Invalid,0.9)(0.1,Invalid,0.9).
• If (c,m,p)=(0.1,Invalid,0.9)(c,m,p)=(0.1,Invalid,0.9) and v=0.2v=0.2, after drawing Cash, the new probabilities will be (Invalid,Invalid,1.0)(Invalid,Invalid,1.0).

You need the cars of Rivals. So, you need to find the expected number of races that you must play in order to draw a pink slip.Input

The first line of input contains a single integer tt (1≤t≤101≤t≤10)  — the number of test cases.

The first and the only line of each test case contains four real numbers cc, mm, pp and vv (0<c,m,p<10<c,m,p<1, c+m+p=1c+m+p=1, 0.1≤v≤0.90.1≤v≤0.9).

Additionally, it is guaranteed that each of cc, mm, pp and vv have at most 44 decimal places.Output

For each test case, output a single line containing a single real number — the expected number of races that you must play in order to draw a Pink Slip.

Your answer is considered correct if its absolute or relative error does not exceed 10−610−6.

Formally, let your answer be aa, and the jury’s answer be bb. Your answer is accepted if and only if |a−b|max(1,|b|)≤10−6|a−b|max(1,|b|)≤10−6.

Example

input

```4
0.2 0.2 0.6 0.2
0.4 0.2 0.4 0.8
0.4998 0.4998 0.0004 0.1666
0.3125 0.6561 0.0314 0.2048
```

output

```1.532000000000
1.860000000000
5.005050776521
4.260163673896
```

Note

For the first test case, the possible drawing sequences are:

• P with a probability of 0.60.6;
• CP with a probability of 0.2⋅0.7=0.140.2⋅0.7=0.14;
• CMP with a probability of 0.2⋅0.3⋅0.9=0.0540.2⋅0.3⋅0.9=0.054;
• CMMP with a probability of 0.2⋅0.3⋅0.1⋅1=0.0060.2⋅0.3⋅0.1⋅1=0.006;
• MP with a probability of 0.2⋅0.7=0.140.2⋅0.7=0.14;
• MCP with a probability of 0.2⋅0.3⋅0.9=0.0540.2⋅0.3⋅0.9=0.054;
• MCCP with a probability of 0.2⋅0.3⋅0.1⋅1=0.0060.2⋅0.3⋅0.1⋅1=0.006.

So, the expected number of races is equal to 1⋅0.6+2⋅0.14+3⋅0.054+4⋅0.006+2⋅0.14+3⋅0.054+4⋅0.006=1.5321⋅0.6+2⋅0.14+3⋅0.054+4⋅0.006+2⋅0.14+3⋅0.054+4⋅0.006=1.532.

For the second test case, the possible drawing sequences are:

• P with a probability of 0.40.4;
• CP with a probability of 0.4⋅0.6=0.240.4⋅0.6=0.24;
• CMP with a probability of 0.4⋅0.4⋅1=0.160.4⋅0.4⋅1=0.16;
• MP with a probability of 0.2⋅0.5=0.10.2⋅0.5=0.1;
• MCP with a probability of 0.2⋅0.5⋅1=0.10.2⋅0.5⋅1=0.1.

So, the expected number of races is equal to 1⋅0.4+2⋅0.24+3⋅0.16+2⋅0.1+3⋅0.1=1.861⋅0.4+2⋅0.24+3⋅0.16+2⋅0.1+3⋅0.1=1.86.

## Solution

Program C++:

```#include <bits/stdc++.h>

using namespace std;

map<int, double> mm;
double eps = 1e-9;

void solve(int k, double c, double m, double p, double v, double cur) {
if (abs(p-1) < eps) {
mm[k+1] += cur;
return;
}
if (c > eps) {
if (c > v) {
if (abs(m) < eps) {
solve(k+1, c-v, 0, p+v, v, cur*c);
} else {
solve(k+1, c-v, m+v/2, p+v/2, v, cur*c);
}
} else {
if (abs(m) < eps) {
solve(k+1, 0., 0., 1., v, cur*c);
} else {
solve(k+1, 0, m+c/2, p+c/2, v, cur*c);
}
}
}
if (m > eps) {
if (m > v) {
if (abs(c) < eps) {
solve(k+1, 0, m-v, p+v, v, cur*m);
} else {
solve(k+1, c+v/2, m-v, p+v/2, v, cur*m);
}
} else {
if (abs(c) < eps) {
solve(k+1, 0., 0., 1., v, cur*m);
} else {
solve(k+1, c+m/2, 0, p+m/2, v, cur*m);
}
}

}
if (p > 0) {
mm[k+1] += cur*p;
}
}

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
double c, m, p, v;
cin >> c >> m >> p >> v;
mm.clear();
solve(0, c, m, p, v, 1.0);
double ans = 0;
//debug(mm);
for (auto x : mm) {
ans += x.first * x.second;
}
cout << setprecision(18) << ans << "\n";
}
return 0;
}```