Sweety

Practice makes perfect

导航

HDU 5245 Joyful(概率 期望)

Posted on 2017-04-30 20:22  蓝空  阅读(173)  评论(0编辑  收藏  举报

Joyful

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1295    Accepted Submission(s): 567


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,N5001K20.
 

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
 

Recommend

题意:

题意大致是:进行K次染色,每次染色会随机选取一个以(x1,y1),(x2,y2)为一组对角的子矩阵进行染色,求K次染色后染色面积的期望值(四舍五入)。


开始还在想通过两个端点来想,但是由于可以k次染色,所以分割来看简单:

摘抄网上(和我之前想的思路是一样的,吐舌头):

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


只不过在我看是看到这道题目的时候,犯了一个致命的错误,就是把分割的那四块计算时本来是可以重复选的,也就是起始和另外一个端点是可以同一个端点的,但是我竟然想成组合问题了,然后C(n,i)然后就错了。。。不过还好想过来了。

我地AC代码:

#include <bits/stdc++.h>
using namespace std;
int main()
{

    long long  T,m,n,k;
    scanf("%d",&T);
    int Case=1;
    while(T--)
    {
        double ans=0;
        long long sum;
        scanf("%lld%lld%lld",&m,&n,&k);
        for(long long  i=0; i<m; i++)
        {
            for(long long j=0; j<n; j++){
            sum=0;
               sum=sum+i*n*i*n;
               sum=sum+(m-i-1)*n*(m-i-1)*n;
               sum=sum+j*m*j*m;
               sum=sum+(n-j-1)*m*(n-j-1)*m;
               sum=sum-i*j*i*j;
               sum=sum-i*(n-j-1) * i*(n-j-1);
               sum=sum-(m-i-1)*j * (m-i-1)*j;
               sum=sum-(m-i-1)*(n-j-1) * (m-i-1)*(n-j-1);

               ans=ans+1- pow(sum*1.0 / ((m*n)*(m*n)) ,k);
            }
//            cout << sum << endl;
        }
        printf("Case #%d: %.0f\n",Case++,ans);
    }
    return 0;
}