HDU 5245 Joyful (期望)

来源:互联网 发布:php.ini在哪 编辑:程序博客网 时间:2024/05/29 19:41


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×NM×N matrix. The wall has M×NM×N squares in all. In the whole problem we denotes (x,y)(x,y) to be the square at the xx-th row, yy-th column. Once Sakura has determined two squares (x1,y1)(x1,y1) and (x2,y2)(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 KK times. More specifically, each time for Sakura to use that tool, she just randomly picks two squares from all the M×NM×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 TT(T100T≤100), denoting the number of test cases. 

For each test case, there is only one line, with three integers M,NM,N and KK
It is guaranteed that 1M,N5001≤M,N≤5001K201≤K≤20
 

Output

For each test case, output ''Case #t:'' to represent the tt-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


题意:

给定一个n*m的矩形,由n*m个格子组成,我们可以选k次,每次可以选择的两个格子

这两个格子作为矩形的对角线可以确定一个矩形,这个矩形里的所有小格子都会被覆

盖,求k次后,被覆盖的格子的个数的期望。


这题傻逼了。。。真的是傻逼了。。一开始觉得很水啊,枚举面积,两个点确定面积,区域点就都在这个矩形里面。。真尼玛zz,样例都过不了,老是少了,仔细一想, 你枚举的面积,不一定是有一个矩形确定的,可能是好几个矩形的面积并。。。啊啊啊啊啊,我真的zz。。

这题想想,好像很难确定被选的概率,有区域问题,中间问题。。但是每个方块不选的概率很简单。。,那么如果取的那两个点(x1,y1),(x2,y2)都在1到x-1行或者都在x+1到m行之间或者都在1到y-1列之间或者都在y+1到n列之间,则(x,y)这个点不会被染色。。然后就很简单了,注意不一直求整块面积,可以分成单位面积。。。正着求不好求,反着求。。

网上一个注释很多的代码

#include <iostream>#include <cmath>#include <cstdio>using namespace std;typedef long long ll;ll c(ll a,ll b){    return a*a*b*b;}int main(){    /*1.用G++提交 C++慢 有可能都超时    2.用scanf写 cin慢 虽然在本题中一样    3.tmp用循环跑 pow慢*/    int t,cas=1;    scanf("%d",&t);    while(t--)    {        ll n,m,k;//这里nm顺序无所谓        //注意这里一定要用long long,要不然计算的时候还得强制转化一下        scanf("%lld%lld%lld",&n,&m,&k);        ll ans,sum=n*n*m*m;        //ans除以sum求不被染色概率        //两个格子每个都有n*m种选择        double p,qiwang=0;        //p表示该格子一次操作后不被染色的概率        //qiwang表示该格子被染色的概率也就是期望,因为是相对于一个格子而言的 乘数为1        for(int i=1;i<=n;i++)//对每个格子进行讨论            for(int j=1;j<=m;j++)        {            ans=0;//初始化            //容斥原理            ans+=c(i-1,m);            ans+=c(n-i,m);            ans+=c(n,j-1);            ans+=c(n,m-j);            ans-=c(i-1,j-1);            ans-=c(i-1,m-j);            ans-=c(n-i,j-1);            ans-=c(n-i,m-j);            p=1.0*ans/sum;//该格子不被染色的概率            double tmp=1;//初始化            //该格子k次操作后不被染色的概率            for(int i=0;i<k;i++)//pow返回double 不要用pow                tmp*=p;            //该格子被染色的概率也就是期望            qiwang+=1-tmp;        }        // %0.f自动取整了 或者floor+0.5或者round函数也可以        printf("Case #%d: %.0lf\n",cas++,qiwang);    }    return 0;}


0 0