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 (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×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≤), 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 ≤M,N≤, ≤K≤.
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
Sample Output
Case #:
Case #:
Hint
The precise answer in the first test case is about 3.56790123.
Source
题意大致是:进行K次染色,每次染色会随机选取一个以(x1,y1),(x2,y2)为一组对角的子矩阵进行染色,求K次染色后染色面积的期望值(四舍五入)。
对于这道题的话,首先要考虑的是进行一次选择时的期望。求期望的方法为单独考虑每一格所能获得的期望,然后将所有格的期望相加即为答案。
对于每一个所能获得的期望,即要计算所有包含这一格的个数ans,除于总的选择方案tot
此时我们的问题转向了如何计算A[x.y]上
由题目描述,一次染色中可能的操作有n^2*m^2种
计算A[x,y]时,我们可以把整个矩阵做如下拆分
当前计算的方块为[x,y],即图中编号为5的部分
将其他部分拆分成图上8个区域,则可得到以下关系
对于一种染色方案能够覆盖方块[x,y]时
①[x1,y1]取在区域1内时,[x2,y2]可以在5、、、9四个区域内任取;
②[x1,y1]取在区域2内时,[x2,y2]可以在4、、、、、9六个区域内任取;
③[x1,y1]取在区域3内时,[x2,y2]可以在4、、、8四个区域内任取;
④[x1,y1]取在区域4内时,[x2,y2]可以在2、、、、、9六个区域内任取;
⑤[x1,y1]取在区域5内时,[x2,y2]可以在所有区域内任取;
⑥[x1,y1]取在区域6内时,[x2,y2]可以在1、、、、、8六个区域内任取;
⑦[x1,y1]取在区域7内时,[x2,y2]可以在2、、、6四个区域内任取;
⑧[x1,y1]取在区域8内时,[x2,y2]可以在1、、、、、6六个区域内任取;
⑨[x1,y1]取在区域1内时,[x2,y2]可以在1、、、5四个区域内任取;
计算出这个格子的概率p后,总的答案加上 1-pow(1-p,k),得到最后的答案
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<stdlib.h>
#include<queue>
using namespace std;
#define ll long long
int m,n,k;
int main()
{
int t;
int ac=;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&k);
double ans=;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
double tmp=;
tmp=tmp+(double)(i-)*(j-)*(n-i+)*(m-j+);//
tmp=tmp+(double)(i-)*(n-i+)*m;//
tmp=tmp+(double)(i-)*(m-j)*(n-i+)*j;//
tmp=tmp+(double)(m-j)*n*j;//
tmp=tmp+(double)n*m;//
tmp=tmp+(double)(j-)*n*(m-j+);//
tmp=tmp+(double)(n-i)*(j-)*i*(m-j+);//
tmp=tmp+(double)(n-i)*i*m;//
tmp=tmp+(double)(n-i)*(m-j)*i*j;// double p=tmp/n/n/m/m;
ans=ans+-pow((-p),k); }
}
printf("Case #%d: ",++ac);
printf("%d\n",int(ans+0.5));
}
return ;
}