http://poj.org/problem?id=2096
概率DP:
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 double dp[1003][1003]; 5 int main() 6 { 7 int n,s,i,j; 8 cin>>n>>s; 9 for (i=n;i>=0;i--) 10 { 11 for (j=s;j>=0;j--) 12 { 13 if (i==n&&j==s) continue; 14 dp[i][j]=(n*s+(n-i)*j*dp[i+1][j]+dp[i][j+1]*i*(s-j)+dp[i+1][j+1]*(n-i)*(s-j))/(n*s-i*j); 15 } 16 } 17 printf("%0.4lf\n",dp[0][0]); 18 }
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 double dp[1003][1003]; 5 int main() 6 { 7 int n,s,i,j; 8 cin>>n>>s; 9 for (i=n;i>=0;i--) 10 { 11 for (j=s;j>=0;j--) 12 { 13 if (i==n&&j==s) continue; 14 dp[i][j]=(n*s+(n-i)*j*dp[i+1][j]+dp[i][j+1]*i*(s-j)+dp[i+1][j+1]*(n-i)*(s-j))/(n*s-i*j); 15 } 16 } 17 printf("%0.4lf\n",dp[0][0]); 18 }