hdu 5502 Candy Game(dp)
2015-10-22 08:29
344 查看
题目链接:hdu 5502 Candy Game
解题思路
枚举i,然后用o(n)的复杂度求出长度小于i的概率。代码
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int maxn = 1505; int N; double P, Q, dp[maxn], f[maxn]; double get (int x) { double tmp = f[0] = 1, p = pow(P, x); for (int i = 1; i <= N + 1; i++) { f[i] = tmp; //for (int j = max(i-x, 0); j < i; j++) //f[i] += pow(P, i-j-1) * f[j]; if (i <= N) f[i] *= (1-P); tmp = tmp * P + f[i]; if (i >= x) tmp -= f[i-x] * p; } return f[N+1]; } double solve () { dp[0] = 0; for (int i = 1; i <= N+1; i++) dp[i] = get(i); double ans = 0; for (int i = 1; i <= N+1; i++) ans += i * (dp[i] - dp[i-1]); return ans; } int main () { int cas; scanf("%d", &cas); while (cas--) { scanf("%d%lf%lf", &N, &P, &Q); P = Q / P; printf("%.3lf\n", solve()); } return 0; }
相关文章推荐
- 开发 linux内核的技术路线
- leetcode-46、47 Permutations/II 数字的排列组合
- sizeof与strlen详解
- HBase深入分析之RegionServer
- mybatis+spring源码解析(动态代理 spring初始化)
- 用两个栈实现一个队列
- C/C++面试题:class和struct有什么区别?
- Zigzag Iterator 解答
- Windows 下环境变量设置(命令行)
- eclipse jsp html 格式化 format
- **LeetCode-Inorder Successor in BST
- iOS面试整理
- 【状压bfs】ctsc 1999 补丁vs错误
- sdwebimage如何获取缓存大小以及清除缓存
- HA集群之二:部署HA wordpress Cluster
- 微服务年度总结,看完这个你应该知道如何回答别人的问题了。
- mysql 不能插入中文和显示中文
- 矩阵算法 高斯消元 行列式 矩阵的秩
- 关于ExecuteNonQuery()执行成功却返回-1的问题
- 按位与运算符