cdoj 邱老师看电影

//第一次写概率dp

//写成记忆化搜索的形式比递推要更方便易懂

//不过好像还是可以写成递推的形式的 但是比较那个……

 1 #include<cstdio>

 2 #include<iostream>

 3 #include<cmath>

 4 #include<algorithm>

 5 #include<cstring>

 6 #include<cstdlib>

 7 #include<queue>

 8 #include<vector>

 9 #include<map>

10 #include<stack>

11 #include<string>

12 

13 using namespace std;

14 

15 int w,b;

16 double f[1007][1007];

17 

18 double dfs(int w,int b){

19     if (w==0) return 0;

20     if (b==0) return 1;

21     if (f[w][b]>=0) return f[w][b];

22     f[w][b]=(double)w/(double)(w+b);

23     if (w+b>=3){

24             double tmp=((double)(b)/double(w+b))*((double)(b-1)/(double)(w+b-1));

25             if (b>=3) f[w][b]=f[w][b]+(tmp*((double)(b-2)/(double)(w+b-2))*dfs(w,b-3));

26             if (w>=1) f[w][b]=f[w][b]+(tmp*((double)(w)/(double)(w+b-2))*dfs(w-1,b-2));

27     }

28     return f[w][b];

29 }

30 

31 int main(){

32     scanf("%d%d",&w,&b);

33     for (int i=0;i<=w;i++){

34             for (int j=0;j<=b;j++){

35                     f[i][j]=-1;

36             }

37     }

38     printf("%.9lf\n",dfs(w,b));

39     return 0;

40 }

41 /*

42 1 3

43 */

 

你可能感兴趣的:(电影)