Zoj 3329 one person game (probability of loops DP)

Source: Internet
Author: User

Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 3754


I started to read the wrong question and thought that I didn't go to A, B, and C. I had to add A + B + C on the basis of the original one. Then I came up with a formula, I did not expect that the sample has passed, so I could not debug it.


The formula is very good. If DP [I] is set, it indicates the expectation to be throttled when the current time is I, and can be transferred to two States: DP [0] and DP [I + K]. Transfer to DP [0]

The probability is P0, and the probability of transferring to DP [I + k] is PK. DP [I] = P0 * DP [0] + PK * DP [I + k] + 1. It is found that all DP [I] are related to DP [0.

The following derivation references: http://www.cnblogs.com/kuangbin/archive/2012/10/03/2710648.html

Set DP [I] = A [I] * DP [0] + B [I]. The following result is obtained from the right side of the above equation: DP [I] = Σ (Pk * A [I + k] * DP [0] + PK * B [I + k]) + dp [0] * P0 + 1 = (Σ (Pk * A [I + k]) + P0) DP [0] + Σ (Pk * B [I + k]) + 1; obvious a [I] = (Σ (Pk * A [I + k]) + P0) B [I] = Σ (Pk * B [I + k]) + 1 first recursion to obtain a [0] and B [0]. then DP [0] = B [0]/(1-A [0]);

Then, a [0] and B [0] are reversed.

#include <stdio.h>#include <iostream>#include <map>#include <set>#include <list>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>//#define LL __int64#define LL long long#define eps 1e-8#define PI acos(-1.0)using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 4010;double dp[10010];double A[600],B[600];double p[100];int main(){int test;int n,a,b,c;int k1,k2,k3;scanf("%d",&test);while(test--){scanf("%d %d %d %d %d %d %d",&n,&k1,&k2,&k3,&a,&b,&c);double p0 = 1.0/(k1*k2*k3);memset(p,0,sizeof(p));for(int i = 1; i <= k1; i++){    for(int j = 1; j <= k2; j++)    {        for(int k = 1; k <= k3; k++)        {            if(i != a || j != b || k != c)                        p[i+j+k] += p0;        }    }}memset(A,0,sizeof(A));memset(B,0,sizeof(B));for(int i = n; i >= 0; i--){    A[i] = p0,B[i] = 1;    for(int j = 1; j <= k1+k2+k3; j++)    {        A[i] += p[j]*A[i+j];        B[i] += p[j]*B[i+j];    }}printf("%.15lf\n",B[0]/(1-A[0]));}return 0;}



Zoj 3329 one person game (probability of loops 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.