hdu 5245 Joyful


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 ( T100 ), 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 1M,N500 , 1K20 .
 

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: 8
Hint
The precise answer in the first test case is about 3.56790123.
 

Source
The 2015 ACM-ICPC China Shanghai Metropolitan Programming Contest

题意:给一个N*M的网格,现在选K轮子矩形,每轮的矩形由随机选取的两个对顶点确定,求最终的被选中过的格子的数目。
分析:我们可以求出格子(i,j)被选中的概率pij,则答案为∑pij*i。

对于每个格子,k次选择相互独立,直接求选中的概率pij比较困难,但是k次未选中概率为pow(qij,k)比较好求,那么pij = 1 - pow(qij,k)。
但是对于其中一轮,格子(i,j)未被选中的qij不好求出,而选中概率比较好求(1-qij) = (1-((n-i)*(n-i)+(i-1)*(i-1)/(n*n)) * (1-((m-j)*(m-j)+(j-1)*(j-1))/(m*m));

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<set>
#include<cmath>
using namespace std;
typedef long long LL;
int main()
{
    int i,j,k,m,n,T;
    int cas = 0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d %d",&m,&n,&k);
        double ans = 0;
        for(i=1; i<=n; i++)
            for(j=1; j<=m; j++)
        {
            double p = (1.0-(1.0*(n-i)*(n-i)+1.0*(i-1)*(i-1))/(1.0*n*n))
                      *(1.0-(1.0*(m-j)*(m-j)+1.0*(j-1)*(j-1))/(1.0*m*m));
            ans+=1.0-pow(1.0-p, k*1.0);
        }
        printf("Case #%d: %.0f\n",++cas,ans);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值