codeforces 518 D. Ilya and Escalator

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

题意:有n个人排队上一个电梯。。。在某一秒内,队首的人有p的概率上电梯,1-p的概率不动。每个人只有在队首的位置才可以上电梯(也就是每一秒内,最多只有一个人可以上电梯)。电梯无线长(也就是上了电梯就不会离开了),问在第t秒的时候,电梯上的人的个数的数学期望是多少。

思路:一开始推公式的我还是图样。这题是dp.其实也不难想。dp[i][j]表示第i秒时电梯上有j个人的概率。 当j==n的时候,也就是所以人都上了电梯以后。dp[i+1][j]+=dp[i][j],对于其他时刻 dp[i+1][j+1]+=dp[i][j]p,dp[i+1][j]+=dp[i][j](1-p).  初始化dp[0][0]=1,即0时刻电梯上有0个人的概率为1.

/* ***********************************************
Author :111qqz
Created Time :2016年02月02日 星期二 15时57分06秒
File Name :code/cf/518D.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6const int N=2E3+7;
 7int n,t;
 8double p;
 9double dp[N][N];
10int main()
11{
12	#ifndef  ONLINE_JUDGE 
13	freopen("code/in.txt","r",stdin);
14  #endif
15	ms(dp,0);
16	dp[0][0] = 1;
17	cin>>n>>p>>t;
18	for ( int i = 0 ; i <= t ; i++)
19	{
20	    for ( int j = 0 ; j <= n ; j++)
21	    {
22		if (j==n)
23		{
24		    dp[i+1][j]+=dp[i][j];
25		}
26		else
27		{
28		    dp[i+1][j+1]+=dp[i][j]*p;
29		    dp[i+1][j]+=dp[i][j]*(1-p);
30		}
31	    }
32	}
33	double ans = 0 ;
34	for ( int j = 1 ; j <= n ; j++)
35	    ans +=j*dp[t][j];
	printf("%.12f\n",ans);
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}