D. Ilya and Escalator (thinking + probability dp)

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


Ideas:

Define dp[t][j] as the probability that the person j gets on the elevator by t seconds

Transfer:

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

Boundary: when j==0, the transfer still needs *(1-p)

When j==n, the transfer of the former is 100%

The answer is 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;
}

 

Guess you like

Origin blog.csdn.net/zstuyyyyccccbbbb/article/details/114898216