Introduction to probability DP

Source: Internet
Author: User
Tags cmath

IProbability issues

1. A collection of algorithms: A Preliminary Study on Solving Probability Problems in informatics Competitions

2. Research on probability and expectation Problems

3. A collection of algorithms: An Analysis of A Class of mathematical expectation problems in the competition


2. Entry question

1. poj 3744 scout yyf I (simple question)
There are n mines on one road. A [I] represents the position where the I-th Mine is placed.

Analysis: if there is a mine at the K position, the safe way to walk through this position is to jump two steps in the K-1 position probability (1-p)

On the opposite side, the probability of death from the first ray of the I-1 is 1-P (from a [I-1] to a [I]). P = p * ans [I-1] + (1-p) * ans [I-2]

Ans [I] indicates the probability of going to the I position. Matrix optimization is required because N is large.
Solution report:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int a[20];struct matrix{    double m[2][2];};matrix I={1.0,0,0,1.0};matrix multi(matrix A,matrix B){    matrix C;    for(int i=0;i<2;i++){        for(int j=0;j<2;j++){            C.m[i][j]=0;            for(int k=0;k<2;k++)                C.m[i][j]+=A.m[i][k]*B.m[k][j];        }    }    return C;}double pow(int k,matrix A){    matrix ans = I,p=A;    while(k){        if(k&1){            ans=multi(ans,p);            k--;        }        k>>=1;        p=multi(p,p);    }    return ans.m[0][0];}int main(){    int n;    double p;    while(cin>>n>>p){        for(int i=1;i<=n;i++)            cin>>a[i];        a[0]=0;        sort(a,a+n+1);        matrix A={p,1.0-p,1.0,0};        double ans=1;        for(int i=0;i<n;i++){            ans*=(1-pow(a[i+1]-a[i]-1,A));        }        printf("%.7lf\n",ans);    }    return 0;}


2. poj 3071 football (simple question)
The elimination of football matches. Ask the team with the highest probability of victory. Compare the winning rate of each team with DP.
Solution report


3. codeforces 148d bag of mice (simple question)
Catch a mouse. Memory-based search is not difficult. Writing for loops is troublesome.
Solution report

 
4. poj 2151 check the difficulty of problems (simple question)
This is a common DP representation. Learn how to "at least" this type of problem angle conversion.
Solution report


5. zoj 3380 patchouli's spell cards (medium question)
There are m locations, and each location is filled in 1 ~ For a number in N, calculate the probability that at least l is the same. It's okay to transfer DP, that is, to use Java's high-precision solution report.


6. sgu 495 kids and prizes)
YY question, O (1) came to the conclusion that it was difficult to think of, and the analysis of the problem is worth learning.
Solution report


7. poj 2096 collecting bugs (simple question)
A person can only find one bug every day. One company has s subsidiaries and N bugs. Let this person find them, find out all the N bugs, and every company needs to find out the expected time required for the bug.

Analysis: getting started with DP. The key is to understand why we need to push it backwards.
DP [I] [J] indicates that J bugs have been found in I companies.

Note: E (AA + BB + CC + ..... N * n) = A * E (A) + B * E (B) + ....... + N * E (n );

The state transition equation is well understood.

DP [I] [J] = (DP [I + 1] [J] * P1 + dp [I] [J + 1] * P2 + dp [I + 1] [J + 1] + 1) /(1-p4 );

P1 = (s-I) * j/N/S;

P2 = I * (N-j)/n/s;

P3 = (s-I) * (N-j)/n/s;

P4 = I * j/N/S;

Solution report

#include <iostream>#include <cstring>#include <cstdio>using namespace std;const int maxn = 1010;double dp[maxn][maxn];int main(){    int n,s;    while(~scanf("%d%d",&n,&s)){        dp[s][n]=0;        for(int i=s;i>=0;i--){            for(int j=n;j>=0;j--){                if(i==s&&j==n)                continue;                dp[i][j]=(dp[i+1][j]*(s-i)*j*1.0/(s*n*1.0)+dp[i+1][j+1]*(s-i)*(n-j)*1.0/(s*n*1.0)+dp[i][j+1]*i*(n-j)*1.0/(s*n*1.0)+1.0)/(1.0-i*j*1.0/n/s);            }        }        printf("%.4lf\n",dp[0][0]);    }    return 0;}




8. HDU 3853 loops (simple question)
Same as 6th, but pay attention to a small problem

A map of R * C. A person can stay in the same place or consume two physical resources at a time in (), or go down to a grid to the left. (R, c) expectation of physical strength consumed

Analysis: DP [I] [J] indicates the expectation of physical strength consumed from (I, j) to the end.

DP [I] [J] = (DP [I + 1] [J] * P1 + dp [I] [J + 1] * P2 + 2)/(1-p3)

P1 is the probability of going right, P2 is the probability of going down, and P3 is the probability of moving

Note: E (AA + BB + CC + ..... N * n) = A * E (A) + B * E (B) + ....... + N * E (n); expectations of States A, B, and C can be decomposed into subexpected solutions.

Solution report

#include <iostream>#include <cmath>#include <cstdio>#include <cstring>using namespace std;const int maxn = 1002;double map[maxn][maxn][3];double dp[maxn][maxn];int main(){    int r,c;    while(~scanf("%d%d",&r,&c)){        for(int i=1;i<=r;i++)            for(int j=1;j<=c;j++)            scanf("%lf%lf%lf",&map[i][j][0],&map[i][j][1],&map[i][j][2]);        memset(dp,0,sizeof(dp));        for(int i=r;i>=1;i--){            for(int j=c;j>=1;j--){                if(i==r&&j==c)                    continue;                if(map[i][j][0]==1.00)                    continue;                dp[i][j]=(map[i][j][1]*dp[i][j+1]+map[i][j][2]*dp[i+1][j]+2)/(1-map[i][j][0]);            }        }        printf("%.3lf\n",dp[1][1]);    }    return 0;}


9. HDU 4405 aeroplane chess (simple question)
This is the subject of the 2012 cyber competition. The same as question 6th, but the question adds a small condition

Analysis: DP [I] = 1/6 (DP [I + 1] + .... + dp [I + 6] + 1) if I can directly reach K, DP [I] = DP [k]; DP [I] indicates the expected number of tokens to be thrown from the I to the end.
Solution report

#include <iostream>#include <cstring>#include <cstdio>using namespace std;const int maxn = 100007;double dp[maxn];int f[1010][2];int main(){    int n,m;    while(~scanf("%d%d",&n,&m)){        if(n==0&&m==0)        break;        for(int i=0;i<m;i++)            scanf("%d%d",&f[i][0],&f[i][1]);        memset(dp,0,sizeof(dp));        double p = 1.0/6;        for(int i=n;i>=0;i--){            if(i>=n) continue;            dp[i]=(dp[i+1]+dp[i+2]+dp[i+3]+dp[i+4]+dp[i+5]+dp[i+6])*p+1;            for(int j=0;j<m;j++){                if(i==f[j][0]){                    dp[i]=dp[f[j][1]];                    break;                }            }        }        printf("%.4lf\n",dp[0]);    }    return 0;}


10. zoj 3640 help me escape (simple question)
After completing 6, 7, and 8 questions, you can think about the same questions. It is easy to think of memory-based search.
Solution report

#include<cstdio>  #include<cstring>  #include<cmath>  #define max(a,b) a>b?a:b  const int LMT=20003;  const double is=0.5*(1+sqrt(5));  double dp[LMT];  int have[102];  int main()  {      int i,n,f,j,all;      while(~scanf("%d%d",&n,&f))      {          memset(dp,0,sizeof(dp));          all=-1;          for(i=0;i<n;i++)          {              scanf("%d",&have[i]);              all=max(all,have[i]);          }          all<<=1;          for(i=max(all,f);i>=f;i--)          {              for(j=0;j<n;j++)                  if(i>have[j])dp[i]+=(int)(is*have[j]*have[j]);                  else dp[i]+=1+dp[i+have[j]];                      dp[i]/=n;          }          printf("%.3lf\n",dp[f]);      }      return 0;  }



11. zoj 3822 domination (simple question)

Given an R * C board, each step can be placed with a pawn. How many steps can each row have the expectation of a pawn?

Analysis: This question is 2014 2014 ACM-ICPC Asia Mudanjiang regional's question, in fact, as long as the probability DP's question is well written

DP [I] [J] [k] indicates that I have used a piece to make K columns in the I row have a piece.

DP [I] [J] [k] = (DP [I + 1] [J] [k] * P1 + dp [I + 1] [J + 1] [K] * P2 + dp [I + 1] [J] [k + 1] * P3 + dp [I + 1] [J + 1] [k + 1] * P4)

P1 = (J * k-I)/(R * c-I );
P2 = (R-j) * k/(R * c-I );

P3 = (c-k) * j/(R * c-I );

P4 = (R-j) * (c-k) * (R * c-I );

Solution report:

# Include <iostream> # include <cstring> # include <cstdio> using namespace STD; const int maxn = 51; double DP [maxn * maxn] [maxn] [maxn]; int main () {int R, C, T; scanf ("% d", & T); While (t --) {scanf ("% d ", & R, & C); memset (DP, 0, sizeof (DP); For (INT I = r * C-1; I> = 0; I --) {for (Int J = r; j> = 0; j --) {for (int K = C; k> = 0; k --) {If (j = R & K = c) continue; // determine whether the denominator is 0 if (J * k <I) continue; // determine whether the numerator is negative. Double p1 = 1.0 * (J * k-I)/(R * c-I); double P2 = 1.0 * (R-j) * k/(R * c-I); double P3 = 1.0 * (c-k) * j/(R * c-I ); double P4 = 1.0 * (R-j) * (c-k)/(R * c-I); // cout <"p1" <P1 <Endl; // cout <"p2" <P2 <Endl; // cout <"P3" <P3 <Endl; // cout <"P4" <P4 <Endl; DP [I] [J] [k] = DP [I + 1] [J] [k] * P1 + dp [I + 1] [J + 1] [k] * P2 + dp [I + 1] [J] [k + 1] * P3 + dp [I + 1] [J + 1] [k + 1] * P4 + 1.0 ;}}} printf ("%. 8lf \ n ", DP [0] [0] [0]);} return 0 ;}


12. HDU 4336 card collector (simple question)
State compression probability DP. You can also use the principle of rejection to directly find out.
There are N types of cards. If you buy a pack of snacks, you may not get one of them. The probability of getting a pack of snacks is pi, collect all the requirements of card purchases for snacks

Since N is relatively small, we can perform State compression.

The location where DP [I] is converted into binary 1 represents the expectation for collecting the snacks to be purchased.

Eg: DP [(1101) 2] = (DP [(1100) 2] * P1 + dp [(1001) 2] * P3 + dp [(0101) 2] * P4 + 1)/(1-p3-p (no cards in snacks ))

Solution report

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int maxn = 1<<20;double dp[maxn+1];double p[25];int main(){    int n;    while(~scanf("%d",&n)){        p[0]=1;        for(int i=1;i<=n;i++){            scanf("%lf",p+i);            p[0]-=p[i];        }        dp[0]=0;        double x;        for(int i=1;i<(1<<n);i++){            x=p[0];            dp[i]=1;            for(int j=0;j<n;j++){                if((1<<j)&i) dp[i]+=p[j+1]*(dp[i-(1<<j)]);                else x+=p[j+1];            }            dp[i]/=(1-x);        }        printf("%.4lf\n",dp[(1<<n)-1]);    }    return 0;}



13. zoj 3329 one person game (medium question -)
The recursive formula is a little troublesome for the dice to find the expected question.
Solution report


14. HDU 4652 dice (medium question -)
DP expected to push mathematical formulas. After the last 5th multi-school questions, it was found that it was a very basic question. Unfortunately, I did not learn it, and I was able to launch it with high school knowledge.
Solution report
 

15. HDU 4035 maze (medium question +)
The DP recursion on the tree, after writing the recursive relational equations, uses DFS to calculate the iterative equation from the leaf node of the tree.
Solution report


16. HDU 4089 activation (medium)

The question of the 2011 Beijing field competition, where the question of probability is obtained using the expected method, is a little complicated and can be made after careful consideration,

Solution report



17. HDU 4418 time travel (medium)

Gauss's elimination of Yuan is a learning term, aiming to help people understand the meaning of the question. After understanding the meaning, it is a very bare Gaussian elimination element (using a program to solve the equation), but there are still many pitfalls.


I only did some questions, and the rest will be updated later...

Introduction to probability DP

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.