LightOJ - 1030 expected +dp

Topic link: https://vjudge.net/problem/25907/origin

In a cave, there are 1 to n positions in it, and each position has a certain amount of gold coins. You have a six-sided dice. At the beginning, you are at 1, and every time you roll a number, take a few steps forward.

If it exceeds n, shake it again, until you know n, ask you the expectation of the number of times you need to shake until n.

At the beginning, I used forward push, and it was wrong all the time. Here we need reverse push. We don't want to push forward first, and we will talk about it later.

We assume that Ei represents the expectation of gold that can be obtained from point i. If the distance from point i to n is >= 6, then Ei=(E(i+1)/6+E(i+2)/6+E(i +3)/6+...+E(i+6)/6).

If the distance is less than 6, Ei=(E(i+1)/6+E(i+2)/6+E(i+3)/6+...+En/6). Note: every time during reverse push The probability of an E is the same.

#include<stdio.h>
int n,m,k,t;
double dp[105];
int min(int a,int b)
{
    if(a<b)
    return a;
    return b;
}
intmain ()
{
    scanf("%d",&t);
    int count=0;
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        scanf("%lf",&dp[i]);
        for(int i=n-1;i>=1;i--)
        {
            int num=min(n,i+6);
            double sum=0;
            for(int j=i+1;j<=num;j++)
            {
                sum+=dp[j];
            }
            dp[i]=sum/(num-i)+dp[i];
        }
        printf("Case %d: %.7f\n",++count,dp[1]);
    }
    return 0;
}

Why can't I push forward, and now I give my senior's explanation, it feels very reasonable:

Why only back-to-front is consistent with reasoning
because the probability distribution from front to back is not uniform.
The probability of going from a[i-6] to a[i] is not 1/6, because a[i-6] can go to a[i-5] first, and then to a[i],

So in fact, the probability of a[i-6] to a[i] is lower than other values, because the probability of other values ​​to a[i] is to first calculate that a[i-6] did not successfully reach a[ i] probability.

 Can't start with 1 because the equation of state can only be
a[i]=1/6*a[i+1]+ 1/6*a[i+2]+…+1/6*a[i+6 ]
If we let the equation be
a[i]=1/6*a[i-1]+ 1/6*a[i-2]+…+1/6*a[i-6]
a[2] =a[1]+a[2]
a[3]=0.5a[1]+0.5a[2]+a[3]=a[1]+0.5a[2]+a[3]
Then
a[ 4]=1/3a[1]+1/3a[2]+1/3a[3]+a[4]=a[1]+0.5a[2]+1/3a[3]+a[4 ]
However actually a[4] is not expected
if 1 to 4.
Starting at 1, the probability of going to 2 is 1/3, and the probability of going to 3 is 1/3 plus the probability of 2 to 3, which is 0.5.
So the correct one is a[4]=a[1]+1/3a[2]+0.5a[3]+a[4].

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325292657&siteId=291194637