zoj 3822 Domination(dp)

dp[i][j][k]表示i行j列上均有至少一枚棋子，并且消耗k步的概率（kij）,因为放置在i+1~n上等价与放在i+1行上，同理列也是如此。所以有转移方程：

• dp[i][j][k+1]+=dp[i][j][k](nk)(Sk)
• dp[i+1][j][k+1]+=dp[i][j][k](Ni)j(Sk)
• dp[i][j+1][k+1]+=dp[i][j][k](Mj)i(Sk)
• dp[i+1][j+1][k+1]+=dp[i][j][k](Ni)(Mj)(Sk)
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 55;
const int maxm = 2505;

int N, M;
double dp[maxn][maxn][maxm];

double solve () {
int S = N * M;
memset(dp, 0, sizeof(dp));
dp[1][1][1] = 1;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
int n = i * j;
for (int k = max(i, j); k <= n; k++) {
dp[i][j][k+1] += dp[i][j][k] * (n - k) / (S - k);
dp[i+1][j][k+1] += dp[i][j][k] * (N - i) * j / (S - k);
dp[i][j+1][k+1] += dp[i][j][k] * (M - j) * i / (S - k);
dp[i+1][j+1][k+1] += dp[i][j][k] * (N - i) * (M - j) / (S - k);
}
}
}

/* for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) { printf("%d %d:", i, j); for (int k = max(i, j); k <= i * j; k++) printf("%.3lf ", dp[i][j][k]); printf("\n"); } } */

double ans = 0;
for (int i = max(N, M); i <= S; i++)
ans += (dp[N][M][i] - dp[N][M][i-1]) * i;
return ans;
}

int main () {
int cas;
scanf("%d", &cas);
while (scanf("%d%d", &N, &M) == 2) {
printf("%.8lf\n", solve());
}
return 0;
}

zoj 3822 Domination(dp)

• 0

开心

• 0

板砖

• 0

感动

• 0

有用

• 0

疑问

• 0

难过

• 0

无聊

• 0

震惊