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 chessboard with N rows and M columns.
Every day after work, Edward will place a chess piece on a random empty cell. A few days later, he found the chessboard was dominated by the chess pieces. That means there is at least one chess piece in every row. Also, there is at least one chess piece in every column.
"That's interesting!" Edward said. He wants to know the expectation number of days to make an empty chessboard of N × M dominated. Please write a program to help him.
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
There are only two integers N and M (1 <= N, M <= 50).
For each test case, output the expectation number of days.
Any solution with a relative or absolute error of at most 10-8 will be accepted.
2 1 3 2 2
3.0000000000002.666666666667
題意:向一個N*M的棋盤裡隨機放棋子,每天往一個格子裡放一個。求每一行每一列
都有棋子覆蓋的天數。
思路:如下圖所示 ,dp[i][j][k]表示左上角有i行j列被k個棋子覆蓋的概率(不管
棋子放到哪,都可以移到左上角的相應地方,k就是區域1中叉的個數)。
當棋子放到區域1時: dp[i][j][k]=(r*c-k)/(n*m-k)*dp[i][j][k+1];<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD48cD61scbl19O3xbW9x/jT8jLKsaO6IGRwW2ldW2pdW2tdPShuLXIpKmMvKG4qbS1rKSpkcFtpJiM0MzsxXVtqXVtrJiM0MzsxXTsKPC9wPjxwPrWxxuXX07fFtb3H+NPyM8qxo7ogZHBbaV1bal1ba109cioobS1jKS8obiptLWspKmRwW2ldW2omIzQzOzFdW2smIzQzOzFdOwo8L3A+PHA+tbHG5dfTt8W1vcf40/I0yrGjuiBkcFtpXVtqXVtrXT0obi1yKSoobS1jKS8obiptLWspKmRwW2kmIzQzOzFdW2omIzQzOzFdW2smIzQzOzFdOzwvcD48cD7X7rrz16LS4s/Ct8W1vcO/uPbH+NPyttTTprXEzPW8/r7NT0vBy6GjPC9wPjxwPgo8L3A+PHA+CjwvcD48cD48cHJlIGNsYXNzPQ=="brush:java;">#include
#include #include using namespace std; const int maxn=55; double dp[maxn][maxn][maxn*maxn]; bool visited[maxn][maxn][maxn*maxn]; int n,m; double DP(int r,int c,int num) { if(visited[r][c][num]) return dp[r][c][num]; if(r==0 || c==0) return DP(r+1,c+1,num+1)+1; if(r>=n && c>=m) return 0; double ans=0.0,p=n*m-num,q=r*c-num; if(num