확률 DP

1275 단어
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 51
#define INF 1e10

double dp[maxn][maxn][maxn*maxn];
long long n, m;

int main () {
    int t;
    cin >> t;
    while (t--) {
        cin >> n >> m;
        memset (dp, 0, sizeof dp);
        dp[0][0][0] = 1.0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                long long all = m*n;
                for (int k = max (i, j); k <= i*j; k++) {
                    if (i == n && j == m) {}
                    else
                        dp[i][j][k] += dp[i][j][k-1]*(i*j-k+1)/(all-k+1);
                    dp[i][j][k] += dp[i-1][j-1][k-1]*(n-i+1)*(m-j+1)/(all-k+1);
                    dp[i][j][k] += dp[i-1][j][k-1]*(n-(i-1))*j/(all-k+1);
                    dp[i][j][k] += dp[i][j-1][k-1]*i*(m-(j-1))/(all-k+1);
                }
            }
        }
        double ans = 0.0;
        for (int i = 1; i <= m*n; i++) {
            ans += dp[n][m][i]*i;
            if (dp[n][m][i] == 1.0)
                break;
        }
        printf ("%.10lf
", ans); } return 0; }

좋은 웹페이지 즐겨찾기