1. 程式人生 > >Alice和Bob賭糖果(概率、經典)

Alice和Bob賭糖果(概率、經典)

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,列出:km(1p)=(1k)np 即可解出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; }