Joyful
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1198 Accepted Submission(s): 528
Problem Description
Sakura has a very magical tool to paint walls. One day, kAc asked Sakura to paint a wall that looks like an
M×N
matrix. The wall has M×N
squares in all. In the whole problem we denotes (x,y)
to be the square at the x
-th
row, y
-th
column. Once Sakura has determined two squares (x
1
,y
1
)
and (x
2
,y
2
)
,
she can use the magical tool to paint all the squares in the sub-matrix which has the given two squares as corners.
However, Sakura is a very naughty girl, so she just randomly uses the tool for K
times. More specifically, each time for Sakura to use that tool, she just randomly picks two squares from all the
M×N
squares, with equal probability. Now, kAc wants to know the expected number of squares that will be painted eventually.
However, Sakura is a very naughty girl, so she just randomly uses the tool for K
Input
The first line contains an integer
T
(T≤100
),
denoting the number of test cases.
For each test case, there is only one line, with three integers M,N
and K
.
It is guaranteed that 1≤M,N≤500
,
1≤K≤20
.
For each test case, there is only one line, with three integers M,N
It is guaranteed that 1≤M,N≤500
Output
For each test case, output ''Case #t:'' to represent the
t
-th
case, and then output the expected number of squares that will be painted. Round to integers.
Sample Input
2 3 3 1 4 4 2
Sample Output
Case #1: 4 Case #2: 8HintThe precise answer in the first test case is about 3.56790123.题意:一个大矩形由m*n个小方块组成,一个人每次选两个小方块,他可以对以这两个方块为顶点的矩形面积内的小方块上色,一共可选k次,现在求被上色的小方块数目的期望
分析:每个格子可以被重复选,因此可以把每一个小方块选不选当做一个独立事件,所以我们算出每个小方块对总期望的贡献值就行了,直接算不好算,考虑算每个小方块一次不被选的概率p,用逆事件求小方格被上色的概率,遍历所有方格,不妨以当前方格为中心,那么只有四种情况,被选的两个小方块之间的区域同在中心的上下左右,不过这样会重复,四个角的矩形区域相当于被算了两次,再把它们减掉一次,这样求出来的是当前点一次不会被上色的情况数,除以总情况数(m * n) *( m * n)就是当前方块一次不会被上色的概率,做k次方,就是k次这个方块都不被上色的概率,用1减则为选k次这个方块被上色的概率,只需要把每个方块选k次后被上色的概率累加即可,情况数中间可能会爆int,用double就行#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <algorithm> #include <vector> #include <map> #include <string> #include <stack> using namespace std; typedef long long ll; #define PI 3.1415926535897932 #define E 2.718281828459045 #define INF 0xffffffff//0x3f3f3f3f #define mod 100 const int M=1005; ll n,m; int cnt; int sx,sy,sz; int mp[1000][1000]; int pa[M*10],rankk[M]; int head[M*6],vis[M*100]; int dis[M*100]; int prime[M*1000]; bool isprime[M*1000]; int lowcost[M],closet[M]; char st1[5050],st2[5050]; //int len[M*6]; typedef pair<int ,int> ac; int dp[55][55][55][55]; int has[1050000]; int month[13]= {0,31,59,90,120,151,181,212,243,273,304,334,0}; int dir[8][2]= {{0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1}}; int main() { int i,j,t; ll k; int cas=1; double ans; scanf("%d",&t); while(t--) { scanf("%lld%lld%lld",&m,&n,&k); double tol=1.0*(n*m)*(m*n); ans=0.0; for(i=1; i<=m; i++) { for(j=1; j<=n; j++) { double tmp=0; tmp+=1.0*(i-1)*n*(i-1)*n+1.0*(j-1)*m*(j-1)*m+1.0*(m-i)*n*(m-i)*n+1.0*(n-j)*m*(n-j)*m; tmp-=1.0*(i-1)*(i-1)*(j-1)*(j-1)+1.0*(i-1)*(i-1)*(n-j)*(n-j)+1.0*(j-1)*(j-1)*(m-i)*(m-i)+1.0*(m-i)*(m-i)*(n-j)*(n-j); double p=1.0*tmp/tol; p=pow(p,k); ans+=(1.0-p); } } printf("Case #%d: %d\n",cas++,(int)(ans+0.5)); } return 0; }