HDOJ5245 Joyful(dp)
2015-08-22 18:28
260 查看
Joyful
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 506 Accepted Submission(s): 219
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
2 3 3 1 4 4 2
Sample Output
Case #1: 4 Case #2: 8 HintThe precise answer in the first test case is about 3.56790123.
概率dp,谁做出来商kalili香吻一枚2333
参考了点击打开链接
就是把矩阵拆分,对每个小矩阵计算概率。
AC代码:
#include "iostream" #include "cstdio" #include "cstring" #include "algorithm" #include "cmath" using namespace std; int t, k; double n, m; int main(int argc, char const *argv[]) { scanf("%d", &t); for(int cas = 1; cas <= t; ++cas) { scanf("%lf%lf%d", &n, &m, &k); double ans = 0; for(double i = 1; i <= n; ++i) for(double j = 1; j <= m; ++j) { double p = m * n; p += (i - 1) * (j - 1) * (n - i + 1) * (m - j + 1); p += (i - 1) * (m - j) * (n - i + 1) * j; p += (j - 1) * (n - i) * (m - j + 1) * i; p += (n - i) * (m - j) * i * j; p += (i - 1) * m * (n - i + 1); p += (m - j) * n * j; p += (n - i) * m * i; p += (j - 1) * n * (m - j + 1); p = p / n / n / m / m; ans += 1 - pow(1 - p, k); } printf("Case #%d: %d\n", cas, int(ans + 0.5)); } return 0; }
相关文章推荐
- 解决JSP中文乱码问题
- 运用js实现批删除功能
- ajax-onreadystatechange 事件
- 烂泥:高负载均衡学习haproxy之关键词介绍
- C/C++中关于typedef的用法总结
- 笔记0_算法,程序设计,语言等_哈佛_计算机科学cs50_David J. Malan
- What is CMSIS-DAP
- 使用Java 多线程编程 让三个线程轮流输出ABC,循环10次后结束
- 深入剖析哪些服务是Oracle 11g必须开启的
- HDU2652Warching TV (二分 dp)
- 深入理解Android卷III 第8章 深入理解Android壁纸 (节选)
- Python - urllib2 模块
- spring3表达式语言(SpEL)
- VLAN间路由
- 一个百万级PHP站点的网站架构
- 黑马程序员-----反射
- HDOJ--5137--How Many Maos Does the Guanxi Worth
- 归并排序
- VTP(链路聚集协议)
- leetCode #258 Add Digits