程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> ZOJ 3822 Domination (概率DP)

ZOJ 3822 Domination (概率DP)

編輯:關於C++
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 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.

Input

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).

Output

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.

Sample Input

2
1 3
2 2

Sample Output

3.000000000000

2.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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved