Codeforces 518D Ilya and Escalator

来源:互联网 发布:创度软件下载 编辑:程序博客网 时间:2024/06/05 14:31

D. Ilya and Escalator
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ilya got tired of sports programming, left university and got a job in the subway. He was given the task to determine the escalator load factor.

Let's assume that n people stand in the queue for the escalator. At each second one of the two following possibilities takes place: either the first person in the queue enters the escalator with probability p, or the first person in the queue doesn't move with probability (1 - p), paralyzed by his fear of escalators and making the whole queue wait behind him.

Formally speaking, the i-th person in the queue cannot enter the escalator until people with indices from 1 to i - 1 inclusive enter it. In one second only one person can enter the escalator. The escalator is infinite, so if a person enters it, he never leaves it, that is he will be standing on the escalator at any following second. Ilya needs to count the expected value of the number of people standing on the escalator after t seconds.

Your task is to help him solve this complicated task.

Input

The first line of the input contains three numbers n, p, t (1 ≤ n, t ≤ 20000 ≤ p ≤ 1). Numbers n and t are integers, number p is real, given with exactly two digits after the decimal point.

Output

Print a single real number — the expected number of people who will be standing on the escalator after t seconds. The absolute or relative error mustn't exceed 10 - 6.

Sample test(s)
input
1 0.50 1
output
0.5
input
1 0.50 4
output
0.9375
input
4 0.20 2
output
0.4



题目链接:http://codeforces.com/problemset/problem/518/D


题目大意:有n个人的队列等待上自动扶梯,每秒最多上一个人,每个人在每秒上扶梯的概率是p,求t秒时扶梯上人数的数学期望。


解题思路:扶梯上人数的数学期望=扶梯上人数的概率*人数。dp [ i ] [ j ]表示第i秒有j个人的概率。第i秒最多有i个人,如果i>=n,第i秒最多有n个人。

对于( i = 2 - t ), dp [ i ] [ j ]=dp [ i -1 ] [ j ]*(1-p)。

对于( j = 1 - n )dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] * p。

对于( i >n )  dp [ i ] [ n ] = dp[ i - 1 ] [ n ] + dp [ i-1 ] [ n-1 ]*p。

利用这几个公式 ,注意初始化和边界条件,详见代码。


代码如下:


#include <cstdio>#include <cmath>#include <cstring>const int maxn=2005;double dp[maxn][maxn];//第i秒上了j个人的概率int main(){    int i,n,t,j;    double p,ans=0.0;memset(dp,0,sizeof(dp));    scanf("%d%lf%d",&n,&p,&t);    dp[1][0]=1-p; // 初始化第一秒有0个人的概率为(1-p)dp[1][1]=p;  // 初始化第一秒有1个人的概率为 p    for(i=2;i<=t;i++)   //初始化j为0的所有情况,如果t>n,最后累加时j只到n,所以这里不做处理        dp[i][0]=dp[i-1][0]*(1-p);    for(i=2;i<=t;i++)    {        for(j=1;j<=n;j++){if(j>t)break;   //如果j>t,t秒最多有t个人            dp[i][j]=dp[i-1][j-1]*p+dp[i-1][j]*(1-p);}        dp[i][n]=dp[i-1][n]+dp[i-1][n-1]*p; //初始化所有j为n的情况,如果i-1>n,i秒最多有n个人,如果i-1<n,dp[i-1][n]的最初初始化是为0 的,不用考虑。    }    for(j=0;j<=n;j++)    {        if(j>t)            break;        ans+=j*dp[t][j];   //累加t秒有j个人的数学期望    }    printf("%f\n", ans);    return 0;}


0 0
原创粉丝点击