HDU 4652 Dice (概率DP)

B - Dice
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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≤10 6) 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
 
先附個大牛的網址:http://blog.csdn.net/ten_three/article/details/18924459
 
第一個圖片中最後四,五行中的 1/m 應爲 m .... 圖片裏的不對
 
 
#include <cstdio>  
#include <iostream>  
#include <cstring>  
#include <algorithm>  
#include <cmath>  
using namespace std;  
double solve0(int m,int n)  
{  
    double ans=0;  
    for(int i=0;i<=n-1;i++)  
        ans+=pow(1.0*m,i);  
    return ans;  
}  
double solve1(int m,int n)  
{  
    double ans=0;  
    double tmp=1;  
    for(int i=1;i<=n;i++)  
    {  
        ans+=tmp;  
        tmp*=(m+0.0)/(m-i);  
    }  
    return ans;  
}  
int main()  
{  
    int t;  
    while(scanf("%d",&t)!=EOF)  
    {  
        while(t--)  
        {  
            int n,m,op;  
            scanf("%d%d%d",&op,&m,&n);  
            if(op==0)  
                printf("%.9lf\n",solve0(m,n));  
            else  
                printf("%.9lf\n",solve1(m,n));  
        }  
    }  
    return 0;  
}  

 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章