uva 1639 Candy 大数的对数处理 数学期望
发布时间:2021-01-23 00:28:41 所属栏目:大数据 来源:网络整理
导读:当排列组合数或者幂很大时可以利用对数计算,之后再用exp还原,保证一定的精度。 数学期望是每一个可能的值和相应的概率的乘积和,没有可能值可以设。 仔细读题,吃完最后一个糖果后不知道是否已经吃完,所以需要再选一次。 %f用来输入float,输出float doubl
题目链接: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: %.6fn",cas,ans); cas++; } return 0; } (编辑:威海站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |