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.

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