zoukankan      html  css  js  c++  java
  • hdu 4465 Candy(2012 ACM-ICPC 成都现场赛)

             简单概率题,可以直接由剩余n个递推到剩余0个。现在考虑剩余x个概率为(1-p)的candy时,概率为C(2 * n - x, x) * pow(p, n + 1)  *pow(1 - p, n - x);

    在写出x - 1的情况,就可以发现组合数可以直接递推,所以可以直接求。但是考虑到p可能很小,n可能很大,这样的话直接用pow函数会丢失精度,我们可以把double类型写成log10的形式,这样可以保存精度。

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<fstream>
    #include<sstream>
    #include<bitset>
    #include<vector>
    #include<string>
    #include<cstdio>
    #include<cmath>
    #include<stack>
    #include<queue>
    #include<stack>
    #include<map>
    #include<set>
    #define FF(i, a, b) for(int i=a; i<b; i++)
    #define FD(i, a, b) for(int i=a; i>=b; i--)
    #define REP(i, n) for(int i=0; i<n; i++)
    #define CLR(a, b) memset(a, b, sizeof(a))
    #define debug puts("**debug**")
    #define LL long long
    #define PB push_back
    #define SL(a) strlen(a)
    using namespace std;
    
    const int N = 11;
    const int MOD = 1e9 + 7;
    
    int main()
    {
        int n, cas = 1, i, j;
        double p, ans, now;
        while(scanf("%d%lf", &n, &p) != EOF)
        {
            now = log10(p) * (n + 1);
            ans = pow(10.0, now) * n;
            for(i = n - 1; i > 0; i --)
            {
                now = now + log10(2 * n - i + 0.0) - log10(n - i + 0.0) + log10(1 - p);
                ans += pow(10.0, now) * i;
            }
            p = 1 - p;
            now = log10(p) * (n + 1);
            ans += pow(10.0, now) * n;
            for(i = n - 1; i > 0; i --)
            {
                now = now + log10(2 * n - i + 0.0) - log10(n - i + 0.0) + log10(1 - p);
                ans += pow(10.0, now) * i;
            }
            printf("Case %d: %.6lf
    ", cas ++, ans);
        }
    }
    


     

  • 相关阅读:
    InterLockedIncrement and InterLockedDecrement函数原理
    矩阵文件书写的简洁代码
    注册自定义URL协议(zhuan)
    求整数的位数
    WinExec unicode 处理
    C++中如何获取对象的名字(变量名,注意不是类名)
    计算所与北大往事回顾
    不尚贤在人事管理中的作用
    寻找适合自己的无资金创业之路
    诺基亚:用薪酬激励员工
  • 原文地址:https://www.cnblogs.com/riskyer/p/3295363.html
Copyright © 2011-2022 走看看