您的位置:首页 > 其它

HDU 5245 Joyful(概率求期望)——2015年上海邀请赛

2017-10-26 17:32 453 查看
传送门

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.
[align=left]Input[/align] 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.

[align=left]Output[/align] 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.
[align=left]Sample Input[/align]
2
3 3 1
4 4 2

[align=left]Sample Output[/align]
Case #1: 4
Case #2: 8

HintThe precise answer in the first test case is about 3.56790123.


题目大意

给定一个 n∗m 的方格,每次选取两个格子a(x1,y1),b(x2,y2) 以这两个格子作为矩形的对角点,得到一个矩形,然后将其涂上颜色,求涂 k 次之后得到的有颜色的面积的期望。

解题思路

就是对于每一个格子,求其 k 次之后被染色的概率,然后将每一个格子的概率加起来就是期望。

然而对于每一个格子来说,求 k 次之后被染色的概率不好求,我们可以求 k 次都没有被染色的概率 p,然后 1−p 就是我们所求的概率。怎么求不被染色的概率呢,其实就是以当前所求点 (i,j) 为中心,分为四个部分。每个部分的面积分别为:

左部分:(j−1)∗n

右部分:(m−j)∗n

上部分:(i−1)∗m

下部分:(n−i)∗m

再减去重复的部分:

左上:(i−1)∗(j−1)

右上:(i−1)∗(m−j)

左下:(n−i)∗(j−1)

左下:(n−i)∗(m−j)

所以我们得到总的没有被涂色的面积为 tmp,涂色的面积就是 1−tmpn∗m∗n∗mk

代码

#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <map>
using namespace std;
typedef long long LL;
const int MAXN = 1e6+5;
const double PI = acos(-1);
const double eps = 1e-8;
const LL MOD = 1e9+7;
int main(){
//freopen("C:/Users/yaonie/Desktop/in.txt", "r", stdin);
//freopen("C:/Users/yaonie/Desktop/out.txt", "w", stdout);
int T; scanf("%d",&T);
for(int cas=1; cas<=T; cas++){
LL n, m, k; scanf("%lld%lld%lld",&n,&m,&k);
double ans = 0;
LL t = n*n*m*m;
for(LL i=1; i<=n; i++){
for(LL j=1; j<=m; j++){
LL tmp = (j-1)*(j-1)*n*n-(i-1)*(i-1)*(j-1)*(j-1);
tmp += ((m-j)*(m-j)*n*n-(i-1)*(i-1)*(m-j)*(m-j));
tmp += ((i-1)*(i-1)*m*m-(n-i)*(n-i)*(j-1)*(j-1));
tmp += ((n-i)*(n-i)*m*m-(n-i)*(n-i)*(m-j)*(m-j));
double sum = 1;
double a = 1.0*tmp/t;
LL b = k;
while(b){
if(b&1) sum=sum*a;
b>>=1;
a=a*a;
}
ans += 1-sum;
}
}
printf("Case #%d: %.0f\n",cas, ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: