Codeforces Round #293 (Div. 2)D.Ilya and Escalator——概率dp

http://codeforces.com/contest/518/problem/D

dp[i][j] 表示第i秒有j个人进入电梯的概率

#include<bits/stdc++.h>
const int maxn=2100;
using namespace std;
double dp[maxn][maxn];
int n,t;
double p;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
    scanf("%d%lf%d",&n,&p,&t);
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    for(int i=1;i<=t;++i){
        for(int j=n;j>=0;--j){
            if(j==n)
                dp[i][j]=dp[i-1][j-1]*p+dp[i-1][j];
            else if(j!=0)
                dp[i][j]=dp[i-1][j-1]*p+dp[i-1][j]*(1-p);
            else
                dp[i][j]=dp[i-1][j]*(1-p);
        }
    }
    double ans=0;
    for(int i=1;i<=n;++i){
        ans+=(dp[t][i]*i);
    }
    printf("%.7lf\n",ans);
    return 0;
}

你可能感兴趣的:(Codeforces Round #293 (Div. 2)D.Ilya and Escalator——概率dp)