hdu 5245 Joyful

来源:互联网 发布:ipad2越狱后必装软件 编辑:程序博客网 时间:2024/06/09 17:02

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 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 thet-th case, and then output the expected number of squares that will be painted. Round to integers.
 

Sample Input
23 3 14 4 2
 

Sample Output
Case #1: 4Case #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

题意:给一个N*M的网格,现在选K轮子矩形,每轮的矩形由随机选取的两个对顶点确定,求最终的被选中过的格子的数目。
分析:我们可以求出格子(i,j)被选中的概率pij,则答案为∑pij*i。

对于每个格子,k次选择相互独立,直接求选中的概率pij比较困难,但是k次未选中概率为pow(qij,k)比较好求,那么pij = 1 - pow(qij,k)。
但是对于其中一轮,格子(i,j)未被选中的qij不好求出,而选中概率比较好求(1-qij) = (1-((n-i)*(n-i)+(i-1)*(i-1)/(n*n)) * (1-((m-j)*(m-j)+(j-1)*(j-1))/(m*m));

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<stack>#include<set>#include<cmath>using namespace std;typedef long long LL;int main(){    int i,j,k,m,n,T;    int cas = 0;    scanf("%d",&T);    while(T--)    {        scanf("%d %d %d",&m,&n,&k);        double ans = 0;        for(i=1; i<=n; i++)            for(j=1; j<=m; j++)        {            double p = (1.0-(1.0*(n-i)*(n-i)+1.0*(i-1)*(i-1))/(1.0*n*n))                      *(1.0-(1.0*(m-j)*(m-j)+1.0*(j-1)*(j-1))/(1.0*m*m));            ans+=1.0-pow(1.0-p, k*1.0);        }        printf("Case #%d: %.0f\n",++cas,ans);    }    return 0;}


0 0
原创粉丝点击