uva11176 - Winning Streak 数学期望 DP

来源:互联网 发布:mac osx使用教程 编辑:程序博客网 时间:2024/05/18 16:54

Problem B
Winning Streak
Input:
Standard Input

Output: Standard Output

 

"You can run onfor a long time,
sooner or later God'll cut you down."

Traditional folksong

Mikael likes to gamble,and as you know, you can place bets on almost anything these days. A particularthing that has recently caught Mikael's interest isthe length of the longest winning streak of a team during a season (i.e. thehighest number of consecutive games won). In order to be able to make smarterbets, Mikael has asked you to write a program to helphim compute the expected value of the longest winning streak of his favourite teams.

In general, the probability that a team wins agame depends on a lot of different factors, such as whether they're the hometeam, whether some key player is injured, and so on. For the first prototype ofthe program, however, we simplify this, and assume that all games have the samefixed probability p of being won, and that the result of a game doesnot affect the win probability for subsequent games.

The expected value of the longest streak is theaverage of the longest streak in all possible outcomes of all games in aseason, weighted by their probability. For instance, assume that the seasonconsists of only three games, and thatp = 0.4. There are eightdifferent outcomes, which we can represent by a string of 'W':s and 'L':s,indicating which games were won and which games were lost (for example, 'WLW' indicates that the team won the firstand the third game, but lost the second). The possible results of the seasonare:

Result

LLL

LLW

LWL

LWW

WLL

WLW

WWL

WWW

Probability

0.216

0.144

0.144

0.096

0.144

0.096

0.096

0.064

Streak

0

1

1

2

1

1

2

3

In this case, the expected length of the longestwinning streak becomes 0.216·0 + 0.144·1 + 0.144·1 + 0.096·2 + 0.144·1 +0.096·1 + 0.096·2 + 0.064·3 = 1.104

Input

Several test cases (at most 40),each containing an integer 1 ≤ n ≤ 500 giving the numberof games in a season, and a floating point number 0 ≤p ≤1, the win probability. Input is terminated by a case wheren = 0,which should not be processed.

 

Output

For each test case, give the expected length of the longestwinning streak. The answer should be given as a floating point number with anabsolute error of at most 10-4.

SampleInput                              Output for Sample Input

3 0.4
10 0.75
0 0.5

 

1.104000
5.068090

 


  N个字母,只能是W和L,出现W的概率是P,出现L的概率是1-P,streak是这N个字母中W最长连续个数,问streak的期望。

  话说这个题真的很难想啊。。

  用dp[i][j]表示前i个字母streak不超过j的概率。那是不是dp[i][j]就等于dp[i-1][j]减去其中由于增加了第i个而使streak大于j的情况的概率。也就是第i-j到第i个(共j+1个)都是W,i-j-1位置上是L,i-j-1之前的最长连续不超过j。也就是dp[i][j]=dp[i-1][j]-P^(j+1)*(1-P)*dp[i-j-2][j],当然这是在j+1<i的情况下,如果j+1==i,dp[i][j]=dp[i-1][j]-P^(j+1),如果j+1>i,dp[i][j]=dp[i-1][j]。最后dp[N][i]-dp[N][i-1]就是streak为i的概率。

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<cctype>#include<algorithm>#define INF 0x3f3f3f3fusing namespace std;int N;double P,a[510],dp[510][510];void init(){    a[0]=1;    for(int i=1;i<=N;i++) a[i]=a[i-1]*P;}void DP(){    int i,j;    for(i=0;i<=N;i++) dp[0][i]=1;    for(i=1;i<=N;i++)        for(j=0;j<=N;j++){            dp[i][j]=dp[i-1][j];            if(i-j==1) dp[i][j]-=a[i];            else if(j+1<i) dp[i][j]-=a[j+1]*(1-P)*dp[i-j-2][j];        }}int main(){    freopen("in.txt","r",stdin);    while(scanf("%d%lf",&N,&P),N){        double ans=0;        init();        DP();        for(int i=1;i<=N;i++) ans+=(dp[N][i]-dp[N][i-1])*i;        printf("%.6lf\n",ans);    }    return 0;}


0 0
原创粉丝点击