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. Python List 列表list()方法

    Python基础数据类型之一列表list,在python中作用很强在,列表List可以包含不同类型的数据对像,同时它是一个有序的变量集合,每个变量可以存储一个地址.所有序列能用到的标准操作方法,列表也 ...

  2. leetcode-mid-array-5. Longest Palindromic Substring

    mycode   12.51% class Solution(object): def longestPalindrome(self, s): """ :type s: ...

  3. Oracle Flashback Transaction Query with Oracle Flashback Version Query

    Oracle Flashback Transaction Query with Oracle Flashback Version Query In this example, a database a ...

  4. Oracle 11g客户端下载地址

    Oracle 11g客户端下载地址: http://www.cr173.com/soft/36349.html

  5. Delphi XE2 之 FireMonkey 入门(10) - 常用结构 TPoint、TPointF、TSmallPoint、TSize、TRect、TRectF 及相关方法

    它们都是结构, TPointF.TRectF 属新增, 其它也都有升级; 现在都拥有丰富的方法和方便的运算符重载; 且有一组相关的公共函数. 这组内容重要的是它们都来自 System.Types 单元 ...

  6. 发邮件--yagmail模块

    准备工作:1.在你的邮箱设置里面打开smtp服务(若有的话)2.开启邮箱授权码,记住这个授权码(连接邮箱服务时用) 1.安装yagmail模块pip install yagmail2.举例:impor ...

  7. Java ——Number & Math 类 装箱 拆箱 代码块

    本节重点思维导图 当需要使用数字的时候,我们通常使用内置数据类型,如:byte.int.long.double 等 int a = 5000; float b = 13.65f; byte c = 0 ...

  8. if you don't go after what you want, you'll never have it

    conquest.n. 征服 quantitative: adj. 数量的 cellar: n. 地窖 roast. v. 烧烤 allowance. n. 津贴 drainage. n. 排水 ma ...

  9. 设置国内AndriodSDK代理

    由于一些原因,Google相关很多服务都无法访问,所以在很多时候我们SDK也无法升级,当然通过技术手段肯定可以解决,但是比较麻烦,而且下载速度也不怎么样. 这里笔者介绍一个国内的Android镜像站, ...

  10. 如何在Web工程中实现任务计划调度

    转载自: http://www.oschina.net/question/146385_37793?sort=time 下面就Servlet侦听器结合Java定时器来讲述整个实现过程.要运用Servl ...