Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4870
Assume that you use two accounts to participate in a competition. In the initial state, both accounts have zero points. Each game uses a low-score account to compete. A probability of winning a game is P, the score of the corresponding account increases by 50 points. Otherwise, the score of the corresponding account decreases by 100 points. Ask the expected number of times of participation in the competition when the score of one account reaches 1000 points.
Train of Thought: During the competition, I thought it was a question to push the formula, and there was no result after I pushed it for half a day. After the game, I thought about it. After reading the question report, I learned that the corresponding status of each score was pushed by more than two States, and that Gaussian elimination element can be used to solve the problem instead of pushing forward from the back. Status 0 ~ Status 209 indicates that the score of a person is (0, 0), (50, 0)... (950,950), and the expected score of an account is 1000. E (x, y) = P (E (x1, Y1) + 1) + (1-p) (E (X2, Y2) + 1), (x1, Y1) it indicates the scores of two accounts after the competition, and (X2, Y2) indicates the scores of the two accounts after the competition fails. Status does not need to be included in the status (1000 ,?) This is because the status does not appear except (1000,950. The (1000,950) status is only related to the status (950,950), and (950,950) will not be obtained from (1000,950). Therefore, no status in the 210 status is related to (1000 ,?) Status.
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <map>#include <cstdlib>#include <queue>#include <stack>#include <vector>#include <ctype.h>#include <algorithm>#include <string>#include <set>#define PI acos(-1.0)#define maxn 210#define INF 0x7fffffff#define eps 1e-8#define MOD 1000000009typedef long long LL;typedef unsigned long long ULL;using namespace std;double a[220][220],b[220];int all[25];double gauss_elimination(int n){ int i,j,k,row; double maxp,t; for(k=0; k<n; k++) { for(maxp=0,i=k; i<n; i++) if (fabs(a[i][k])>eps) { maxp=a[row=i][k]; break; } if(fabs(maxp)<eps) return 0; if(row!=k) { for(j=k; j<n; j++) swap(a[k][j],a[row][j]); swap(b[k],b[row]); } for (int j = 0; j < n; j++) { if (k == j) continue; if (fabs(a[j][k]) > eps) { double x = a[j][k] / a[k][k]; for (int i = k; i < n; i++) { a[j][i] -= a[k][i] * x; } b[j] -=b[k]*x; } } } return 1;}int init(){ all[0]=0; for(int i=1; i<=21; i++) { all[i]=all[i-1]+i; } return 0;}int main(){ double p; init(); while(~scanf("%lf",&p)) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); for(int i=0; i<20; i++) { for(int j=0; j<=i; j++) { a[all[i]+j][all[i]+j]+=1; b[all[i]+j]+=1; a[all[i]+j][all[i]+max(j-2,0)]+=(p-1); if(j+1<=i) a[all[i]+j][all[i]+j+1]+=(-p); else { if(i==19&&j==19) continue; else a[all[i]+j][all[j+1]+i]+=(-p); } } } gauss_elimination(210); printf("%.6lf\n",b[0]/a[0][0]); } return 0;}