[DP] expected [UVA1498] Activation

Obviously DP probability

We use probability size dp [i] [j] i represents the team has personal, lyk little girl lost in a row now at this location j

The following list is not difficult transition equation:

(Apparently has lined up in front of the k-th position is the time to add the explosion is the probability p4)


 

$$f[i][1]=f[i][1]*p1+f[i][i]*p2+p4$$
$$f[i][j]=f[i][j]*p1+f[i][j-1]*p2+f[i-1][j-1]*p3+p4(j∈[2,k])$$
$$f[i][j]=f[i][j]*p1+f[i][j-1]*p2+f[i-1][j-1]*p3(j∈[k+1,i])$$

 


 


This is a recursive process from front to back, but very jarring is: f [i] [1] of Recurrence occurred $ f [i] [i] $, obviously we find a way to put stuff to eliminate child

First simplification, the merger of similar items on both sides, and the result is this:

 


 

$$f[i][1]=f[i][i]*\frac{p2}{1-p1}+\frac{p4}{1-p1}$$
$$f[i][j]=f[i][j-1]*\frac{p2}{1-p1}+f[i-1][j-1]*\frac{p3}{1-p1}+\frac{p4}{1-p1}(j∈[2,k])$$
$$f[i][j]=f[i][j-1]*\frac{p2}{1-p1}+f[i-1][j-1]*\frac{p3}{1-p1}(j∈[k+1,i])$$


 

Since recursion front to back, in seeking f [i] [j] is, f [i-1] [] values ​​we have all seek out, it can be seen as constant, p1, p2, p3, p4 clearly known, it is constant

So we can put stuff into equations as long as we have f [i] [i] seen as unknowns, in constant iteration can go on

Probably look something like this:

 



You may wish to make a = $ \ frac {p2} {1-p1} $, $ b = \ frac {p3} {1-p1} $, $ c = \ frac {p4} {1-p1} $

Provided $ C_j = f [i-1] [j-1] * c + d $

There: $$ F [I] [. 1] = F [I] [I] * A + C $$
$$ F [I] [2] = F [I] [. 1] * A + C_2 $$
( then f [i] [1] is substituted into the formula ②, ...... and so on)


 

Finally, we constant rearmost portion sol into an equation

Obviously be obtained $$ f [i] [i] = a ^ {i} * f [i] [i] + sol $$

That
$$ f [i] [i] = \ frac {sol} {1-a ^ i} $$

After filling out this all $ f [] [] $ Table This question will be solved qaq

However, because of space constraints, we have to squeeze out (the value f [i] [] only with f [i-1] [] relevant) f [] [] first dimension

Here oh ~ code for

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 inline int read(){
 4     int ans=0,f=1;char chr=getchar();
 5     while(!isdigit(chr)){if(chr=='-')f=-1;chr=getchar();}
 6     while(isdigit(chr)) {ans=(ans<<3)+(ans<<1)+chr-48;chr=getchar();}
 7     return ans*f;
 8 }const int M = 2005;int n,m,k;
 9 double f[2][M],p1,p2,p3,p4,a,b,c,v[M],p[M];
10 int main(){
11     while(~scanf("%d%d%d%lf%lf%lf%lf",&n,&m,&k,&p1,&p2,&p3,&p4)){
12         if(fabs(p4)<=1e-5) {puts("0.00000");continue;}//p4为0时显然不可能
13         a=p2/(1-p1),b=p3/(1-p1),c=p4/(1-p1);
14         v[0]=1;for(int i=1;i<=n;i++) v[i]=v[i-1]*a;//预处理a的i次方
15         p[1]=c;f[1][1]=p4/(1-p1-p2);
16         for(int i=2;i<=n;i++){
17             double sol=0;
18             for(int j=2;j<=k;j++) p[j]=f[i-1&1][j-1]*b+c;
19             for(int j=k+1;j<=i;j++) p[j]=f[i-1&1][j-1]*b;//求每一个方程式的常数项
20             for(int j=1;j<=i;j++) sol+=v[i-j]*p[j];//求最后一个式子f[i][i]=......的常数项
21             f[i&1][i]=sol/(1-v[i]);
22             f[i&1][1]=f[i&1][i]*a+c;//回代消元
23             for(int j=2;j<i;j++) f[i&1][j]=f[i&1][j-1]*a+p[j];//回去填表
24         }printf("%.5lf\n",f[n&1][m]);
25     }
26     return 0;
27 }

 

Guess you like

Origin www.cnblogs.com/zhenglw/p/11299699.html