C. Need for Pink Slips-Codeforces Round #730 (Div. 2)

题目链接Problem - 1543C - Codeforces

C. Need for Pink Slips

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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. 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

Copy

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

Copy

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.

---------------------------------------------------------------------------------------------------------------------------------本题突破点在于v的取值范围,由于概率不会超过1,所以很轻易的进行几局就把前两个概率都减为了0。

题意比较难理解,就是说抽到第三个就不能往下抽。抽到前两个,下次的概率会受到影响。比如抽到第一个,第一个概率会减为0,或者a-v,后两者概率也会受到影响,非零的受到减去的概率的均分。

另外考察精度eps的应用,由于计算机对于浮点数精度存在误差,大于某个数不能单纯写>于号,应该是 a-b>eps才行。a>0,应该是a>eps。

最终答案是期望,也就是第一轮抽到第三个的概率*1,加上第二轮抽到第三个的概率*2...概率乘轮数的和即可。

利用dfs,设置a,b,c,v变量代表三者本次概率,和减去的v,cnt代表本次是第几次抽,z代表出现本次情况的概率,p是本次情况抽到第三者的概率。z*p就是这一轮数部分情况下抽到第三者的概率。

# include<iostream>
# include<algorithm>
# include<iomanip>

using namespace std;

double eps=1e-8,sum=0;

void dfs(double a,double b, double c,double v,int cnt,double z,double p)
{
    sum+=cnt*p*z;

    if(a>eps)
    {
        double min1=min(a,v);

        if(b>eps)
            dfs(a-min1,b+min1/2,c+min1/2,v,cnt+1,z*a,c+min1/2);
        else
            dfs(a-min1,b,c+min1,v,cnt+1,z*a,c+min1);

    }

    if(b>eps)
    {
        double min1=min(b,v);

        if(a>eps)
            dfs(a+min1/2,b-min1,c+min1/2,v,cnt+1,z*b,c+min1/2);
        else
            dfs(a,b-min1,c+min1,v,cnt+1,z*b,c+min1);
    }
}
int main()
{


    int t;
    cin>>t;

    while(t--)
    {


        double a,b,c,v;
        cin>>a>>b>>c>>v;

        sum=0;
        dfs(a,b,c,v,1,1,c);

        cout<<fixed<<setprecision(12)<<sum<<'\n';

    }

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinsanma and Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值