Description
Sakura has a very magical tool to paint walls. One day, kAc asked Sakura to paint a wall that looks like an M×NM×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 (x1,y1)and (x2,y2), 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×NM×N squares, with equal probability. Now, kAc wants to know the expected number of squares that will be painted eventually.
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.
Output
For each test case, output ”Case #t:” to represent the tt-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: 8
题目大意
在一个M*N的矩形格子中涂格子,Sakura有 k 次机会,每次可以选择矩形中的两个格子,然后两个格子所在的矩形范围会全部被涂满,问最后涂格子数目的期望是多少。
解题思路
涂格子数目的期望可以转化为涂每个格子的概率之和,则问题就转化为求每个格子被涂色的概率。
如图所示,我们把M*N的矩形分成 9 部分,假设⑤(i,j)是我们当前要求的格子涂色的概率,那么它被涂到的种数可以分为九种情况来进行计算,最后再把每种情况求和,得到总种数:
1.第一次选点就在⑤,那么下一次选点不论选在哪个区域,涂色时⑤都可以被涂到;
2.第一次选点在①区域,那么下一次选点在⑤⑥⑧⑨四个区域,涂色时可以涂到⑤;
3.第一次选点在②区域,那么下一次选点在④⑤⑥⑦⑧⑨六个区域,涂色时可以涂到⑤;
4.第一次选点在③区域,那么下一次选点在④⑤⑦⑧四个区域,涂色时可以涂到⑤;
5.第一次选点在④区域,那么下一次选点在②③⑤⑥⑧⑨六个区域,涂色时可以涂到⑤;
6.第一次选点在⑥区域,那么下一次选点在①②④⑤⑦⑧六个区域,涂色时可以涂到⑤;
7.第一次选点在⑦区域,那么下一次选点在②③⑤⑥四个区域,涂色时可以涂到⑤;
8.第一次选点在⑧区域,那么下一次选点在①②③④⑤⑥六个区域,涂色时可以涂到⑤;
9.第一次选点在⑨区域,那么下一次选点在①②④⑤四个区域,涂色时可以涂到⑤;
此时我们可以得到涂到此点的总的种类数,代码中记为了 p ,因为两次选点总的种类数为M*N*M*N,则该点被涂到的概率为p=
pM∗N∗M∗N
,则该点 1 次涂色之后不被涂到的概率为(1-p),则该点 k 次涂色之后不被涂到的概率为
(1−p)k
,则该点 k 次涂色被涂到的概率为
1−(1−p)k
,然后遍历每个点,累加求和即可。
代码实现
#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
#define ll long long
int k;
double m,n;
double ans;
int main()
{
int T;
scanf("%d",&T);
for(int num=1; num<=T; num++)
{
scanf("%lf %lf %d",&m,&n,&k);
ans=0;
for(int i=1; i<=m; i++)
{
for(int j=1; j<=n; j++)
{
double p=m*n; //5
p+=(i-1)*(j-1)*(m-i+1)*(n-j+1); //1
p+=(i-1)*(n-j)*(m-i+1)*j; //3
p+=(j-1)*(m-i)*(n-j+1)*i; //7
p+=i*j*(m-i)*(n-j); //9
p+=(i-1)*(m-i+1)*n; //2
p+=(j-1)*(n-j+1)*m; //4
p+=(n-j)*(j)*m; //6
p+=(m-i)*i*n; //8
p=p/m/m/n/n;
ans+=1-pow(1-p,k);
}
}
printf("Case #%d: %d\n",num,(int)(ans+0.5));
}
return 0;
}