HDU 5245 Joyful (概率期望)

系统管理员 2022-08-21 14:41 59阅读 0赞

HDU 5245 Joyful (概率) :http://acm.hdu.edu.cn/showproblem.php?pid=5245

题面描述:

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 877 Accepted Submission(s): 382

Problem Description

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

Sample Input

  1. 2
  2. 3 3 1
  3. 4 4 2

Sample Output

  1. Case #1: 4
  2. Case #2: 8
  3. Hint
  4. The precise answer in the first test case is about 3.56790123.

题目大意:

再一个M*N的矩阵中随机涂色k次,每次涂色都是随机的选择两个点作为要涂色部分的对顶点,且涂色部分为选中部分的子区域,求经过k次染色后染色面积的期望值(四舍五入)。

题目分析:

当k等于1时,期望被染色的面积应等于每个1*1的方块被染色的期望累加之和。

设k=1时,即只染色一次时,位于第x行第y列的方块被染色的概率为A[x,y],在经过k次操作之后,被染色的期望假设为p[x,y],则有:p[x,y]=1-(1-A[x,y])^k

且在每次染色中可能的操作有n*n*m*m种,根据数据范围,要用long long进行存储。

代码实现:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. using namespace std;
  5. int main()
  6. {
  7. int t,casenum=0;
  8. long long m,n,k;
  9. scanf("%d",&t);
  10. while(t--)
  11. {
  12. scanf("%lld%lld%lld",&n,&m,&k);
  13. double ans=0;
  14. for(int i=1;i<=n;i++)
  15. {
  16. for(int j=1;j<=m;j++)
  17. {
  18. double p=1.0*m*n;
  19. p+=1.0*(i-1)*(j-1)*(n-i+1)*(m-j+1);
  20. p+=1.0*(i-1)*(m-j)*(n-i+1)*j;
  21. p+=1.0*(j-1)*(n-i)*(m-j+1)*i;
  22. p+=1.0*(n-i)*(m-j)*i*j;
  23. p+=1.0*(i-1)*m*(n-i+1);
  24. p+=1.0*(m-j)*n*j;
  25. p+=1.0*(n-i)*m*i;
  26. p+=1.0*(j-1)*n*(m-j+1);
  27. p=1.0*p/n/n/m/m;
  28. ans+=1-(pow(1-p,k));
  29. }
  30. }
  31. printf("Case #%d: %d\n",++casenum,(int)(ans+0.5));
  32. }
  33. return 0;
  34. }

发表评论

表情:
评论列表 (有 0 条评论,59人围观)

还没有评论,来说两句吧...

相关阅读

    相关 cf期望概率专题

    cf1009E:求到第i段期望和的比较困难,但是单独求每段的期望是比较容易的,所以单独对每段求和,然后累计总和 E\[i\]=1/2\a1+1/4\a2+...+1/2^(i