HDU-5194-DZY Loves Balls(BestCoder Round # 35 )
2015-03-30 16:59
387 查看
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
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
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; } }
相关文章推荐
- hdu 5196 DZY Loves Inversions && BestCoder Round #35
- 【HDU5648 BestCoder Round 76 (div1)C】【打表or数位DP】DZY Loves Math nm范围内i和j i或j的gcd之和
- 【HDU5649 BestCoder Round 76 (div1)D】【二分+线段树】DZY Loves Sorting 全排列1~n 区间升序降序排序 最后k位置的数是几
- hdu 5274 Dylans loves tree && BestCoder Round #45
- HDU 5996 BestCoder Round #90 1002 dingyeye loves stone 博弈
- HDU 5194 DZY Loves Balls(排列组合瞎搞 )
- HDU 5194 DZY Loves Balls(概率)
- HDU 5996 dingyeye loves stone(阶梯尼姆博弈)——BestCoder Round #90
- hdu 5269 ZYB loves Xor I && BestCoder Round #44
- HDU 5194 DZY Loves Balls
- HDU 5194——DZY Loves Balls
- hdu 5194 DZY Loves Balls
- HDU 5194 DZY Loves Balls (组合数学)
- hdu 5194 DZY Loves Balls(暴力,数学期望)
- hdu 5194 DZY Loves Balls【bfs+打表】
- HDU 5997 BestCoder Round #90 1003 rausen loves cakes 线段树 树状数组
- HDU 5194 DZY Loves Balls
- HDU 5194 DZY Loves Balls(数学组合or各种乱搞)
- 【HDU5646 BestCoder Round 76 (div1)A】【贪心】DZY Loves Partition n个数拆分k个最大乘积
- HDU 5194 DZY Loves Balls(期望可加性)