HDU 5245 Joyful

传送门
Joyful

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

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(T≤100), 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 1≤M,N≤500, 1≤K≤20.

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

题目大意:
就是有一个 m*n 的矩阵方格,然后你每次取两个方格,分别是(x1,y1)和(x2,y2);然后就是每次覆盖一个子矩阵是以(x1,y1)和(x2,y2)为对角线构成的矩阵,让你求的就是你进行 k 次之后得到的方格数的期望。

解题思路:
其实这个题,画一下就行了而且非常清楚,我也不会画,我就用语言描述啦,我们先求一下总的方案数,第一个方格是m*n,第二个方格还是m*n,那么总的方案数就是 m^2*n^2,假设我们选的(i, j)点就是没有被覆盖的点,那么我们选的那两个方格肯定是 i行上面的和下面的 j列左面的和右面的,但是我们重复了那4个角(其实也不能叫角,应该叫子矩阵),所以我们要减去那四个角的矩阵,假设结果是ans,那么概率显然就是 p = ans/(m^2*n^2)然后我们取了k次之后就是p^k,那么被覆盖的概率就是 1-p^k,然后我们进行的就是 两个循环 for(i: 1-m),for(j: i-n),那么每次都对1-p^k进行累加得到的就是期望,注意的就是m^2*n^2会爆int,
最后的结果是四舍五入的,有一个小窍门(四舍五入的时候加上0.5就行了)
My Code:

#include 
#include 
using namespace std;
typedef long long LL;
int main()
{
    int T;
    cin>>T;
    for(int cas=1; cas<=T; cas++)
    {
        LL m, n, k;
        cin>>m>>n>>k;
        LL tot = (m*n) * (m*n);
        double ret = 0;
        for(LL i=1; i<=m; i++)
        {
            for(LL j=1; j<=n; j++)
            {
                LL up = ((i-1)*n) * ((i-1)*n);
                LL down = ((m-i)*n) * ((m-i)*n);
                LL left = ((j-1)*m) * ((j-1)*m);
                LL right = ((n-j)*m) * ((n-j)*m);
                LL leftup = ((i-1)*(j-1)) * ((i-1)*(j-1));
                LL rightup = ((n-j)*(i-1)) * ((n-j)*(i-1));
                LL leftdown = ((j-1)*(m-i)) * ((j-1)*(m-i));
                LL rightdown = ((m-i)*(n-j)) * ((m-i)*(n-j));
                LL ans = up + down + left + right- leftup - rightup - leftdown - rightdown;
                ///cout<
                double tp = 1.0;
                double p = 1.0*ans/tot;
                for(int ii=0; ii1.0-tp;
            }
        }
        printf("Case #%d: %lld\n",cas,(LL)(ret+0.5));
    }
    return 0;
}

你可能感兴趣的:(ACM_概率,ACM_HDU,ITAK的ACM之路)