博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4652 Dice (概率DP)
阅读量:4975 次
发布时间:2019-06-12

本文共 3075 字,大约阅读时间需要 10 分钟。

版权声明:欢迎关注我的博客,本文为博主【炒饭君】原创文章,未经博主同意不得转载 https://blog.csdn.net/a1061747415/article/details/36685493

Dice


Problem Description
You have a dice with m faces, each face contains a distinct number. We assume when we tossing the dice, each face will occur randomly and uniformly. Now you have T query to answer, each query has one of the following form:
0 m n: ask for the expected number of tosses until the last n times results are all same.
1 m n: ask for the expected number of tosses until the last n consecutive results are pairwise different.
 

Input
The first line contains a number T.(1≤T≤100) The next T line each line contains a query as we mentioned above. (1≤m,n≤10
6) For second kind query, we guarantee n≤m. And in order to avoid potential precision issue, we guarantee the result for our query will not exceeding 10
9 in this problem.
 

Output
For each query, output the corresponding result. The answer will be considered correct if the absolute or relative error doesn't exceed 10
-6.
 

Sample Input
 
6 0 6 1 0 6 3 0 6 5 1 6 2 1 6 4 1 6 6 10 1 4534 25 1 1232 24 1 3213 15 1 4343 24 1 4343 9 1 65467 123 1 43434 100 1 34344 9 1 10001 15 1 1000000 2000
 

Sample Output
 
1.000000000 43.000000000 1555.000000000 2.200000000 7.600000000 83.200000000 25.586315824 26.015990037 15.176341160 24.541045769 9.027721917 127.908330426 103.975455253 9.003495515 15.056204472 4731.706620396
 

Source
 

题目大意:

m边形的骰子,问你出现连续同样(不同)n次须要掷的次数的数学期望。

解题思路:

利用递归方式的DP的思想推公式

(1)若询问为0,则:

dp[i] 记录的是已经连续i个同样,到n个同样同须要的次数的数学期望
dp[0]= 1+dp[1]
dp[1]= 1+( 1/m*dp[2]+(m-1)/m*dp[1])=1+(dp[2]+(m-1)*dp[1])/m;
dp[2]= 1+(dp[3]+(m-1)*dp[2])/m;
....................
dp[n]= 0
推出:
dp[i]   = 1 + ( (m-1)*dp[1] + dp[i+1] ) / m
dp[i+1] = 1 + ( (m-1)*dp[1] + dp[i+2] ) / m
因此。m*(dp[i+1]-dp[i])=(dp[i+2]-dp[i+1])
我们发现是等比数列
dp[0]-dp[1]=1;
dp[1]-dp[2]=m;
..........
dp[n-1]-dp[n]=m^(n-1)
累加,得:dp[0]-dp[n]=1+m+m^2+..........m^(n-1)=(1-m^n)/(1-m)
所以:dp[0]=(1-m^n)/(1-m);
(2)若询问为1,则:
 dp[0] = 1 + dp[1]
 dp[1] = 1 + (dp[1] + (m-1) dp[2]) / m
 dp[2] = 1 + (dp[1] + dp[2] + (m-2) dp[3]) / m
 dp[i] = 1 + (dp[1] + dp[2] + ... dp[i] + (m-i)*dp[i+1]) / m
dp[i+1]= 1 + (dp[1] + dp[2] + ... dp[i] + dp[i+1] + (m-i-1)*dp[i+1]) / m
 ...
 dp[n] = 0;
选出 dp[i] 和 dp[i+1] 这两行相减 得
dp[i] - dp[i+1] = (m-i-1)/m * (dp[i+1] - dp[i+2]);
因此  dp[i+1] - dp[i+2] = m/(m-i-1)*(dp[i]-dp[i+1]);
所以:
dp[0]-dp[1]=1;
dp[1]-dp[2]=1*m/(m-1);
dp[2]-dp[3]=1*m/(m-1)*m/(m-2);
..........
dp[n-1]-dp[n]=1*m/(m-1)*m/(m-2)*.......*m/(m-n+1);
累加得到答案

解题代码:

#include 
#include
#include
using namespace std;inline double solve(){ int op,m,n; scanf("%d%d%d",&op,&m,&n); double ans=0; if(op==0){ for(int i=0;i<=n-1;i++){ ans+=pow(1.0*m,i); } }else{ double tmp=1.0; for(int i=1;i<=n;i++){ ans+=tmp; tmp*=m*1.0/(m-i); } } return ans;}int main(){ int t; while(scanf("%d",&t)!=EOF){ while(t-- >0){ printf( "%.9lf\n",solve() ); } } return 0;}

转载于:https://www.cnblogs.com/ldxsuanfa/p/10869612.html

你可能感兴趣的文章
grid网格布局
查看>>
JSP常用标签
查看>>
九涯的第一次
查看>>
处理器管理与进程调度
查看>>
向量非零元素个数_向量范数详解+代码实现
查看>>
java if 用法详解_Java编程中的条件判断之if语句的用法详解
查看>>
matlab sin函数 fft,matlab的fft函数的使用教程
查看>>
mysql adddate()函数
查看>>
mysql sin() 函数
查看>>
单片机复位电路
查看>>
php json_decode失败,返回null
查看>>
3-day3-list-truple-map.py
查看>>
Edit控件显示多行文字
查看>>
JS第二周
查看>>
dataTable.NET的search box每輸入一個字母進行一次檢索的問題
查看>>
Python 文件处理
查看>>
邻接表详解
查看>>
迭代dict的value
查看>>
eclipse package,source folder,folder区别及相互转换
查看>>
Py 可能是最全面的 python 字符串拼接总结(带注释版)
查看>>