【题目链接】
http://poj.org/problem?id=2096
【算法】
概率DP
【代码】
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; #define MAXN 1010 int i,j,n,s; double f[MAXN][MAXN]; int main() { while (scanf("%d%d",&n,&s) != EOF) { memset(f,0,sizeof(f)); f[n][s] = 0; for (i = n; i >= 0; i--) { for (j = s; j >= 0; j--) { if (i == n && j == s) continue; f[i][j] = (1.0 * i * (s - j) * f[i][j+1] + 1.0 * (n - i) * j * f[i+1][j] + 1.0 * (n - i) * (s - j) * f[i+1][j+1] + n * s) / (n * s - i * j); } } printf("%.4lf ",f[0][0]); } return 0; }