【期望DP】 - HDU 5502 Candy Game

f[i][k]表示长度为i,最大连续蓝球长度不超过k的概率

g[i]表示长度为n,最大连续蓝球长度不超过i的概率

转移方程:

1. i<=n时,f[i][k]=f[i-1][k]*p+f[i-1]*(1-p)

2. i>=k时,f[i][k]-=f[i-k][k]*p^k

3. g[i]=f[n+1][i]

f[i][k]可以降维

 1 // #include <bits/stdc++.h>
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include <cctype>
 6 #include <cmath>
 7 #include <cstring>
 8 #include <queue>
 9 #define For(i,a,b) for(int i=a;i<=b;++i)
10 #define Dec(i,b,a) for(int i=b;i>=a;--i)
11 #define file() freopen("c://users/asus/desktop/v.txt","r",stdin)
12 #define inf 0x3f3f3f3f
13 using namespace std;
14 
15 typedef unsigned long long ll;
16 inline ll qr(){
17     ll x=0,f=1; char ch=getchar();
18     while(!isdigit(ch)){if(ch=='-')f=-1; ch=getchar();}
19     while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48); ch=getchar();}
20     return x*f;
21 }
22 double f[1510],g[1510],p,q;
23 int n;
24 double dp(int x)
25 {
26     f[0]=1;
27     double t=f[0],k=pow(p,x);
28     For(i,1,n+1)
29     {
30         f[i]=t;
31         if(i<=n) f[i]*=1-p;
32         t=t*p+f[i]; 
33         if(i>=x) t -= f[i-x]*k;
34     }
35     return f[n+1];
36 }
37 int main()
38 {
39     // file();
40     int kase=qr(); while(kase--)
41     {
42         scanf("%d%lf%lf",&n,&p,&q);
43         p=q/p;
44         For(i,1,n+1) g[i]=dp(i);
45         double ans=0;
46         For(i,1,n+1) ans+=i*(g[i]-g[i-1]);
47         printf("%.3lf\n", ans);
48     }
49     return 0;
50 }
View Code

猜你喜欢

转载自www.cnblogs.com/uuuxxllj/p/10874683.html
今日推荐