HDU-5194-DZY Loves Balls(BestCoder Round # 35 )

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

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;
	}
}

时间: 2024-10-11 12:03:58

HDU-5194-DZY Loves Balls(BestCoder Round # 35 )的相关文章

hdu 5194 DZY Loves Balls

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 k

HDU 5194 DZY Loves Balls(概率)

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 t

Hdoj 5194 DZY Loves Balls 【打表】+【STL】

DZY Loves Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 394    Accepted Submission(s): 221 Problem Description There are n black balls and m white balls in the big box. Now, DZY starts

HDU 5268 ZYB loves Score (BestCoder Round#44)

题目链接:ZYB loves Score 题面: ZYB loves Score Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 395    Accepted Submission(s): 232 Problem Description One day,ZYB participated in the BestCoder Contest

hdu 5230 ZCC loves hacking(BestCoder Round #41)

ZCC loves hacking                                                   Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others) Total Submission(s): 126    Accepted Submission(s): 49 Problem Description Now, a Codefires roun

HDU 5995 Kblack loves flag ---BestCoder Round #90

题目链接 用两个布尔数组分别维护每个行/列是否被插过旗帜,最后枚举每一行.列统计答案即可.空间复杂度O(n+m),时间复杂度O(n+m+k). #include <cstdio> #include <iostream> #include <cstring> using namespace std; int t,ansx[1000005],ansy[1000005],ax,ay; const int _K=50268147,_B=6082187,_P=100000007;

HDU 5996 dingyeye loves stone ---BestCoder Round #90

题目链接 设根节点的深度为0,将所有深度为奇数的节点的石子数目xor起来,则先手必胜当且仅当这个xor和不为0. 证明同阶梯博弈.对于偶深度的点上的石子,若对手移动它们,则可模仿操作:对于奇深度上的石子,移动一次即进入偶深度的点. 时空复杂度O(n). 用vector存搜一下就行. #include <cstdio> #include <iostream> #include <vector> #include <cstring> using namespac

hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 221    Accepted Submission(s): 52 Problem Description A topological sort or topological ordering of a directed

BestCoder Round #35(DZY Loves Topological Sorting-堆+贪心)

DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 323    Accepted Submission(s): 86 Problem Description A topological sort or topological ordering of a directed gr