HDU 5245 Joyful (概率题 求期望)


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 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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5245

题目大意:一个大矩形由n*m个小方块组成,一个人每次选两个小方块,他可以对以这两个方块为顶点的矩形面积内的小方块上色,他一共可选k次,现在求被上色的小方块数目的期望

题目分析:这是最伤心的一题,上海大都会邀请赛,出了这题就拿银牌了,无奈坑了队友
每个格子可以被重复选,因此可以把每一个小方块选不选当做一个独立事件,所以我们算出每个小方块对总期望的贡献值就行了,直接算不好算,考虑算每个小方块一次不被选的概率p,这个算起来就方便很多,不妨以当前点为中心,那么只有四种情况,被选的两个小方块同在中心的上下左右,不过这样会重复,四个角相当于被算了两次,再把它们减掉一次,这样求出来的是当前点一次不会被上色的情况数,除以总情况数(m * n * m * n)就是当前方块一次不会被上色的概率,再乘k次,就是k次这个方块都不被上色的概率,用1减则为选k次这个方块被上色的概率,只需要把每个方块选k次后被上色的概率累加即可,情况数中间可能会爆int,用long long


#include <cstdio>
#include <cmath>
#define ll long long

int main()
{
    int T;
    scanf("%d", &T);
    for(int ca = 1; ca <= T; ca++)
    {
        int m, n, k;
        scanf("%d %d %d", &m, &n, &k);
        double ans = 0;
        ll sum = (ll) m * m * n * n;
        for(int i = 1; i <= m; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                ll num = 0;
                num += (ll) (i - 1) * (i - 1) * n * n;
                num += (ll) (m - i) * (m - i) * n * n;
                num += (ll) (j - 1) * (j - 1) * m * m;
                num += (ll) (n - j) * (n - j) * m * m;
                num -= (ll) (i - 1) * (i - 1) * (j - 1) * (j - 1);
                num -= (ll) (i - 1) * (i - 1) * (n - j) * (n - j);
                num -= (ll) (j - 1) * (j - 1) * (m - i) * (m - i);
                num -= (ll) (m - i) * (m - i) * (n - j) * (n - j);
                double p = 1.0 * num / sum;
                p = pow(p, k);
                ans += 1.0 - p;
            }
        }
        printf("Case #%d: %d\n", ca, (int)round(ans));
    }
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值