HDU 5245 染色问题(数学期望)

8 篇文章 0 订阅

 

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

 

题目大意是给定一个m*n的矩阵,每次选择一个随机选择子矩阵进行染色,问k次染色后,被染色格子数目的期望值。

 

思路:染色格子数的期望值=每个格子被染色的概率和,所以先把一个格子被染色的概率求出来,然后二重循环累加每个格子的概率就是了。

 

如何求一个格子被染色的概率?要么该矩阵在这个格子的左边,要么在上,下或者右边,那么如果以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;
 } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值