传送门:http://codeforces.com/problemset/problem/398/B
Description:
User ainta decided to paint a wall. The wall consists of
n2 tiles, that are arranged in an
n?×?n table. Some tiles are painted, and the others are not. As he wants to paint it beautifully, he will follow the rules below.
- Firstly user ainta looks at the wall. If there is at least one painted cell on each row and at least one painted cell on each column, he stops coloring. Otherwise, he goes to step 2.
- User ainta choose any tile on the wall with uniform probability.
- If the tile he has chosen is not painted, he paints the tile. Otherwise, he ignores it.
- Then he takes a rest for one minute even if he doesn‘t paint the tile. And then ainta goes to step 1.
However ainta is worried if it would take too much time to finish this work. So he wants to calculate the expected time needed to paint the wall by the method above. Help him find the expected time. You can assume that choosing and painting any tile consumes
no time at all.
中文:涂墙壁游戏,要求每row每colum都至少一瓦被paint。
Analyse:
期望dp:从终点状态,往前面推;
dp(i,j)=(dp(i,j)+1)*p1+(dp(i-1,j)+1)*p2+(dp(i,j-1)+1)*p3+(dp(i-1,j-1)+1)*p4;
然后移项,dp(i,j)=.........详见CODE。=_=|
CODE:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<string> #include<queue> #include<deque> #include<stack> #include<map> #include<set> #define INF 0x7fffffff #define SUP 0x80000000 #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long LL; const int N=2007; set<int> vr,vc; double dp[N][N],p; int main() { int n,m; while(scanf("%d%d",&n,&m)==2) { vr.clear(),vc.clear(); p=1.0/n/n; int r,c; for(int i=0;i<m;i++){ scanf("%d%d",&r,&c); if(!vr.count(r)) vr.insert(r); if(!vc.count(c)) vc.insert(c); } r=vr.size(); c=vc.size(); dp[0][0]=0; for(int i=0;i<=n-r;i++){ for(int j=0;j<=n-c;j++){ double tmp=0; if(i==0&&j==0) continue; tmp+=p*(n-i)*(n-j); dp[i][j]=1-tmp; if(i>0) tmp+=(dp[i-1][j]+1)*p*(n-j)*i; if(j>0) tmp+=(dp[i][j-1]+1)*p*(n-i)*j; if(i>0&&j>0) tmp+=(dp[i-1][j-1]+1)*p*i*j; dp[i][j]=tmp/dp[i][j]; } } printf("%.10f\n",dp[n-r][n-c]); } return 0; }
时间: 2024-09-28 12:46:55