# [HDU4652] Dice

E[i]表示已经有i连续个相同，到达目标的期望步数

E[i]=E[i+1]/m+(1-1/m)E[1].................... 1

E[i+1]=E[i+2]/m+(1-1/m)E[1]................ 2

1-2得 E[i]-E[i+1]=(E[i+1]+E[i+2])/m

设s[i]=E[i]-E[i+1]，

则s[0]=E[0]-E[1]=1，s[i+1]=m*s[i]，ans=sigma(s[i])(0<=i<n)

E[i]表示已经有i连续个不同，到达目标的期望步数

E[i]=simga(E[j])/m+((m-i)/m)E[i+1],(1<=j<=i).................. 1

E[i+1]=sigma(E[j])/m+((m-i-1)/m)E[i+2],(1<=j<=i+1)....... 2

1-2得 E[i]-E[i+1]=(m-i-1)/m*(E[i+1]-E[i+2])

设s[i]=E[i]-E[i+1]，

则s[0]=E[0]-E[1]=1，s[i+1]=m/(m-i-1)s[i]，ans=sigma(s[i])(0<=i<n)

 1 #include<bits/stdc++.h>
2 using namespace std;
3 #define maxn 1000005
4 int T,op,n;
5 double m,s[maxn];
6 void solve0(){
7     s[0]=1;
8     double ans=s[0];
9     for(int i=0;i<n-1;i++)
10         s[i+1]=s[i]*m,ans+=s[i+1];
11     printf("%.9lf\n",ans);
12 }
13 void solve1(){
14     s[0]=1;
15     double ans=s[0];
16     for(int i=0;i<n-1;i++)
17         s[i+1]=m/(m-i-1)*s[i],ans+=s[i+1];
18     printf("%.9lf\n",ans);
19 }
20 int main(){
21     scanf("%d",&T);
22     while(T--){
23         scanf("%d%lf%d",&op,&m,&n);
24         if(op==0)solve0();
25         else solve1();
26     }
27     return 0;
28 }
View Code

posted @ 2016-05-26 21:17  Ngshily  阅读(305)  评论(0编辑  收藏  举报