思路:显然考虑正着做会很麻烦,因为矩阵是可以重叠的,那么就设矩阵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.
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 .
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
2 3 3 1 4 4 2
Sample Output
Case #1: 4 Case #2: 8HintThe precise answer in the first test case is about 3.56790123.