[hihoCoder太阁最新面经算法竞赛8]题目2 : Dice Possibility(简单记忆化搜索)
2016-07-17 11:09
579 查看
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
What is possibility of rolling N dice and the sum of the numbers equals to M?
Two integers N and M. (1 ≤ N ≤ 100, 1 ≤ M ≤ 600)
Output the possibility in percentage with 2 decimal places.
样例输入
样例输出
题解:简单记忆化搜索
单点时限:1000ms
内存限制:256MB
描述
What is possibility of rolling N dice and the sum of the numbers equals to M?
输入
Two integers N and M. (1 ≤ N ≤ 100, 1 ≤ M ≤ 600)
输出
Output the possibility in percentage with 2 decimal places.样例输入
2 10
样例输出
8.33
题解:简单记忆化搜索
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int n,m; double tmp; double dp[101][610]; double dfs(int pos,int last) { if(dp[pos][last]!=-1) return dp[pos][last]; int s=max(last-pos*6,1); int e=min(last-pos*1,6); double ans=0; for(int i=s;i<=e;i++) { double tmp_ans=dfs(pos-1,last-i); if(tmp_ans!=0) ans+=tmp_ans*tmp; } dp[pos][last]=ans; return dp[pos][last]; } int main() { cin>>n>>m; tmp=1.0/6; for(int i=0;i<n;i++) { for(int j=1;j<=600;j++) { dp[i][j]=-1; } } for(int i=1;i<=6;i++) { dp[0][i]=tmp; } dfs(n-1,m); if(dp[n-1][m]==-1) printf("0.00\n"); else printf("%.2f\n",dp[n-1][m]*100); return 0; }
相关文章推荐
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- 本人即将筹备败家日志,敬请期待!
- 书评:《算法之美( Algorithms to Live By )》
- IE:使用搜索助手
- 动易2006序列号破解算法公布
- C#递归算法之分而治之策略
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- C#算法之大牛生小牛的问题高效解决方法
- C#算法函数:获取一个字符串中的最大长度的数字
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- 经典排序算法之冒泡排序(Bubble sort)代码
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解