zoj3822

这题说得是给了一个n*m的棋盘,每天在这个棋盘中放置一个棋子,不能放在之前已经摆放过得地方,求最后使得每行每列都有至少一个棋子的期望天数是多少,这样我们考虑怎么放,放哪里,显然数据大而且不知道状态怎么表示, 考虑现在有i行j列放有k个棋子  这样我们要求的概率就是dp[n][m][k],表示n行m列有棋子棋子个数为k

那么 dp[i][j][k] 会从 1扩展行 2扩展列 3 同时扩展行和列,4 行列 都不扩展, 相应的求出其概率

#include<map>
#include<set>
#include<list>
#include<cmath>
#include<ctime>
#include<deque>
#include<stack>
#include<queue>
#include<cstdio>
#include<bitset>
#include<string>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<ctype.h>
#include<complex>
#include<fstream>
#include<iomanip>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
typedef long long LL;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
const int MAXN = 1e5 + 5;
const int INF = 0x7fffffff;
const double PI = acos(-1.0);
typedef unsigned long long uLL;

int n, m, ans = -1;
double dp[55][55][55 * 50];
int main()
{
    int cas;
    scanf("%d", &cas);
    while(cas--){
        int n, m;
        scanf("%d%d", &n, &m);
        memset(dp, 0, sizeof(dp));
        dp[1][1][1] = 1;
        int cnt = n*m;
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= m; ++j)
            for(int num = max(i, j); num <= i*j; ++num)
            {
                dp[i][j][num] += dp[i - 1][j][num - 1] * (n - i + 1)*j / (cnt - num + 1);
                dp[i][j][num] += dp[i][j - 1][num - 1] * (m - j + 1)*i / (cnt - num + 1);
                dp[i][j][num] += dp[i - 1][j - 1][num - 1] * (cnt - (i - 1)*m - (j - 1)*n + (i - 1)*(j - 1)) / (cnt - num + 1);
                if(i==n&&j==m) continue;
                dp[i][j][num] += dp[i][j][num - 1] * (i*j - num + 1) / (cnt - num + 1);
            }

        double ans = 0;
        int tt = n*m;// max(n*(m - 1) + 1, (n - 1)*m + 1);
        for(int i = 1; i <= tt; ++i)
            ans += dp[n][m][i] * i;
        printf("%.12lf\n", ans);

    }
    return 0;
}

时间: 2024-11-06 10:38:31

zoj3822的相关文章

ZOJ-3822

Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboar

ZOJ3822 ACM-ICPC 2014 亚洲区域赛牡丹江赛区现场赛D题Domination 概率DP(两种解法)

题目地址:点击打开链接 这道题有两种做法,第一种是直接求期望,类似于poj 2096 区别在于这个步数有限.所以要迭代步数. #include <cstdio> #include <cstring> #include <iostream> #define maxn 55//这里刚开始写成了50+10 那么maxn*maxn就会小很多wa了一次 using namespace std; double dp[maxn][maxn][maxn*maxn]; int N,M,T

ZOJ--3822(概率dp)

题意:一个n行m列的棋盘,每次可以放一个棋子,问要使得棋盘的每行每列都至少有一个棋子 需要的放棋子次数的期望. 思路: 定义三维的状态,dp[i][j][k]表示用k天占据了i行j列的概率. 下一天的概率分四种情况,一个是只占据了新的一行,只占据了新的一列,占据了新的一行和一列,并没有占据新的行和列. 初始化只用初始化dp[1][1][1]=1.0即可,第一个棋子放上肯定占据新的一行和一列,其它赋值为0即可. 求期望的时候要每次的概率要减去在k-1天就达到n*m的概率,再乘以天数,相加即可: 求

ZOJ3822——概率DP——Domination

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3822 /* dp[i][j][k]定义为覆盖了i行j列用了>=k个棋子的概率 状态转移方程 dp[i][j][k] = dp[i-1][j][k-1]*1.0*(j*(n-i+1))/(n*m-k+1) + dp[i][j-1][k-1]*1.0*i*(m-j+1)/(n*m-k+1) + dp[i-1][j-1][k-1]*1.0*(n-i+1)*(m-j+1)/(n*

ZOJ3822 Domination(14牡丹江 D) 概率DP

题意:给你一个N×M的棋盘,每一次随机在这里放一个子(不能重复)问你最后每一行每一列只要有一个子的期望次数 解题思路:dp[i][j][s] 已经用 i 个子 占了 j 行 s 列的概率,再找出状态转移方程就行. 解题代码: 1 // File Name: d.cpp 2 // Author: darkdream 3 // Created Time: 2014年10月18日 星期六 10时19分16秒 4 5 #include<vector> 6 #include<list> 7

zoj3822 Domination(概率dp)

Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboar

2014牡丹江区域赛D(概率DP)ZOJ3822

Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboar

zoj3822||牡丹江现场赛D题 概率dp

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5376 Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with

ZOJ3822---- Domination

Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboar