HDU 5194 DZY Loves Balls(数学组合or各种乱搞)

来源:互联网 发布:阿里云价格计算 编辑:程序博客网 时间:2024/05/29 03:48

DZY Loves Balls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 257    Accepted Submission(s): 142


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. (TestCase150)

The first line contains two integers, nm(1n,m12)
 

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

Sample Input
1 12 3
 

Sample Output
1/26/5
Hint
Case 1: S='01' or S='10', so the expected times = 1/2 = 1/2Case 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个白球,随意一个一个取出,取出黑球代表1,否则代表0,最后成为一个由1、0组成的序列。列举所有可能情况,然后求所有情况中有多少个01序列。假设有共有x个01序列,y种情况,最后输出x/y(最简形式)。

分析:
一直在用组合求,可还是求错了。于是愤怒之下随便写了个m*n/m+n然后除公约数的。谁知pretest过了。。但是由于自己手误,所以最终还是错了。BC给的题解是利用期望的可加性。。或者暴力(不造怎么暴力)。赛后我还是延续自己的组合方法求,终于求出了。先求有多少个01串:利用插入法,假设n<=m,那么组成的一个串就有可能有1~n个01串。对于假设有i个01串,首先找m个0后面的位置分别插1,可以插i个,共有C(i,m)种方法。对于剩下的n-i个1只有插入原先插入1的地方或者最前面才不会改变有i个01串的事实,共有i+1个位置可插。把n-i个1插入到i+1个格子中(每个格子可插0~n-i个),一开始觉得很难算,后来发现只需要注重结果就行了。也就是说可以把它看做是每个格子只能插0个或1个,然后格子就相当于合并后减1个了。这里就是从n-i+(i+1)-1个格子中放入n-i个1了,共有C(n-i,n)种方法。最后乘上i就是有i个01串的情况。遍历1~n即可。对于n>m的反过来,一样的。最后要求共有多少种串:当作有n+m个空,插入n个1:C(n,n+m)。

唉,不想多说了,真是弱啊~
#include<stdio.h>#include<algorithm>using namespace std;int C(int m, int n){    int sum = 1;    for(int i=1; i<=m; i++,n--)        sum = sum*n/i;    return sum;}int main(){    int n, m;    while(~scanf("%d%d", &n,&m))    {        int sum = 0;        for(int i=1; i<=n; i++)            sum += C(i,m)*C(n-i,n)*i;        int x = __gcd(sum,C(n,n+m));        printf("%d/%d\n", sum/x,C(n,n+m)/x);    }    return 0;}


0 0