HDU 4652 Dice (概率DP)

传送门
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

题目大意:
给你一个骰子,这个骰子一共有 m 个面,给你一个操作数op,如果 op == 0的话让你求的就是最后扔的骰子的 n 个连续相同的扔的次数的期望,如果 op == 1的话就是让你求的 n 个连续不同的扔的次数的期望。

解题思路:
看到这个题的话,第一反应肯定就是概率DP,然后肯定是分两种情况:
在第一种情况的条件下(n个连续相同):
设 DP[i]表示的就是已经有 i 个 连续相同的面了,然后到 n 个连续相同的面的期望。

那么可以分析一下DP[n]一定是0;

DP[0]=1+DP[1]
有0个相同的然后后面必定是一个相同的 概率是1
DP[1]=1+1mDP[2]+(m1)mDP[1]

已经有一个连续相同的了,那么之后如果是连续两个的话,概率是 1/m,否则的话还是一个连续的面概率就是(m-1)/m;

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

其实道理都是一样的…
那么可以推一下公式就是DP[i]的状态转移方程
DP[i]=1+1mDP[i+1]+(m1)mDP[1]

设a[i] = DP[i] - DP[i+1] ,那么:
a[i]=1/m(DP[i+1]DP[i+2])=1/ma[i]

然后可以发现a[i+1] = m*a[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+m^2+..+m^(n-1)(等比数列求和公式可得)

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

在第二种情况的条件下(n个连续不相同):
设 DP[i]表示的就是已经有 i 个 连续不相同的面了,然后到 n 个连续不相同的面的期望。
这里直接给一下公式吧:

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])

还是令a[i] = DP[i]-DP[i+1]
得到的公式就是
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)

左边相加,右边相加,DP[n]还是=0,那么DP[0]就能求了
这个可以通循环写:
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;
}

你可能感兴趣的:(概率DP)