标签:hdu 概率 期望
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 anM×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 theM×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 integerT(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 thet-th
case, and then output the expected number of squares that will be painted. Round to integers.
Sample Input
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 = 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));
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 5245 Joyful (概率题 求期望)
标签:hdu 概率 期望
原文地址:http://blog.csdn.net/tc_to_top/article/details/47681573