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 \times N$ matrix. The wall has $M \times 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 $(x_1, y_1)$ and $(x_2, y_2)$, 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 \times 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 \le 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 \le M, N \le 500$, $1 \le K \le 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: 8

Hint The precise answer in the first test case is about 3.56790123.

题意:有t组数据,每组输入m,n,k。表示有一个m*n的矩阵,在矩阵中随机取两个点(x1,y1),(x2,y2),以这两个点为矩形的两个顶点,画一个矩形,即矩形的四个顶点为(x1,y1),(x1,y2),(x2,y1),(x2,y2)。矩形中的所有点视为被染色,进行k次这样的操作,问该矩阵中被染色的格子的个数的期望。这两个点互不影响,也就是这两个点可以相同。每个点可以被多次染色,就是被染两次就算两次,不是算一次。

题解:因为(x1,y1),(x2,y2)这两个点是从矩阵中取的,第一个点有n*m种可能性,第二个点也有n*m种可能性,所以总的情况数为n*n*m*m。我们对矩阵中的每个点进行单独讨论,假设有这么一个点x,y。我们知道,x表示该点在第x行,y表示该点在第y列,那么如果取的那两个点(x1,y1),(x2,y2)都在1到x-1行或者都在x+1到m行之间或者都在1到y-1列之间或者都在y+1到n列之间,则(x,y)这个点不会被染色,将上面的四种情况可以看做是上下左右四种情况。根据容斥原理,我们要减去左上,左下,右上,右下这四种情况,这是因为上和左同时覆盖左上,以此类推。用该情况数除以总情况数所得概率p就是该点不被染色的概率,进行k次该操作,则tmp=p^k就是该点k次操作之后不被染色的概率,1-tmp就是该点被染色的概率,因为该点是一个点,所以概率就是期望,将每个点的期望加起来,就是结果了,注意四舍五入用%.0f就能实现,具体的有很多很多需要注意的细节问题请看代码注释。

#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=;
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=;
//p表示该格子一次操作后不被染色的概率
//qiwang表示该格子被染色的概率也就是期望,因为是相对于一个格子而言的 乘数为1
for(int i=;i<=n;i++)//对每个格子进行讨论
for(int j=;j<=m;j++)
{
ans=;//初始化
//容斥原理
ans+=c(i-,m);
ans+=c(n-i,m);
ans+=c(n,j-);
ans+=c(n,m-j);
ans-=c(i-,j-);
ans-=c(i-,m-j);
ans-=c(n-i,j-);
ans-=c(n-i,m-j);
p=1.0*ans/sum;//该格子不被染色的概率
double tmp=;//初始化
//该格子k次操作后不被染色的概率
for(int i=;i<k;i++)//pow返回double 不要用pow
tmp*=p;
//该格子被染色的概率也就是期望
qiwang+=-tmp;
}
// %0.f自动取整了 或者floor+0.5或者round函数也可以
printf("Case #%d: %.0lf\n",cas++,qiwang);
}
return ;
}

这个题之前把题目里的题号写错了,写成5345了。(流汗)

HDU 5245 Joyful(概率题求期望)的更多相关文章

  1. HDU 3853 LOOP (概率DP求期望)

    D - LOOPS Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit St ...

  2. HDU3853-LOOPS(概率DP求期望)

    LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Su ...

  3. hdu 5245 Joyful(期望的计算,好题)

    Problem Description Sakura has a very magical tool to paint walls. One day, kAc asked Sakura to pain ...

  4. J - Joyful HDU - 5245 (概率)

    题目链接: J - Joyful  HDU - 5245 题目大意:给你一个n*m的矩阵,然后你有k次涂色机会,然后每一次可以选定当前矩阵的一个子矩阵染色,问你这k次用完之后颜色个数的期望. 具体思路 ...

  5. HDU 5245 Joyful(期望)

    http://acm.hdu.edu.cn/showproblem.php?pid=5245 题意: 给出一个n*m的矩阵格子,现在有k次操作,每次操作随机选择两个格子作为矩形的对角,然后将这范围内的 ...

  6. POJ2096 Collecting Bugs(概率DP,求期望)

    Collecting Bugs Ivan is fond of collecting. Unlike other people who collect post stamps, coins or ot ...

  7. LightOJ 1030 【概率DP求期望】

    借鉴自:https://www.cnblogs.com/keyboarder-zsq/p/6216762.html 题意:n个格子,每个格子有一个值.从1开始,每次扔6个面的骰子,扔出几点就往前几步, ...

  8. HDU 5245 Joyful (期望)

    题意:进行K次染色,每次染色会随机选取一个以(x1,y1),(x2,y2)为一组对角的子矩阵进行染色,求K次染色后染色面积的期望值(四舍五入). 析:我们可以先求出每个格子的期望,然后再加起来即可.我 ...

  9. hdu 4405 Aeroplane chess(简单概率dp 求期望)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

随机推荐

  1. JZOJ 5922. sequence

    5922. [NOIP2018模拟10.23]sequence (File IO): input:sequence.in output:sequence.out Time Limits: 1000 m ...

  2. css 自动换行,超出省略号代替

    overflow : hidden;    text-overflow: ellipsis;    display: -webkit-box;    -webkit-line-clamp: 2;    ...

  3. Requests库:python实现的简单易用的http库

    1.get请求: get(url, params, headers) 2.json 解析 3.content 获取二进制内容 4.headers 添加 5.post请求:post(url,data,h ...

  4. poj1182食物链

    Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到 ...

  5. Aizu:2170-Marked Ancestor

    Marked Ancestor Time limit 8000 ms Memory limit 131072 kB Problem Description You are given a tree T ...

  6. Leetcode 173. 二叉搜索树迭代器

    题目链接 https://leetcode.com/problems/binary-search-tree-iterator/description/ 题目描述 实现一个二叉搜索树迭代器.你将使用二叉 ...

  7. 九、MySQL 5.7.9版本sql_mode=only_full_group_by问题

    MySQL 5.7.9版本sql_mode=only_full_group_by问题 用到GROUP BY 语句查询时com.mysql.jdbc.exceptions.jdbc4.MySQLSynt ...

  8. hdu1251统计难题(trie)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  9. RandomAecessFile open failed: EISDIR (Is a directory)

    今天下午搞断点续传,下载文件的时候,弄了一下,我把下载的文件命名为plug.jar.后来老是报错: open failed: EISDIR (Is a directory) 我烦了,两个小时啊,我操. ...

  10. Null / Loopback (Null)

    参考: http://www.cnblogs.com/caoguoping100/p/3654452.html https://wiki.wireshark.org/NullLoopback 抓包安装 ...