hdu5245 Joyful

本文探讨了一种使用随机选择的方格进行矩阵涂鸦的问题,通过数学计算得出最终涂画的平均数量。针对不同矩阵大小和涂鸦次数,提供了精确的预期涂画数计算方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:显然考虑正着做会很麻烦,因为矩阵是可以重叠的,那么就设矩阵k次都没被选中的概率为pp,那么最后累加1-pp就是概率了。对于任意一个矩阵,他不被选的概率为(r-1)^2*m^2+(c-1)^2*n^2+(n-r)^2*m^2+(m-c)^2*n^2,注意到四个角会被重复计算一次,那么再减去(r-1)^2*(c-1)^2,(r-1)^2*(m-c)^2,(n-r)^2*(c-1)^2,(n-r)^2*(m-c)^2就是啦

坑点:500^4会爆int

#include<bits/stdc++.h>
using namespace std;
#define LL long long
int main()
{
    int T;
	scanf("%d",&T);
	int cas = 1;
	while (T--)
	{
		int n,m,k;
		scanf("%d%d%d",&n,&m,&k);
		LL tot = (LL)n*n*m*m;
		double ans = 0;
		for (int r=1;r<=n;r++)
		{
			for (int c = 1;c<=m;c++)
			{
				LL p = 0;
				p += (LL)(r-1)*(r-1)*m*m;
				p += (LL)(c-1)*(c-1)*n*n;
				p += (LL)(n-r)*(n-r)*m*m;
				p += (LL)(m-c)*(m-c)*n*n;
				p -= (LL)(r-1)*(r-1)*(c-1)*(c-1);
				p -= (LL)(n-r)*(n-r)*(c-1)*(c-1);
				p -= (LL)(r-1)*(r-1)*(m-c)*(m-c);
				p -= (LL)(n-r)*(n-r)*(m-c)*(m-c);
				double tempp = (double)p/tot;
				double pp = 1.0;
				for (int i = 1;i<=k;i++)
					pp*=tempp;
				ans+=1.0-pp;
			}
		}
		printf("Case #%d: %.0f\n",cas++,ans);
	}
}


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(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,N5001K20.
 

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值