hdu 4652 Dice(期望)
来源:互联网 发布:主力意图分析 知乎 编辑:程序博客网 时间:2024/05/17 04:49
http://acm.hdu.edu.cn/showproblem.php?pid=4652
掷一枚骰子,有m个面,问掷出连续出现n个相同的面以及连续出现n个两两不同的面的期望。
设dp[i]表示已经掷出i个相同/不同的面的期望,可以确定终态dp[n] = 0,
对于出现连续n个相同的面有
dp[i] = 1/m * dp[i+1] + (m-1)/m*dp[1] + 1
再列一个式子 dp[i+1] = 1/m * dp[i+2] + (m-1)/m * dp[1] + 1
两式相减得dp[i+1] - dp[i] = 1/m * (dp[i+2] - d[i+1]),可以发现任意连续的两数之差成等比数列,就可以求出dp[0]。
对于出现n个两两不同的面有
dp[i] = (m-1)/m * dp[i+1] + 1/m * (dp[1] + dp[2] + ...+ dp[i])
再列一个式子dp[i+1] = (m-1)/m * dp[i+2] + 1/m*(dp[1] + dp[2] +....+dp[i+1])
两式相减得dp[i+1] - dp[i] = (m-i-1)/m * (dp[i+1] - dp[i+2]),最后各个连续的式子相减也能求出dp[0]。
#include <stdio.h>#include <iostream>#include <map>#include <set>#include <list>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>//#define LL __int64#define LL long long#define eps 1e-8#define PI acos(-1.0)using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 100010;int main(){int test;int order,m,n;while(~scanf("%d",&test)){for(int i = 1; i <= test; i++){scanf("%d %d %d",&order,&m,&n);double ans = 1;if(order == 0){double p = m*1.0,tmp = m*1.0;for(int i = 1; i <= n-1; i++){ans += tmp;tmp *= p;}}else{double tmp = 1.0;for(int i = 1; i <= n-1; i++){tmp *= m*1.0/(m-i);ans += tmp;}}printf("%.9lf\n",ans);}}return 0;}
0 0
- hdu 4652 Dice(期望)
- HDU 4568 Play the Dice(期望)
- HDU 4586 Play the Dice 概率期望
- HDU 4586 Play the Dice 数学期望
- Play the Dice - HDU 4586 期望dp
- HDU 4586 Play the dice (数学期望)
- HDU-4586 Play the Dice(概率期望)
- hdu 4652 Dice
- 概率dp求期望-hdu-4586-Play the Dice
- HDU 4518 Play the Dice(数学 概率 期望)
- hdu 4586 Play the Dice(水期望DP)
- hdu 4586 Play the Dice(概率期望题)
- Play the Dice hdu 4586 数学期望的问题
- HDU 4586 Play the Dice(概率+期望)
- 【jzoj100004】【Dice】【数学期望】
- HDU 4652 Dice(概率dp)
- hdu 4652 Dice(dp)
- HDU 4652 Dice (概率DP)
- 基础6---最后一个单词的长度
- 64位 Ubuntu android studio 编译android工程出错
- C#.Net应用程序异常处理
- 基础幻灯片
- Scala疯狂的函数式编程实例
- hdu 4652 Dice(期望)
- JavaScript之DOM编程
- 软件工程-设计
- Maximum Depth of Binary Tree
- 麻省理工时域差分软件 MEEP windows 下编译开发(二)——开始编译
- 关于OC中成员变量,属性的一些基本知识(针对初学者)
- unix shell if 参数
- C# 程序Hello World
- Android 自定义控件打造史上最简单的侧滑菜单