uva 1639 Candy 大数的对数处理 数学期望

  1. 当排列组合数或者幂很大时可以利用对数计算,之后再用exp还原,保证一定的精度。
  2. 数学期望是每一个可能的值和相应的概率的乘积和,没有可能值可以设。
  3. 仔细读题,吃完最后一个糖果后不知道是否已经吃完,所以需要再选一次。
  4. %f用来输入float,输出float double.
  5. %lf 用来输入double,输出long double.

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=825&problem=4514&mosmsg=Submission+received+with+ID+17971092

#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<vector>
#include<fstream>
#include<list>
using namespace std;

#define ms(s) memset(s,0,sizeof(s))
typedef unsigned long long ULL;
typedef long long LL;

const int INF = 0x3fffffff;

const int maxn = 400010;
long double logF[maxn];

void make_logF(){
    logF[0] = 0;
    for(int i = 1; i <= maxn; ++i)
        logF[i] = logF[i-1]+log(i);
}

long double getC(int n, int r){
    return logF[n] - logF[n-r] - logF[r];
}

int main(){
    freopen("F:\\input.txt","r",stdin);
//    freopen("F:\\output.txt","w",stdout);
//    ios::sync_with_stdio(false);
    int n;
    double p;
    double ans;
    long double t1,t2,c;
    int cas = 1;
    make_logF();

    while(~scanf("%d%lf",&n,&p)){
        ans = 0;
        for(int i = 0; i <= n; ++i){
            c = getC(2*n-i,n);
            t1 = c + (n+1)*log(p)+(n-i)*log(1-p);
            t2 = c + (n+1)*log(1-p)+(n-i)*log(p);
            ans += i*(exp(t1)+exp(t2));
        }
        printf("Case %d: %.6f\n",cas,ans);
        cas++;
    }

    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值