Simple probability dp-hdu-4487-Maximum Random Walk

Source: Internet
Author: User

Question:
The start position is 0. Each step can be left or not moved to the right. After n steps are taken, the path can reach the rightmost expectation.

Solution:

During the competition, the question was wrong and thought it was necessary to return to the starting point. -_--_-

The last location is unknown, and the rightmost distance of each path is not determined.

Therefore, dp [I] [j] [k] indicates the probability that step I is taken, and the rightmost position in the path is k.

Obviously k> = j; otherwise, it is 0.

If k = j, there are two situations in this step: 1. the maximum is reached for the first time. 2. The maximum has been reached previously. Note that at this time, you cannot go from the right to the left, more than k.

If k> j, it indicates that this step has not reached k, but the previous step has reached k.

 

S is the probability of not moving. r and l are the probability of going right and left respectively.

The start position of the log is 100.

Code:

<SPAN style = "FONT-SIZE: 18px ">#include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # include <set> # include <stack> # include <list> # include <queue> # define eps 1e-6 # define INF 0x1f1f1f1f # define PI acos (-1.0) # define ll _ int64 # define lson l, m, (rt <1) # define rson m + 1, r, (rt <1) | 1 // # pragma comment (linker, "/STACK: 1024000000,1024000000") using namespace std;/* freopen ("data. in "," r ", stdin); freopen (" data. out "," w ", stdout); */# define N 110 double dp [2] [N * 2] [N * 2]; // dp [] [I] [j] indicates the probability int main () {int t, d, n when the current step arrives at I, and the rightmost position is j; double l, r, s; scanf ("% d", & t); while (t --) {scanf ("% d % lf", & d, & n, & l, & r); s = 1-l-r; memset (dp, 0, sizeof (dp); dp [0] [100] [100] = 1; // initialize int la = 0, cur; for (int I = 1; I <= n; I ++) {cur = la ^ 1; for (int j = 100-i; j <= 100 + I; j ++) {for (int k = max (100, j); k <= 200; k ++) // k must be greater than or equal to j. The first step is in 100. No matter how the rightmost step is taken, it must be greater than 100. {if (k = j) // This step goes to the rightmost dp [cur] [j] [k] = dp [la] [j] [k] * s + dp [la] [J-1] [k-1] * r + dp [la] [J-1] [k] * r; else // k> j has reached the maximum k dp [cur] [j] [k] = dp [la] [j] [k] * s + dp [la] [J-1] [k] * r + dp [la] [j + 1] [k] * l ;}} la = cur;} double ans = 0; for (int I = (100-n); I <= 100 + n; I ++) for (int j = 100; j <= 100 + n; j ++) ans = ans + dp [cur] [I] [j] * (j-100 ); // directly obtain the desired printf ("% d %. 4lf \ n ", d, ans);} return 0 ;}</SPAN>

 

Related Article

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.