HDU 4487 probability DP

Source: Internet
Author: User

It is simple to ask for the problem of state transfer equation;

Dp[i][j][k] means walk I step to J position the rightmost position is k so it's easy to know k>=j;

S: probability l go left probability R go right

if k==j;

Dp[i][j][k]=dp[i-1][j][k]*s+dp[i-1][j-1][k-1]*r+dp[i-1][j-1][k]*r;

if k!=j;

Dp[i][j][k]=dp[i-1][j][k]*s+dp[i-1][j-1][k]*r+sp[i-1][j+1][k]*r;

If the N value reaches 1000 and dp[1001][2001][2001] is not open, then the dynamic array is used, because the next step is only related to the previous one, so open dp[2][2001][2001].

#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <
algorithm> #include <cmath> #include <vector> #include <queue> using namespace std;
Double l,r,s;
Double dp[2][300][300];
    int main () {int t,n;
    scanf ("%d", &t);
        for (int i1=1;i1<=t;i1++) {scanf ("%d%d%lf%lf", &i1,&n,&l,&r);
        S=1-l-r;
        Memset (Dp,0,sizeof (DP));
        int cur=1;
        int last=0;
        Dp[0][100][100]=1;
            for (int i=1;i<=n;i++) {cur=last^1;
                    for (int j=100-i;j<=100+i;j++) {for (int k=max (100,J); k<=100+i;k++) {
                    if (j==k) dp[cur][j][k]=dp[last][j][k]*s+dp[last][j-1][k-1]*r+dp[last][j-1][k]*r;
                else dp[cur][j][k]=dp[last][j][k]*s+dp[last][j-1][k]*r+dp[last][j+1][k]*l; }} last=Cur
        } double sum=0; for (int i=100-n;i<= (100+n), i++) {for (int j=100;j<= (100+n); j + +) {su
            m+=dp[cur][i][j]* (j-100);
    }} printf ("%d%.4lf\n", i1,sum);
} return 0;
 }


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.