UVA 11176 - Winning Streak

Problem B
Winning Streak
Input: 
Standard Input

Output: Standard Output

 

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

– Traditional folk song

Mikael likes to gamble, and as you know, you can place bets on almost anything these days. A particular thing that has recently caught Mikael'sinterest is the length of the longest winning streak of a team during a season (i.e. the highest number of consecutive games won). In order to be able to make smarter bets, Mikael has asked you to write a program to help him compute the expected value of the longest winning streak of hisfavourite teams.

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

The expected value of the longest streak is the average of the longest streak in all possible outcomes of all games in a season, weighted by their probability. For instance, assume that the season consists of only three games, and that p = 0.4. There are eight different 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 first and the third game, but lost the second). The possible results of the season are:

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 longest winning 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 number of games in a season, and a floating point number 0 ≤p ≤ 1, the win probability. Input is terminated by a case where n = 0, which should not be processed.

 

Output

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

Sample Input                               Output for Sample Input

3 0.4
10 0.75
0 0.5

 

1.104000
5.068090

 


Problem setter: Per Austrin

Special Thanks: Mikael Goldmann

 

思路:直接dp吧....我是用f[i][j]表示前i个中最后一个是W,并且最长的W是j的概率,g[i][j]表示前i个中最后一个是L,并且最长的W是j的概率。 然后直接转移一下就是了....当然不能用n^3的转移啦,有一维可以省掉的,因为能够通过前缀和啊,类似等比之类的东西直接求出来。


代码:

<span style="font-family:FangSong_GB2312;">#include <iostream>
#include <string.h>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)
#define rrep(i,b,a) for(int i=(b);i>=(int)(a);--i)
#define clr(a,x) memset(a,x,sizeof(a))
#define ll long long
#define eps 1e-13
const int maxn = 500 + 5;
double f[maxn][maxn], g[maxn][maxn];
double h[maxn][maxn],sum[maxn][maxn];
double pw[maxn];

int main()
{
    #ifdef ACM
        freopen("in.txt","r",stdin);
    #endif
    double p; int n;
    while (scanf("%d%lf",&n,&p),n) {
        pw[0] = 1;
        rep(i,1,n+1) pw[i] = pw[i-1] * p;
        clr(f,0); clr(g,0); clr(h,0); clr(sum,0);
        g[0][0] = 1.0;
        h[0][0] = p; rep(i,1,n+1) h[i][0] = h[i-1][0] * p;
        sum[0][0] = 1.0; rep(i,1,n+1) sum[0][i] = sum[0][i-1];
        rep(i,1,n+1) {
            g[i][0] += (g[i-1][0] + f[i-1][0]) * (1.0 - p);
            rep(j,1,i+1) {
                g[i][j] += (g[i-1][j] + f[i-1][j]) * (1.0 - p);
                double q = p;
                f[i][j] += (h[i-1][j] - h[i-j][j] * pw[j-1] + sum[i-j][j] * pw[j]);
//                rep(k,1,j) {
//                    f[i][j] += g[i-k][j] * q;
//                    q *= p;
//                }
//                rep(k,0,j+1) f[i][j] += g[i-j][k] * q;
            }
            rep(j,0,n+1) {
                sum[i][j] = (j > 0 ? sum[i][j-1] : 0) + g[i][j];
                h[i][j] = h[i-1][j] * p + g[i][j] * p;
            }
        }
        double ans = 0;
        rep(i,1,n+1) ans += i * (f[n][i] + g[n][i]);
        printf("%.10f\n",ans);
    }
}

</span>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值