D. Ilya and Escalator(思维+概率dp)

https://codeforces.com/problemset/problem/518/D


思路:

定义dp[t][j]为到t秒j个人上了电梯的概率

转移:

dp[t][j]=dp[t-1][j-1]*p+dp[t-1][j]*(1-p);

边界:j==0的时候转移仍然要*(1-p)

j==n的时候前者的转移是100%的

答案是ans+=dp[t][j:0~n]*j

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e3+100;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
double dp[maxn][maxn];///到t秒 j个人上了电梯的概率
int main(void)
{
  ///cin.tie(0);std::ios::sync_with_stdio(false);
  LL n,t;double p;
  n=read();
  scanf("%lf",&p);
  t=read();
  dp[1][0]=1.0-p;
  dp[1][1]=1.0*p;
  for(LL i=2;i<=t;i++){///枚举时间


     for(LL j=0;j<=n;j++){///枚举人

        if(j==0) dp[i][j]=dp[i-1][j]*(1-p);
        else if(j==n) dp[i][j]=dp[i-1][j]+dp[i-1][j-1]*p;
        else dp[i][j]=dp[i-1][j-1]*p+dp[i-1][j]*(1.0-p);
     }
  }
  double ans=0;
  for(LL j=0;j<=n;j++){
        ans+=1.0*j*dp[t][j];
  }
  printf("%.8f\n",ans);
return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/114898216