• Candy HDU 4665 概率期望问题


    Candy

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1159    Accepted Submission(s): 508
    Special Judge


    Problem Description
    LazyChild is a lazy child who likes candy very much. Despite being very young, he has two large candy boxes, each contains n candies initially. Everyday he chooses one box and open it. He chooses the first box with probability p and the second box with probability (1 - p). For the chosen box, if there are still candies in it, he eats one of them; otherwise, he will be sad and then open the other box.
    He has been eating one candy a day for several days. But one day, when opening a box, he finds no candy left. Before opening the other box, he wants to know the expected number of candies left in the other box. Can you help him?
     
    Input
    There are several test cases.
    For each test case, there is a single line containing an integer n (1 ≤ n ≤ 2 × 105) and a real number p (0 ≤ p ≤ 1, with 6 digits after the decimal).
    Input is terminated by EOF.
     
    Output
    For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) and Y is a real number indicating the desired answer.
    Any answer with an absolute error less than or equal to 10-4 would be accepted.
     
    Sample Input
    10 0.400000 100 0.500000 124 0.432650 325 0.325100 532 0.487520 2276 0.720000
     
    Sample Output
    Case 1: 3.528175 Case 2: 10.326044 Case 3: 28.861945 Case 4: 167.965476 Case 5: 32.601816 Case 6: 1390.500000
     
    Source
     
    Recommend
    liuyiding
     公式很好推。。。
    主要是组合数的计算
    /*
     * Author:  
     * Created Time:  2013/10/16 18:29:51
     * File Name: A.cpp
     * solve: A.cpp
     */
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    #include<string>
    #include<map>
    #include<stack>
    #include<set>
    #include<iostream>
    #include<vector>
    #include<queue>
    //ios_base::sync_with_stdio(false);
    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    
    using namespace std;
    #define sz(v) ((int)(v).size())
    #define rep(i, a, b) for (int i = (a); i < (b); ++i)
    #define repf(i, a, b) for (int i = (a); i <= (b); ++i)
    #define repd(i, a, b) for (int i = (a); i >= (b); --i)
    #define clr(x) memset(x,0,sizeof(x))
    #define clrs( x , y ) memset(x,y,sizeof(x))
    #define out(x) printf(#x" %d
    ", x)
    #define sqr(x) ((x) * (x))
    typedef long long LL;
    
    const int INF = 1000000000;
    const double eps = 1e-8;
    const int maxn = 210000;
    
    int sgn(const double &x) {  return (x > eps) - (x < -eps); }
    
    double lnx[maxn*2];
    void init()
    {
        lnx[0] = log(1.0);
        lnx[1] = log(1.0);
        repf(i,2,400010)
        {
            lnx[i] = lnx[i - 1] + log((double)i);
        }
    }
    double C(int n,int m)
    {
        return lnx[n] - lnx[m] - lnx[n - m];
    }
    int main() 
    {
        //freopen("in.txt","r",stdin);
        int n;
        double p;
        int k = 1;
        init();
        while(scanf("%d%lf",&n,&p) == 2)
        {
            double ans = 0;
            double P = log(p);
            double Q = log(1 - p);
            rep(i,0,n)
            {
                double tmp = C(n+i,i);
                ans += (n-i)*(exp(tmp + (n+1)*P + i*Q) + exp(tmp + (n+1)*Q + i*P));
            }
            printf("Case %d: %.6lf
    ",k++,ans);
            
        }
        return 0;
    }
  • 相关阅读:
    第一章:Android系统的编译和移植实例读书笔记
    第二章:Android系统与嵌入式开发读书笔记
    第三章:Android移植平台工具介绍读书笔记
    第十章 嵌入式Linux的调试技术
    第九章 硬件抽象层:HAL
    第八章 让开发板发出声音:蜂鸣器驱动
    第七章 LED将为我闪烁:控制发光二极管
    第六章 第一个Linux驱动程序:统计单词个数
    第五章 搭建S3C6410开发板的测试环境
    第四章 源代码的下载和编译
  • 原文地址:https://www.cnblogs.com/DreamHighWithMe/p/3372819.html
Copyright © 2020-2023  润新知