Dice

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 180    Accepted Submission(s): 121 Special Judge

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

题意:

输入 :op,m,n;

op=0:表示最后n次骰子的面都是一样的!!

op=1:表示最后n次骰子的面是互不相同的!!

对于op=0:对于i状态(即最后i次骰子的面都是一样的,比如是xx。。xx),然后接下来我可以有1/m的概率掷到x,即有1/m的概率可以转移到i+1这个状态

  同时,若我可以有1-1/m的概率掷到非x,比如序列变为(xx。。xxy),即有1-1/m的概率可以转移到i=1这个状态;

  所以状态转移为:dp[i]=1/m*dp[i+1]+(1-1/m)*dp[1]+1;  (__dp[n]=0__);

  然后就是n-1个方程递推下去求出dp[1]即可;

对于op=1:对于i状态(即最后i次骰子的面都是互不相同的,比如是xy。。ab),然后接下来我可以有1-i/m的概率掷到新的元素,比如序列变为(xy。。abc),

  即有1-i/m的概率可以转移到i+1这个状态

  同时,我各有可以有1/m的概率分别转移到(i,i-1,i-2,。。,1)这些状态,比如序列变为(xy。。abx,即转为i状态!!!),

  所以状态转移为:dp[i]=(1-i/m)*dp[i+1]+1/m*(dp[i]+dp[i-1]+..+dp[1])+1;  (__dp[n]=0__);

  然后就是n-1个方程递推下求解啦(这别要细心奥!!);

 #include<stdio.h>

 int m,n;
void DP1()
{
int i;
double ans,a,b;
a=1.0*(m-)/m;
b=1.0;
for(i=;i<=n-;i++)
{
a=a*1.0/m+1.0*(m-)/m;
b=b/m+;
}
if(n==)ans=1.0;
else ans=b/(-a)+;//b/(1-a)为dp[1];
printf("%.9f\n",ans);
} void DP2()
{
int i;
double ans=1.0,tmp=1.0;
for(i=;i<=n-;i++)//找到递推关系求解!!
{
tmp=tmp*m/(m-i);
ans+=tmp;
}
printf("%.9f\n",ans);
} int main()
{
int T,i,op; while(~scanf("%d",&T))
{
while(T--)
{
scanf("%d%d%d",&op,&m,&n);
if(op==)
DP1();
else
DP2();
} }
}

hdu 4625 Dice(概率DP)的更多相关文章

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

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

  2. 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 ...

  3. hdu 4652 Dice 概率DP

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

  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. js刷新当前页面的5种方式

    1. reload reload 方法,该方法强迫浏览器刷新当前页面.语法:location.reload([bForceGet])   参数: bForceGet, 可选参数, 默认为 false, ...

  2. leetcode-mid-Linked list-2 Add Two Numbers

    mycode 87.22% # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x) ...

  3. 【洛谷T2695 桶哥的问题——吃桶】

    这是我们团队的一个题目(就是一个_rqy说很好写的题QwQ) 题目背景 @桶哥 这个题目的思路很玄学(性感_rqy在线讲解) 60 Pts 对于前面的六十分,好像很好拿,单纯的打一个模拟 唯一需要注意 ...

  4. 错误 warning: LF will be replaced by CRLF in README.md.

    问题类型 windows中的换行符为 CRLF, 而在Linux下的换行符为LF,所以在执行add . 时出现提示:warning: LF will be replaced by CRLF in RE ...

  5. Git 实践

    最近也学习了Git的相关知识,现通过一个实例来记录Git使用流程,也方便日后使用. git的基础学习: https://www.yiibai.com/git/git-quick-start.html ...

  6. 《Python Data Structures》 Week4 List 课堂笔记

    Coursera课程<Python Data Structures> 密歇根大学 Charles Severance Week4 List 8.2 Manipulating Lists 8 ...

  7. MVC 源码系列之控制器执行(二)

    ## 控制器的执行 上一节说道Controller中的ActionInvoker.InvokeAction public virtual bool InvokeAction(ControllerCon ...

  8. C#实体类生成Create Table SQL

    using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespac ...

  9. SoapUI乱码问题处理方法

    前言 每个工具都会有一些意想不到的“坑”,SoapUI也不例外.无论是参数或者响应报文,大家可能都遇到过乱码问题,这里记录一下几种解决乱码的方法. 一.修改显示字体 在File>>> ...

  10. redis 设置密码 和 redis.config文件