# 数学期望/期望DP

还是戳《浅析竞赛中一类数学期望问题的解决方法》这篇论文……

$$f[i][j]= \begin{cases} 0 &, &i==0 \\ f[i-1][j]+1 &, &i>0,j=0 \\ max\{0,(f[i-1][j]+1)*\frac{i}{i+j} + (f[i][j-1]-1)* \frac{j}{i+j}\} &, &i>0,j>0 \end{cases}$$

然而TLE了一发……因为空间限制，所以只能开滚动数组，直接开$N^2$的挂了……

 1 /**************************************************************
2     Problem: 1419
3     User: Tunix
4     Language: C++
5     Result: Accepted
6     Time:2188 ms
7     Memory:1352 kb
8 ****************************************************************/
9
10 //BZOJ 1419
11 #include<cstdio>
12 #include<cstring>
13 #include<cstdlib>
14 #include<iostream>
15 #include<algorithm>
16 #define rep(i,n) for(int i=0;i<n;++i)
17 #define F(i,j,n) for(int i=j;i<=n;++i)
18 #define D(i,j,n) for(int i=j;i>=n;--i)
19 #define pb push_back
20 using namespace std;
21 const int N=5010;
22 /*******************template********************/
23
24 int n,m;
25 double f[2][N];
26
27 int main(){
28 #ifndef ONLINE_JUDGE
29     freopen("1419.in","r",stdin);
30     freopen("1419.out","w",stdout);
31 #endif
32     scanf("%d%d",&n,&m);
33     F(i,0,n) F(j,0,m){
34         int now=i&1;
35         if (i==0){f[now][j]=0;continue;}
36         if (j==0){f[now][j]=f[now^1][j]+1;continue;}
37         f[now][j]=max(0.0,(f[now^1][j]+1)*i/double(i+j)+(f[now][j-1]-1)*j/double(i+j));
38     }
39     printf("%.6f\n",f[n&1][m]-0.0000005);
40     return 0;
41 }
View Code

## 1419: Red is good

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 426  Solved: 144
[Submit][Status][Discuss]

5 1

4.166666

## Source

[Submit][Status][Discuss]
posted @ 2015-06-12 10:23  Tunix  阅读(277)  评论(0编辑  收藏  举报