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 <iostream>
#include <cstdio>
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<<ans<<endl;
                double tp = 1.0;
                double p = 1.0*ans/tot;
                for(int ii=0; ii<k; ii++)
                    tp *= p;
                ret += 1.0-tp;
            }
        }
        printf("Case #%d: %lld\n",cas,(LL)(ret+0.5));
    }
    return 0;
}
时间: 2024-12-19 02:08:29

HDU 5245 Joyful的相关文章

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

HDU 5245 Joyful (2015年上海大都赛J题,概率)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5245 题意: 给定一个n*m的矩形,由n*m个格子组成,我们可以选k次,每次可以选择的两个格子 这两个格子作为矩形的对角线可以确定一个矩形,这个矩形里的所有小格子都会被覆 盖,求k次后,被覆盖的格子的个数的期望. 分析: 棋盘被覆盖的格子数的期望 = 每个格子被覆盖的概率的和. 每次选择的方案有n*m*n*m种. 格子坐标为(i,j)被覆盖的方案数为: tot = 2*(2*(i*j*(n-i+1)

hdu 5245 Joyful(期望)

Joyful Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1243    Accepted Submission(s): 546 Problem Description Sakura has a very magical tool to paint walls. One day, kAc asked Sakura to paint a

HDU 5245 Joyful (期望)

题意:进行K次染色,每次染色会随机选取一个以(x1,y1),(x2,y2)为一组对角的子矩阵进行染色,求K次染色后染色面积的期望值(四舍五入). 析:我们可以先求出每个格子的期望,然后再加起来即可.我们可以把格子进行划分,然后再求概率. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cst

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

J - Joyful HDU - 5245 (概率)

题目链接: J - Joyful  HDU - 5245 题目大意:给你一个n*m的矩阵,然后你有k次涂色机会,然后每一次可以选定当前矩阵的一个子矩阵染色,问你这k次用完之后颜色个数的期望. 具体思路:颜色个数的期望等于每一个方块单独的期望加起来,就是总的期望. 对于当前的方块的期望,我们先计算这个方块不会出现的概率,就是当前的(x,y),先计算出当前的两个点在他周围四整块的出现的概率,但是这样四个角会重复计算,再去掉就好了. AC代码: 1 #include<bits/stdc++.h> 2

HDU 5345 Joyful(概率题求期望)

D - Joyful Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Sakura has a very magical tool to paint walls. One day, kAc asked Sakura to paint a wall that looks like an $M \times N$ matrix. The wal

HDU 5245

题目大意: 每次随机选择两个点,便把这两个点之间形成的子矩阵上的每一个方块涂色,问随机选择k次,整个m*n的矩阵中有多少个小方块被涂上了颜色 这道题不难,但自己智商实在捉急,一直想不出来... 因为这里n,m<=500,所以总共250000个方块,我们可以考虑的是每一个方块在随机选择1次后被染色的概率 p[i][j] 那么k次后就变成了 1-(1-p[i][j])^k的概率了,我们将所有概率相加就得到了总共的染色块数 至于怎么计算被染色概率就是可以将整个矩形块分解,然后去计算所有不能包括当前点的

HDU 5245 上海大都会 J题 (概率期望)

这道题的概率可以单独考虑每个格子对期望的贡献值.因为其实每个格子是否被选都可以认为是独立的,单独一个格子贡献的期望为1*(该格子K次被选的概率),所以答案其实就是每个格子K次被选中的概率之和. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define LL long long using namespace std; int main(){ LL