HDU-4326-game-probability DP

Source: Internet
Author: User

The answer to this question is very simple. N people are given each time, 4 people are playing the game, others are waiting, the winner continues, and the loser is ranked at the end. Those who win or win m times in a row become the final winner, what is the probability that the k-th person will eventually win?

For this question, we should first establish a model like this: X1 won the I game first, and is playing the game on X2, X3, and X4, followed by X5, X6 ,......, XN wait. P [I] [J] indicates the probability that the current XJ wins when X1 wins the I first.

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4326
I <m

P [I] [J] = P [I + 1] [J] * 1/4 + P [1] [n-2] * 3/4 j = 1

P [I + 1] [n-2] * 1/4 + P [1] [1] * 1/4 + P [1] [n-1] * 2/4 J = 2

P [I + 1] [n-1] * 1/4 + P [1] [1] * 1/4 + P [1] [n-1] * 1/4 + P [1] [N] * 1/4 J = 3

P [I + 1] [N] * 1/4 + P [1] [1] * 1/4 + P [1] [N] * 2/4 J = 4

P [I + 1] [J-3] * 1/4 + P [1] [J-3] * 3/4 j> = 4

I = m

P [I] [J] = 1 j = 1

0 J! = 1

Use the fractional form Gaussian elimination element to solve the equation.

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>using namespace std;#define maxn 102#define eps 1e-10double g[maxn][maxn];double x[maxn];int n,m,k;void add(int cnt,int i,int j,double val){    int t=i*n+j;    if(i==m){        if(j==1)    g[cnt][n*m+1]+=-1.0*val;        return;    }    g[cnt][t]+=val;}void gauss(int n,int m){    int row,col,i,j,k;    for(row=1,col=1;row<n,col<m;row++,col++){        k=row;        for(i=row+1;i<=n;i++)            if(fabs(g[i][col])>fabs(g[k][col])) k=i;        if(k!=row){            for(i=col;i<=m;i++) swap(g[k][i],g[row][i]);        }        if(fabs(g[row][col])<eps)   continue;        for(i=row+1;i<=n;i++){            if(fabs(g[i][col])<eps) continue;            double t=g[i][col]/g[row][col];            g[i][col]=0.0;            for(j=col+1;j<=m;j++)   g[i][j]-=t*g[row][j];        }    }    for(i=n;i>=1;i--){        x[i]=g[i][m];        for(j=i+1;j<=n;j++)    x[i]-=x[j]*g[i][j];        x[i]/=g[i][i];    }}int main(){    int i,j,cs,nn=0;    scanf("%d",&cs);    while(cs--){        scanf("%d%d%d",&n,&m,&k);        memset(g,0,sizeof(g));        int cnt=0;        for(i=0;i<m;i++)            for(j=1;j<=n;j++){                cnt++;                add(cnt,i,j,1.0);                if(j==1){                    add(cnt,i+1,j,-0.25);                    add(cnt,1,n-2,-0.75);                }                else if(j==2){                    add(cnt,i+1,n-2,-0.25);                    add(cnt,1,1,-0.25);                    add(cnt,1,n-1,-0.5);                }                else if(j==3){                    add(cnt,i+1,n-1,-0.25);                    add(cnt,1,1,-0.25);                    add(cnt,1,n-1,-0.25);                    add(cnt,1,n,-0.25);                }                else if(j==4){                    add(cnt,i+1,n,-0.25);                    add(cnt,1,n,-0.5);                    add(cnt,1,1,-0.25);                }                else{                    add(cnt,i+1,j-3,-0.25);                    add(cnt,1,j-3,-0.75);                }            }        gauss(n*m,n*m+1);        printf("Case #%d: %.6lf\n",++nn,x[k]);    }    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.