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

版权声明:编写不易,转载请注明出处,谢谢。 https://blog.csdn.net/qingshui23/article/details/78356547

传送门

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 ( T100 ), 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 1M,N500 , 1K20 .
 

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.

题目大意

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

解题思路

就是对于每一个格子,求其 k 次之后被染色的概率,然后将每一个格子的概率加起来就是期望。
然而对于每一个格子来说,求 k 次之后被染色的概率不好求,我们可以求 k 次都没有被染色的概率 p ,然后 1p 就是我们所求的概率。怎么求不被染色的概率呢,其实就是以当前所求点 (i,j) 为中心,分为四个部分。每个部分的面积分别为:
左部分: (j1)n
右部分: (mj)n
上部分: (i1)m
下部分: (ni)m
再减去重复的部分:
左上: (i1)(j1)
右上: (i1)(mj)
左下: (ni)(j1)
左下: (ni)(mj)
所以我们得到总的没有被涂色的面积为 tmp ,涂色的面积就是 1tmpnmnmk

代码

#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;
}

猜你喜欢

转载自blog.csdn.net/qingshui23/article/details/78356547
今日推荐