DZY Loves Balls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 374 Accepted Submission(s): 205
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
Sample Output
1/2
6/5
Hint
Case 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
Recommend
hujie | We have carefully selected several similar problems for you: 5197 5196 5195 5193 5192
BestCoder解析:
Problem A - DZY Loves Balls
考虑期望的可加性。第
i(1≤i<n+m)
个位置上出现0,第
i+1
个位置上出现1的概率是
mn+m×nn+m−1
,那么答案自然就是
∑i=1n+m−1mn+m×nn+m−1=nmn+m
如果你不能马上想到上述的简便的方法,也可以选择暴力枚举所有01串,也是可以AC的。最后一步你需要再计算一下gcd,十分简便。
n个黑球,m个白球,1表示取出的是黑球,0表示取出的是白球。
这题最好的方法就是找出题目的规律,即第i(1<=i<n+m)个位置上出现0,第i+1个位置上出现1的概率是 m/(n+m) * n/(n+m-1) .
因为要求‘01’串在S串中出现的位置,即满足这个条件,但是'0'不可以在末尾,'1'不可以在开头.对于前面n+m-1个位置都可以为0.
第i+1个位置上出现1的概率是:(出现黑球(出现1)的概率)*(出现在第i个位置上的概率)。
但是一共有n+m-1个位置上可以出现1,所以便是所有位置上的概率想加.
等价于= m/(n+m) * n/(n+m-1) * (n+m-1) = n*m/(n+m).
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
while (input.hasNext())
{
int n = input.nextInt();
int m = input.nextInt();
int temp = GCD(n * m, n + m);
System.out.println(n * m / temp + "/" + (n + m) / temp);
}
}
public static int GCD(int x, int y)
{
if (x < y)
return GCD(y, x);
while (x % y != 0)
{
int temp = x % y;
x = y;
y = temp;
}
return y;
}
}