ZOJ 3329 One person Game probability dp Good question

Source: Internet
Author: User

One person Game Time limit: 1 Second Memory Limit: 32768 KB Special Judge

there is a very simple and interesting One-person game. You have 3 dice, namely  Die1 ,   Die2  and  Die3 .   Die1  has  K1  faces.  Die2  has  K2  faces.  Die3  has  K3 faces. All the dice is fair dice, so the probability of rolling each value, 1 to  K1 ,   K2 ,   K3  is exactly 1/  K1 , 1/  K2  and 1/  K3 . You had a counter, and the game is played as follow:

    1. Set the counter to 0 at first.
    2. Roll the 3 dice simultaneously. If the up-facing number of Die1 is a, the up-facing number of Die2 is b and the UP-FAC ing number of Die3 is C, set the counter to 0. Otherwise, add the counter by the total value of the 3 up-facing numbers.
    3. If The counter ' s number is still not greater than n, go to step 2. Otherwise the game is ended.

Calculate the expectation of the Times that you cast dice before the end of the game.

Input

There is multiple test cases. The first line of input was an integer T (0 < T <=) indicating the number of test cases. Then T test Cases follow.  Each test case was a line contains 7 non-negative integers n, K1, K2, K3, a, b, C (0 <= n <=, 1 < K1, K2, K3 <= 6, 1 <= a <= K1, 1 <= b <= K2, 1 <= c <= k3 >).

Output

For each test case, the output of the answer. A relative error of 1e-8 would be accepted.

Sample Input

20 2 2 2 1 1 10 6 6 6 1 1 1

Sample Output

1.1428571428571431.004651162790698

Author: CAO, Peng
Source: The 7th Zhejiang Provincial Collegiate Programming Contest

Test instructions: There are three evenly-spaced dice, each with a k1,k2,k3 face, and your initial score is 0,
When the number of three dice is a,b,c, the score is zeroed, otherwise the score plus three dice of the points and
When the score >n the end. The desired number of times to roll the dice.

This problem is really good, write your own ideas in front of the code.

1 /*2 3 Dp[i] Indicates the number of steps expected when the current point is I and the state of the point >n is reached4 obviously, when I>n, dp[i]=0.05 set k= (double) k1*k2*k3 for easy calculation6 the number of points I plus the cast point after the J7 then: dp[i]=1.0/k* (dp[0]+1) +1/k*[∑dp[j]+1]//Here is the summation of k-1, K possible-1 cases8 =1/k*dp[0]+1/k+1/k*∑dp[j]+ (k-1)/k9 =1/k*dp[0]+1/k*∑dp[j]+1Ten  One can see, here Dp[i] and dp[j] and dp[0] about, dp[j] and dp[0] about A and what we're asking for is dp[0], then dp[0] is a fixed value - There is no way to get dp[0 directly by recursion. -  the then set Dp[i]=a[i]*dp[0]+b[i] - with: Dp[j]+a[j]*dp[0]+b[j] - ∑dp[j]=dp[0]*∑a[j]+∑b[j] - the formula brought into the dp[i] is: + dp[i]=1/k*dp[0]+1/k*dp[0]*∑a[j]+1/k∑b[j]+1 - =1/k (1+∑a[j]) dp[0]+1/k∑b[j]+1 +  A so that we can eliminate the dp[j of the initial equation . at also get: - when I>n, - a[i]=0 - b[i]=0 - when I<=n, - a[i]=1/k (1+∑a[j]) in b[i]=1/k∑b[j]+1 - so the array, a, B, can be passed by recursion. to Where j refers to the number of points I can be reached by adding the number of points to the dice, pay attention to the a,b,c when cast not count + Then there are: Dp[0]=a[0]*dp[0]+b[0] - dp[0]=b[0]/(1.0-a[0]) the        * In this way, as long as the a[0],b[0 is handed out], can be brought into the dp[0] $ Panax Notoginseng Note: - The number of points clear 0 is when the K1,K2,K3 cast the points are a,b,c the not when the sum of the points is a+b+c. +  A because this WA 2 times. the  + */ -  $  $#include <cstdio> -  - Const intmaxn= -; the  - DoubleA[MAXN];Wuyi DoubleB[MAXN]; the DoubleK; - intK1,k2,k3; Wu inta,b,c; -  About DoubleSolveintN) $ { -      for(inti=n+1; i<n+ -; i++) -     { -a[i]=0.0; Ab[i]=0.0; +     } the  -      for(inti=n;i>=0; i--) $     { the         DoubleCnta=0.0; the         Doublecntb=0.0; the          for(intj1=1; j1<=k1;j1++) the         { -              for(intJ2=1; j2<=k2;j2++) in             { the                  for(intj3=1; j3<=k3;j3++) the                 { About                     //It's starting to be written J1+j2+j3==a+b+c,wa 2 times. the                     if(j1==a&&j2==b&&j3==c) the                         Continue; thecnta+=a[i+j1+j2+J3]; +cntb+=b[i+j1+j2+J3]; -                 } the             }Bayi         } theA[i]= (1.0+cnta)/K; theb[i]=cntb/k+1.0; -     } -  the     returnb[0]/(1.0-a[0]); the } the  the intMain () - { the     inttest; thescanf"%d",&test); the      while(test--)94     { the         intN; thescanf"%d",&n); thescanf"%d%d%d",&k1,&k2,&K3);98scanf"%d%d%d",&a,&b,&c); About  -K= (Double) k1*k2*K3;101 102printf"%.15f\n", Solve (n));103     }104     return 0; the}
View Code

ZOJ 3329 One person Game probability dp Good question

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.