您的位置:首页 > 其它

hdoj5194DZY Loves Balls

2016-03-22 15:46 267 查看


DZY Loves Balls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 801    Accepted Submission(s): 437


Problem Description

There are n black
balls and m white
balls in the big box.

Now, DZY starts to randomly pick out the balls one by one. It forms a sequence S.
If at the i-th
operation, DZY takes out the black ball, Si=1,
otherwise Si=0.

DZY wants to know the expected times that '01' occurs in S.

 

Input

The input consists several test cases. (TestCase≤150)

The first line contains two integers, n, m(1≤n,m≤12)

 

Output

For each case, output the corresponding result, the format is p/q(p and q are
coprime)

 

Sample Input

1 1
2 3

 

Sample Output

1/2
6/5

HintCase 1: S='01' or S='10', so the expected times = 1/2 = 1/2
Case 2: S='00011' or S='00101' or S='00110' or S='01001' or S='01010'
or S='01100' or S='10001' or S='10010' or S='10100' or S='11000',
so the expected times = (1+2+1+2+2+1+1+1+1+0)/10 = 12/10 = 6/5

 

Source

BestCoder Round #35

 

看输入样例发现n*m/(n+m)就交了一下AC了。贴一下官方的题解吧

考虑期望的可加性。第i(1≤i<n+m)i(1\leq
i < n+m)i(1≤i<n+m)个位置上出现0,第i+1i+1i+1个位置上出现1的概率是mn+m×nn+m−1\frac{m}{n+m}
\times \frac{n}{n+m-1}​n+m​​m​​×​n+m−1​​n​​,那么答案自然就是∑i=1n+m−1mn+m×nn+m−1=nmn+m\sum_{i=1}^{n+m-1}\frac{m}{n+m}
\times \frac{n}{n+m-1}=\frac{nm}{n+m}∑​i=1​n+m−1​​​n+m​​m​​×​n+m−1​​n​​=​n+m​​nm

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<list>
#include<cmath>
#include<vector>
using namespace std;
const int maxn=10010;
int gcd(int a,int b){
return b==0?a:gcd(b,a%b);
}
int main()
{
int n,m,i,j,k;
while(scanf("%d%d",&n,&m)!=EOF){
int x=gcd(n*m,n+m);
printf("%d/%d\n",(n*m)/x,(n+m)/(x));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdoj5194DZY Loves Ba