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

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

对于这道题的话,首先要考虑的是进行一次选择时的期望。求期望的方法为单独考虑每一格所能获得的期望,然后将所有格的期望相加即为答案。

对于每一个所能获得的期望,即要计算所有包含这一格的个数ans,除于总的选择方案tot

此时我们的问题转向了如何计算A[x.y]上

由题目描述,一次染色中可能的操作有n^2*m^2种

计算A[x,y]时,我们可以把整个矩阵做如下拆分

当前计算的方块为[x,y],即图中编号为5的部分

将其他部分拆分成图上8个区域,则可得到以下关系

对于一种染色方案能够覆盖方块[x,y]时
①[x1,y1]取在区域1内时,[x2,y2]可以在5、6、8、9四个区域内任取;
②[x1,y1]取在区域2内时,[x2,y2]可以在4、5、6、7、8、9六个区域内任取;
③[x1,y1]取在区域3内时,[x2,y2]可以在4、5、7、8四个区域内任取;
④[x1,y1]取在区域4内时,[x2,y2]可以在2、3、5、6、8、9六个区域内任取;
⑤[x1,y1]取在区域5内时,[x2,y2]可以在所有区域内任取;
⑥[x1,y1]取在区域6内时,[x2,y2]可以在1、2、4、5、7、8六个区域内任取;
⑦[x1,y1]取在区域7内时,[x2,y2]可以在2、3、5、6四个区域内任取;
⑧[x1,y1]取在区域8内时,[x2,y2]可以在1、2、3、4、5、6六个区域内任取;
⑨[x1,y1]取在区域1内时,[x2,y2]可以在1、2、4、5四个区域内任取;

计算出这个格子的概率p后,总的答案加上 1-pow(1-p,k),得到最后的答案

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<stdlib.h>
 6 #include<queue>
 7 using namespace std;
 8 #define ll long long
 9 int m,n,k;
10 int main()
11 {
12     int t;
13     int ac=0;
14     scanf("%d",&t);
15     while(t--){
16         scanf("%d%d%d",&n,&m,&k);
17         double ans=0;
18         for(int i=1;i<=n;i++){
19             for(int j=1;j<=m;j++){
20                 double tmp=0;
21                 tmp=tmp+(double)(i-1)*(j-1)*(n-i+1)*(m-j+1);//1
22                 tmp=tmp+(double)(i-1)*(n-i+1)*m;//2
23                 tmp=tmp+(double)(i-1)*(m-j)*(n-i+1)*j;//3
24                 tmp=tmp+(double)(m-j)*n*j;//6
25                 tmp=tmp+(double)n*m;//5
26                 tmp=tmp+(double)(j-1)*n*(m-j+1);//4
27                 tmp=tmp+(double)(n-i)*(j-1)*i*(m-j+1);//7
28                 tmp=tmp+(double)(n-i)*i*m;//8
29                 tmp=tmp+(double)(n-i)*(m-j)*i*j;//9
30
31                 double p=tmp/n/n/m/m;
32                 ans=ans+1-pow((1-p),k);
33
34             }
35         }
36         printf("Case #%d: ",++ac);
37         printf("%d\n",int(ans+0.5));
38     }
39     return 0;
40 }

时间: 2024-11-15 00:27:35

hdu 5245 Joyful(期望的计算,好题)的相关文章

HDU 5245 Joyful (期望)

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

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

传送门 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 w

J - Joyful HDU - 5245 (概率)

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

hdu 1201 18岁生日 (简单题)

18岁生日 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18281    Accepted Submission(s): 5776 Problem Description Gardon的18岁生日就要到了,他当然很开心,可是他突然想到一个问题,是不是每个人从出生开始,到达18岁生日时所经过的天数都是一样的呢?似乎并不全都是这样,所以他

HDU 1228 A + B 的浙大考研题

Problem Description 读入两个小于100的正整数A和B,计算A+B. 需要注意的是:A和B的每一位数字由对应的英文单词给出. Input 测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出. Output 对每个测试用例输出1行,即A+B的值. Sample Input one + two = three four + five six = zero seven + eig

2014 HDU多校弟五场J题 【矩阵乘积】

题意很简单,就是两个大矩阵相乘,然后求乘积. 用 Strassen算法 的话,当N的规模达到100左右就会StackOverFlow了 况且输入的数据范围可达到800,如果变量还不用全局变量的话连内存开辟都开不出来 1 #pragma comment(linker, "/STACK:16777216") 2 #include <iostream> 3 #include <stdio.h> 4 #define ll long long 5 using namesp