hdu5194 DZY Loves Balls 【概率论 or 搜索】

//yy:那天考完概率论,上网无聊搜个期望可加性就搜到这题,看到以后感觉特别亲和,挺有意思的。

hdu5194 DZY Loves Balls 【概率论 or 搜索】

题意:

一个盒子里有n个黑球和m个白球【n,m≤12】。每次随机从盒子里取走一个球,取了n+m次后,刚好取完。现在用一种方法生成了一个随机的01串S[1…(n+m)],如果第i次取出的球是黑色的,那么S[i]=1,如果是白色的,那么S[i]=0。求‘01‘在S串中出现的期望次数。

题解:

求出在第i个位置上出现0,第i+1个位置上出现1的概率,这种情况设为Xi = 1,这就是二项分布啦,

根据期望的可加性,有E∑Xi = N * P。(期望的可加性不要求事件相互独立喔,方差要求所以可以这样做吖)

Xi = 1的概率 P = m / (n + m)  *  n / (n + m - 1)

由题意可知这里的 N = (n + m - 1)

化简一下答案就出来了: n * m / (n + m)

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

还有个搜索方法,房教写的,膜拜ORZ

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
long long dp[13][13];
long long C[25][25];
long long ans = 0;
ll dfs(const int& x,const int& y,const int& st)
{
    if(st==1&&dp[x][y]!=-1) return dp[x][y];
    long long ans = 0;
    if(st==0){
        if(x>0){
            ans += C[x+y-1][y] + dfs(x-1,y,1);
        }
        if(y>0)
        {
            ans += dfs(x,y-1,0);
        }
    }
    if(st==1){
        if(x>0){
            ans += dfs(x-1,y,1);
        }
        if(y>0)
        {
            ans += dfs(x,y-1,0);
        }
    }
    if(st==1)dp[x][y] = ans;
    return ans;
}
void get_C(int maxn)
{
    C[0][0] = 1;
    for(int i=1;i<=maxn;i++)
    {
        C[i][0] = 1;
        for(int j=1;j<=i;j++)
            C[i][j] = C[i-1][j]+C[i-1][j-1];
        //C[i][j] = (C[i-1][j]+C[i-1][j-1])%MOD;
    }
}
int main(){
    memset(dp,-1,sizeof(dp));
    dp[0][0] = 0;
    get_C(24);
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        ll b=C[n+m][n];
        ll sum=dfs(n,m,1);
        ll a=__gcd(sum,b);
        printf("%lld/%lld\n",sum/a,b/a);
    }
    return 0;
}
时间: 2024-10-13 17:44:29

hdu5194 DZY Loves Balls 【概率论 or 搜索】的相关文章

HDU5194 DZY Loves Balls【排列组合】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5194 题目大意: 袋子里有N个黑球.M个白球.用1表示取出来的是黑球,0表示取出来的是白球.不放回 的从袋子里取出这N+M个球.求相邻取出的两个球第一个球白球,第二个球是黑球的期望 次数是多少,即出现"01"的期望次数是多少. 思路: 考虑期望可加.第i(1<=i<=N+M)个位置上出现白球的概率为M/(M+N),则i+1个位置上 出现黑球的概率为N/(M+N-1).因为白球

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-5645 DZY Loves Balls(水题)

题目链接: DZY Loves Balls Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 105    Accepted Submission(s): 75 Problem Description DZY loves playing balls. He has n balls in a big box. On each ball

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

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

BestCoder Round #35(DZY Loves Balls-暴力dp)

DZY Loves Balls Accepts: 371 Submissions: 988 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description There are n  black balls and m  white balls in the big box. Now, DZY starts to randomly pick out the ba

Codeforces 444A DZY Loves Physics(图论)

题目链接:Codeforces 444A DZY Loves Physics 题目大意:给出一张图,图中的每个节点,每条边都有一个权值,现在有从中挑出一张子图,要求子图联通,并且被选中的任意两点,如果存在边,则一定要被选中.问说点的权值和/边的权值和最大是多少. 解题思路:是图论中的一个结论,最多两个节点,所以枚举两条边就可以了.我简单的推了一下,2个点的情况肯定比3个点的优. 假设有3个点a,b,c,权值分别为A,B,C 现a-b,b-c边的权值分别为u,v 那么对于两点的情况有A+Bu,B+

Hdoj 5195 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): 922 Accepted Submission(s): 269 Problem Description A topological sort or topological ordering of a directed graph i