HDU 4652 Dice (probability dp)
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
Refer to others' blogs: http://blog.csdn.net/auto_ac/article/details/9919851
#include
#include
#include
#include#include
#include
#include
#include
#include
#include
#define L(x) (x<<1)#define R(x) (x<<1|1)#define MID(x,y) ((x+y)>>1)#define eps 1e-8typedef __int64 ll;#define fre(i,a,b) for(i = a; i
= a;i--)#define mem(t, v) memset ((t) , v, sizeof(t))#define ssf(n) scanf("%s", n)#define sf(n) scanf("%d", &n)#define sff(a,b) scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf printf#define bug pf("Hi\n")using namespace std;#define INF 0x3f3f3f3f#define N 10005int n,m;double pp;void solve(){int i,j; double ans=0; fre(i,0,n) ans+=pow(pp,i);pf("%.7f\n",ans);}void solvee(){ int i,j; double t=1; double ans=0; double up=m,down=m; double temp=up/down; fre(i,0,n) { ans+=temp; down--; temp*=up/down; } pf("%.7f\n",ans);}int main(){ int i,j,t; while(~sf(t)){int op;while(t--){sfff(op,m,n);if(op==0){pp=m; solve();}else{solvee();}}} return 0;}