# HDU 4652 Dice （概率DP）

3950人阅读 评论(0)

Dice

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 470 Accepted Submission(s): 323
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
2013 Multi-University Training Contest 5

DP[0]=1+DP[1]

DP[1]=1+1mDP[2]+(m1)mDP[1]

DP[2]=1+1mDP[3]+(m1)mDP[1]

DP[i]=1+1mDP[i+1]+(m1)mDP[1]

a[i]=1/m(DP[i+1]DP[i+2])=1/ma[i]

DP[0]DP[1]=1DP[1]DP[2]=mDP[2]DP[3]=m2...DP[n1]DP[n]=m(n1)

DP[0]DP[n]=1+m+m2+..+m(n1)1+m+m2+..+m(n1)=1(1m)n1mDP[n]=0,DP[0]=1(1m)n1m

DP[0]=1+DP[1]DP[1]=1+m1mDP[2]+1mDP[1]DP[2]=1+m2mDP[3]+1m(DP[1]+DP[2])...DP[i]=1+m1mDP[i+1]+1m(DP[1]+DP[2]+..+DP[i])

DP[0]DP[1]=1DP[1]DP[2]=1m(m1)DP[2]DP[3]=1m(m1)m(m2)...DP[n1]DP[n]=1m(m1)m(m2)..m(mn+1)

My Code:（代码真少，推的是真累呀…）

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
int m, n;
double Solve0()
{
double ans = pow(m,n);
return 1.0*(ans-1)/(m-1);
}
double Solve1()
{
double sum = 0.0, ans = 1.0;
for(int i=1; i<=n; i++)
{
ans *= 1.0*m/(m-i+1);
sum += ans;
}
return sum;
}
int main()
{
int T, op;
while(~scanf("%d",&T))
{
while(T--)
{
scanf("%d%d%d",&op,&m,&n);
if(op == 0)
printf("%.8lf\n",Solve0());
else if(op == 1)
printf("%.8lf\n",Solve1());
}
}
return 0;
}


个人资料
等级：
访问量： 53万+
积分： 1万+
排名： 1605
博客专栏
 ITAK的ACM之路 文章：295篇 阅读：347420 ACM_组合数学 文章：27篇 阅读：33172 计算机专业的相关课程知识 文章：24篇 阅读：34097
友情推荐