http://codeforces.com/problemset/problem/518/D
题意:n个人,每秒有p的概率进电梯,求t秒后电梯里人数的期望
考虑dp:f[i][j]代表第i秒有j个人的概率,f[0][0]=1,f[i][j]=f[i-1][j-1]*p+f[i-1][j]*(1-p),特别有:f[i][n]=f[i-1][n]+f[i-1][n-1]*p
1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<cstring> 5 #include<iostream> 6 double p,f[2005][2005]; 7 int n,t; 8 int read(){ 9 int t=0,f=1;char ch=getchar(); 10 while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();} 11 while ('0'<=ch&&ch<='9'){t=t*10+ch-'0';ch=getchar();} 12 return t*f; 13 } 14 int main(){ 15 double p; 16 n=read();scanf("%lf",&p);t=read(); 17 f[0][0]=1; 18 for (int i=1;i<=t;i++){ 19 for (int j=0;j<n;j++) 20 f[i][j]=f[i-1][j]*(1-p)+f[i-1][j-1]*p; 21 f[i][n]=f[i-1][n]+f[i-1][n-1]*p; 22 } 23 double ans=0; 24 for (int i=1;i<=n;i++) 25 ans+=f[t][i]*i; 26 printf("%.6f ",ans); 27 return 0; 28 }