设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 数据 创业者 手机
当前位置: 首页 > 大数据 > 正文

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

发布时间:2021-01-21 14:39 所属栏目:125 来源:网络整理
导读:当排列组合数或者幂很大时可以利用对数计算,之后再用exp还原,保证一定的精度。 数学期望是每一个可能的值和相应的概率的乘积和,没有可能值可以设。 仔细读题,吃完最后一个糖果后不知道是否已经吃完,所以需要再选一次。 %f用来输入float,输出float doubl

  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,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;
}

(编辑:ASP站长网)

    网友评论
    推荐文章
      热点阅读