hdu 5194 DZY Loves Balls(暴力,数学期望)

本文介绍了一个关于从盒子中随机取出黑白球形成序列的问题,并探讨了序列中特定子序列“01”出现次数的数学期望。通过列举所有可能情况的方法给出了问题的解答,并提出了一种更高效的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DZY Loves Balls

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


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 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
题意:有n个1和m个0,问组成的所有串中01的数学期望是多少?

思路:最简单的思路就是暴力枚举出所有串,然后对a和b求一次gcd即可,n和m都是12,复杂度可以承受。

更加高效方便的办法是利用期望的可加性,把每一个位置上01串的期望相加即可得到总期望值

代码1:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
long long a;
int n,m;
void dfs(int s,int t,int d,int last,int num)
{
    if(d==n+m)
    {
        a+=num;
        return;
    }
    if(s<n)  dfs(s+1,t,d+1,0,num);
    if(t<m)
    {
        if(last==0) dfs(s,t+1,d+1,1,num+1);
        else dfs(s,t+1,d+1,1,num);
    }
}
long long gcd(long long a,long long b)
{
    return b?gcd(b,a%b):a;
}
int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        a=0;
        dfs(0,0,0,-1,0);
        long long b=1;
        for(long long i=n+m; i>=m+1; i--)
            b*=i;
        for(long long i=n; i; i--)
            b/=i;
        long long c=gcd(a,b);
        printf("%lld/%lld\n",a/c,b/c);
    }
    return 0;
}
代码2:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
long long a;
int n,m;
int gcd(int a,int b)
{
    return b?gcd(b,a%b):a;
}
int main()
{
    while(~scanf("%d %d",&n,&m))
    {
        int a=n*m,b=n+m;
        int c=gcd(a,b);
        printf("%d/%d\n",a/c,b/c);
    }
    return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值