HDU 4652 Dice(概率dp)

来源:互联网 发布:ce系统导航软件 编辑:程序博客网 时间:2024/05/16 15:08

题目的大意是:dp[i]表示当前在已经投掷出i个不相同/相同这个状态时期望还需要投掷多少次。

求得到连续的相同的期望:是1->m^n的前n项和。这里的解释可以看这里:http://www.matrix67.com/blog/archives/3638

另一种情况:dp[i]=1+(dp[1]+……+dp[i]+(m-i)*dp[i+1])/m

可以迭代系数也可以系数做差:

dp[n]=0
令d[i]=dp[i]-dp[i+1]
则有d[i]=m*dp[i-1]/(m-i),d[0]=1

Dice

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 382    Accepted Submission(s): 248
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
60 6 10 6 30 6 51 6 21 6 41 6 6101 4534 251 1232 241 3213 151 4343 241 4343 91 65467 1231 43434 1001 34344 91 10001 151 1000000 2000
 

Sample Output
1.00000000043.0000000001555.0000000002.2000000007.60000000083.20000000025.58631582426.01599003715.17634116024.5410457699.027721917127.908330426103.9754552539.00349551515.0562044724731.706620396
 

#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-9///#define M 1000100#define LL __int64///#define LL long long#define INF 0x7ffffff#define PI 3.1415926535898using namespace std;const int maxn = 1010000;double dp[maxn];int main(){    int T;    while(cin >>T)    {        while(T--)        {            int u, n, m;            cin >>u>>m>>n;            if(!u)            {                printf("%.9lf\n", (pow(m,n)-1.0)/(m*1.0-1.0));                continue;            }            double sum = 1.0;            double d = 1.0;            for(int i = 1; i < n; i++)            {                d = 1.0*m/(m-i)*d;                sum += d;            }            printf("%.9lf\n",sum);        }    }    return 0;}


0 0
原创粉丝点击