有两个盒子各有n (n<=2e5) 个糖,每天随机选一个(概率分别为p,1-p),然后吃一颗糖。直到有一天,没糖了!输入n,p,求此时另一个盒子里糖的个数的数学期望
假设最后某个盒子有 k 颗糖,然后计算概率即可
#include<iostream> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int N =2e5; int n; double ans , p,q; long double v[N*2]; signed main(){ int i,cas=0; for(i=1;i<=4e5;i++) v[i] = v[i-1]+log(i); while(cin>>n>>p){ ans=0.0; q=log(1-p); p=log(p); for(i=0;i<=n;i++){ double t1= v[2*n-i] -v[n] -v[n-i]+ (n+1)*p+(n-i)*q; double t2= v[2*n-i]-v[n]-v[n-i]+ (n+1)*q+ (n-i)*p; // cout <<"t1 t2: "<<t1<<' '<<t2<<endl; ans+= i*(exp(t1)+exp(t2)); } printf("Case %d: %.6lf\n",++cas,ans); } return 0; }
标签:log,uva1639,Candy,int,ans,include,糖果 From: https://www.cnblogs.com/towboa/p/17300664.html