problem
Alice和Bob赌糖果。规则是这样的:Alice从[ l, r]中随机抽一个数,Bob从[ L, R]中随机抽一个数,谁抽的数大谁就赢,输的一方给另一方1颗糖(平局不用给糖),他们会一直赌下去直到有一方没有糖果为止。
Alice有n颗糖果,Bob有m颗糖果,求Alice将Bob的糖果赢完的概率。
Input
第一行3个整数n, l, r。
第二行3个整数m, L, R。
Output
Alice将Bob糖果赢完的概率,结果保留5位小数。
Sample Input1
1 1 3
1 1 2
Sample Output1
0.75000
Sample Input2
0 1 100
1 1 100
Sample Output2
0.00000
hint
0 <= n,m <=1e5 , n+m > 0
1 <= l <= r <= 100,1 <= L <= R <= 100
思路
先分多种情况计算出去除平局后Alice每局获胜的概率p,然后设Alice将Bob的糖果赢完的概率为k,列出: k∗m∗(1−p)=(1−k)∗n∗p 即可解出k。
代码示例
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m,l,r,L,R;
int t1,t2;
double p;
t1=t2=0;
scanf("%d %d %d %d %d %d",&n,&l,&r,&m,&L,&R);
if(n==0) printf("0.00000\n");
else if(m==0) printf("1.00000\n");
else{
for(int i=l;i<=r;++i){
for(int j=L;j<=R;++j){
if(i>j) t1++;
if(i<j) t2++;
}
}
if(t1==0) printf("0.00000\n");
else{
p=t1*1.0/((t1+t2)*1.0);
printf("%.5f\n",n*p*1.0/(m-m*p+n*p));
}
}
return 0;
}