版权声明:欢迎关注我的博客,本文为博主【炒饭君】原创文章,未经博主同意不得转载 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≤106) 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 109 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 <iostream>
#include <cstdio>
#include <cmath>
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;
}

HDU 4652 Dice (概率DP)的更多相关文章

  1. hdu 4652 Dice 概率DP

    思路: dp[i]表示当前在已经投掷出i个不相同/相同这个状态时期望还需要投掷多少次 对于第一种情况有: dp[0] = 1+dp[1] dp[1] = 1+((m-1)*dp[1]+dp[2])/m ...

  2. HDU 4599 Dice (概率DP+数学+快速幂)

    题意:给定三个表达式,问你求出最小的m1,m2,满足G(m1) >= F(n), G(m2) >= G(n). 析:这个题是一个概率DP,但是并没有那么简单,运算过程很麻烦. 先分析F(n ...

  3. hdu 4599 Dice 概率DP

    思路: 1.求f[n];dp[i]表示i个连续相同时的期望 则 dp[0]=1+dp[1]     dp[1]=1+(5dp[1]+dp[2])/6     ……     dp[i]=1+(5dp[1 ...

  4. HDU 3853LOOPS(简单概率DP)

    HDU 3853    LOOPS 题目大意是说人现在在1,1,需要走到N,N,每次有p1的可能在元位置不变,p2的可能走到右边一格,有p3的可能走到下面一格,问从起点走到终点的期望值 这是弱菜做的第 ...

  5. Throwing Dice(概率dp)

    C - Throwing Dice Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Lig ...

  6. HDU - 1099 - Lottery - 概率dp

    http://acm.hdu.edu.cn/showproblem.php?pid=1099 最最简单的概率dp,完全是等概率转移. 设dp[i]为已有i张票,还需要抽几次才能集齐的期望. 那么dp[ ...

  7. HDU 4405 【概率dp】

    题意: 飞行棋,从0出发要求到n或者大于n的步数的期望.每一步可以投一下筛子,前进相应的步数,筛子是常见的6面筛子. 但是有些地方可以从a飞到大于a的b,并且保证每个a只能对应一个b,而且可以连续飞, ...

  8. HDU 4576 Robot(概率dp)

    题目 /*********************复制来的大致题意********************** 有N个数字,M个操作, 区间L, R. 然后问经过M个操作后落在[L, R]的概率. * ...

  9. [HDU 4089]Activation[概率DP]

    题意: 有n个人排队等着在官网上激活游戏.Tomato排在第m个. 对于队列中的第一个人.有以下情况: 1.激活失败,留在队列中等待下一次激活(概率为p1) 2.失去连接,出队列,然后排在队列的最后( ...

  10. hdu 3853 LOOPS 概率DP

    简单的概率DP入门题 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include ...

随机推荐

  1. matploylib之热力图

    刚学我也不熟,做个笔记吧 # coding:utf-8 import numpy as np import matplotlib.pyplot as plt dx = 0.01 dy = 0.01 # ...

  2. java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation getting while running test project?

    转摘:http://stackoverflow.com/questions/11155340/java-lang-illegalaccesserror-class-ref-in-pre-verifie ...

  3. A re-introduction to JavaScript (JS Tutorial) 转载自:https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript

    A re-introduction to JavaScript (JS Tutorial) Redirected from https://developer.mozilla.org/en-US/do ...

  4. centos7.3安装wordpress

    一.安装并配置数据库 1.安装mariadb #yum install -y mariadb-server mariadb 2.启动数据库并设置开机自启#systemctl start mariadb ...

  5. 安装软件 学习linux命令

    nm -D /usr/lib64/libstdc++.so.6 | grep GLIBCnm dumps named symbols, -D for dynamic libs, and grep fo ...

  6. JS面向对象——构造函数模型

    ECMAScript中的构造函数可用来创建特定类型的对象.我们可以创建自定义构造函数,从而定义对象类型的属性和方法,解决工厂模型中对象识别的问题. <!DOCTYPE html> < ...

  7. runtime之归档和解档

    IOS开发之NSCoding协议(使用runtime)近期学习IOS的runtime库,然后看到之前写的NSCoding协议有点复杂,如果属性少还好,如果100多个属性,则会显得麻烦.下面使用常规方式 ...

  8. 【记录】docker 安装redis

    docker拉取镜像 docker pull redis docker 启动redis docker run -dit -p 6379:6379 --name redis redis:latest - ...

  9. HDU-5072 补集转化+容斥原理

    题意:给n个数,求满足一下条件的三元组(a,b,c)数量:a,b,c两两互质或者a,b,c两两不互质. 解法:这道题非常巧妙地运用补集转化和容斥原理.首先我们令这n个数为n个点,然后两两之间连边如果是 ...

  10. 数据结构---Java---String

    1.概述 1.1 源码(JDK1.8) public final class String implements java.io.Serializable, Comparable<String& ...