POJ--2096(概率DP)

2014-12-26 00:51:46

思路:逆推....

  定义dp[i][j]为已经有i个cate..,j个system,还需要期望多少天来达到目标。

  递推过程见注释..^.^

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <vector>
 6 #include <map>
 7 #include <set>
 8 #include <stack>
 9 #include <queue>
10 #include <iostream>
11 #include <algorithm>
12 using namespace std;
13 #define lp (p << 1)
14 #define rp (p << 1|1)
15 #define getmid(l,r) (l + (r - l) / 2)
16 #define MP(a,b) make_pair(a,b)
17 typedef long long ll;
18 typedef unsigned long long ull;
19 const int INF = 1 << 30;
20 const int maxn = 1010;
21 
22 int n,s;
23 double dp[maxn][maxn];
24 
25 int main(){
26     //dp[i][j] : i cate.. , j systems : remaining expected days
27     //->dp[i][j] : (i/n)*(j/s)
28     //->dp[i][j+1] : (i/n)*(1-j/s)
29     //->dp[i+1][j] : (1-i/n)*(j/s)
30     //->dp[i+1][j+1] : (1-i/n)*(1-j/s)
31     while(scanf("%d%d",&n,&s) != EOF){
32         dp[n][s] = 0;
33         for(int i = n; i >= 0; --i){
34             for(int j = s; j >= 0; --j){
35                 if(i == n && j == s) continue;
36                 dp[i][j] = (dp[i][j+1]*(i*(s-j))+dp[i+1][j]*(n-i)*j+dp[i+1][j+1]*(n-i)*(s-j)+n*s)/(n*s-i*j);
37             }
38         }
39         printf("%.4f\n",dp[0][0]);
40     }
41     return 0;
42 }

 

posted @ 2014-12-26 00:54  Naturain  阅读(100)  评论(0编辑  收藏  举报