Coloring problem HDU_5245 Mathematical expectations

Joyful

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 478    Accepted Submission(s): 209


Problem Description
Sakura has a very magical tool to paint walls. One day, kAc asked Sakura to paint a wall that looks like an matrix. The wall has squares in all. In the whole problem we denotes to be the square at the -th row, -th column. Once Sakura has determined two squares and , 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 times. More specifically, each time for Sakura to use that tool, she just randomly picks two squares from all the 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(), denoting the number of test cases.

For each test case, there is only one line, with three integers and .
It is guaranteed that ,.
 

Output
For each test case, output ''Case #t:'' to represent the-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

The main idea of ​​the title is to give a matrix of m*n, select a random sub-matrix for coloring each time, and ask the expected value of the number of colored grids after k times of coloring.

Idea: The expected value of the number of dyed grids = the sum of the probability of each grid being dyed , so first calculate the probability of a grid being dyed, and then double-cycle to accumulate the probability of each grid.


如何求一个格子被染色的概率?要么该矩阵在这个格子的左边,要么在上,下或者右边,那么如果以j值(列)来看,exp:如果最小j值和最大j值都在格子的左边,是不是不用管i值,矩阵直接在格子左边?依此类推,那么j值矩阵不覆盖的概率为p1=((j-1)*(j-1)+(n-j)*(n-j)/(n*n);依次类推出i值矩阵不覆盖的概率p2,然后该格子被覆盖的概率为 p=(1-p1)*(1-p2),再用1-p是没有覆盖该格子的概率,然后k次方就是k次都没有覆盖该格子的概率,用1-(1-p)^k就是一个格子被染色的概率。


为什么不直接求该格子被染色的概率,而是直接1-减去p?假设该格子被覆盖的概率为p那么该格子被染色的概率就要用多项式来统计,非常麻烦...

代码如下:

#include<cstdio>
#include<algorithm>
using namespace std;
int m,n;
double pp(int i,int m)
{
double t1=i-1,t2=m-i;

return 1.0-(t1*t1+t2*t2)/(m*m);
}
double ans(int i,int j,int k)
{
   double x=1-pp(i,m)*pp(j,n),p=1;
   
   for(int i=1;i<=k;i++)
   p=p*x;
   
   return 1-p;
}
int main()
{
int i,j,k,t;

scanf("%d",&t);

for(int ll=1;ll<=t;ll++)
{

double res=0;
scanf("%d %d %d",&m,&n,&k);

for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{

res=res+ans(i,j,k);
}


    printf("Case #1: %.0lf\n",res);
    
}

return 0;
 } 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325933984&siteId=291194637