One Person Game ZOJ - 3329(期望dp, 数学)

There is a very simple and interesting one-person game. You have 3 dice, namely Die1Die2 and Die3Die1 has K1 faces. Die2 has K2 faces. Die3 has K3 faces. All the dice are fair dice, so the probability of rolling each value, 1 to K1K2K3 is exactly 1 / K1, 1 / K2 and 1 / K3. You have 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-facing 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 number of times that you cast dice before the end of the game.

Input

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

Output

For each test case, output the answer in a single line. A relative error of 1e-8 will be accepted.

Sample Input

2
0 2 2 2 1 1 1
0 6 6 6 1 1 1

Sample Output

1.142857142857143
1.004651162790698


有三个骰子,每个骰子有k1, k2, k3面,如果第一个骰子是a, 第二个骰子是b, 第三个骰子是c时,计数器重置为0,否则计数器加上三个骰子的点数之和,当计数器的分数大于n时,游戏结束。
令dp[i] =, 表示从分数为 i 开始, 到游戏结束的步数期望为x, p[i] = x 表示计数器增加 i 的概率为 x, 特殊的 p[0] 表示计数器重置为 0 的概率。
那么有dp[i] = Σ(dp[i+k]p[k]) + dp[0]p[0] + 1
令两个函数A[i], B[i]为辅助函数,则有
1. dp[i] = Σ(dp[i+k]p[k]) + dp[0]p[0] + 1
2. dp[i] = A[i]dp[0] + B[i]
3. dp[i+k] = A[i+k]dp[0] + B[i]
将 3 代入 1 中并化简可以得到
4. dp[i] = Σ(A[i+k]p[k] + p[0]) * dp[0] + Σ(B[i+k]p[k]) + 1
通过 2 和 4 的比较可以得到
A[i] = Σ(A[i+k]p[k] + p[0])
B[i] = Σ(B[i+k]p[k]) + 1
发现 A[i] 和 B[i] 是一个递推式, 则可以 dfs 出 A[0] 和 B[0]
然后把 A[0], B[0] 代入 2 中
dp[0] = A[0]dp[0] + B[0]
dp[0] = B[0] / (1 - A[0])
也就是最后的期望。
  1 /*
  2           .
  3          ';;;;;.
  4         '!;;;;;;!;`
  5        '!;|&#@|;;;;!:
  6       `;;!&####@|;;;;!:
  7      .;;;!&@$$%|!;;;;;;!'.`:::::'.
  8      '!;;;;;;;;!$@###&|;;|%!;!$|;;;;|&&;.
  9      :!;;;;!$@&%|;;;;;;;;;|!::!!:::;!$%;!$%`    '!%&#########@$!:.
 10      ;!;;!!;;;;;|$$&@##$;;;::'''''::;;;;|&|%@$|;;;;;;;;;;;;;;;;!$;
 11      ;|;;;;;;;;;;;;;;;;;;!%@#####&!:::;!;;;;;;;;;;!&####@%!;;;;$%`
 12     `!!;;;;;;;;;;!|%%|!!;::;;|@##%|$|;;;;;;;;;;;;!|%$#####%;;;%&;
 13     :@###&!:;;!!||%%%%%|!;;;;;||;;;;||!$&&@@%;;;;;;;|$$##$;;;%@|
 14     ;|::;;;;;;;;;;;;|&&$|;;!$@&$!;;;;!;;;;;;;;;;;;;;;;!%|;;;%@%.
 15    `!!;;;;;;;!!!!;;;;;$@@@&&&&&@$!;!%|;;;;!||!;;;;;!|%%%!;;%@|.
 16 %&&$!;;;;;!;;;;;;;;;;;|$&&&&&&&&&@@%!%%;!||!;;;;;;;;;;;;;$##!
 17 !%;;;;;;!%!:;;;;;;;;;;!$&&&&&&&&&&@##&%|||;;;!!||!;;;;;;;$&:
 18 ':|@###%;:;;;;;;;;;;;;!%$&&&&&&@@$!;;;;;;;!!!;;;;;%&!;;|&%.
 19  !@|;;;;;;;;;;;;;;;;;;|%|$&&$%&&|;;;;;;;;;;;;!;;;;;!&@@&'
 20   .:%#&!;;;;;;;;;;;;;;!%|$$%%&@%;;;;;;;;;;;;;;;;;;;!&@:
 21   .%$;;;;;;;;;;;;;;;;;;|$$$$@&|;;;;;;;;;;;;;;;;;;;;%@%.
 22     !&!;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|@#;
 23      `%$!;;;;;;;;;;;$@|;;;;;;;;;;;;;;;;;;;;;;;;!%$@#@|.
 24        .|@%!;;;;;;;;;!$&%||;;;;;;;;;;;;;;;;;!%$$$$$@#|.
 25            ;&$!;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;%#####|.
 26            |##$|!;;;;;;::'':;;;;;;;;;;;;;!%$$$@#@;
 27           ;@&|;;;;;;;::'''''':;;;;;;;|$&@###@|`
 28         .%##@|;;;;:::''''''''''::;!%&##$'
 29       `$##@$$@@&|!!;;;:'''''::::;;;;;|&#%.
 30     ;&@##&$%!;;;;;;::''''''''::;!|%$@#@&@@:
 31      .%@&$$|;;;;;;;;;;:'''':''''::;;;%@#@@#%.
 32     :@##@###@$$$$$|;;:'''':;;!!;;;;;;!$#@@#$;`
 33      `%@$$|;;;;;;;;:'''''''::;;;;|%$$|!!&###&'
 34      |##&%!;;;;;::''''''''''''::;;;;;;;!$@&:`!'
 35     :;!@$|;;;;;;;::''''''''''':;;;;;;;;!%&@$:                 !@#$'
 36       |##@@&%;;;;;::''''''''':;;;;;;;!%&@#@$%:              '%%!%&;
 37       |&%!;;;;;;;%$!:''''''':|%!;;;;;;;;|&@%||`            '%$|!%&;
 38       |@%!;;!!;;;||;:'''''':;%$!;;;;!%%%&#&%$&:           .|%;:!&%`
 39       !@&%;;;;;;;||;;;:''::;;%$!;;;;;;;|&@%;!$;          `%&%!!$&:
 40       '$$|;!!!!;;||;;;;;;;;;;%%;;;;;;;|@@|!$##;         !$!;:!$&:
 41        |#&|;;;;;;!||;;;;;;;;!%|;;;;!$##$;;;;|%'      `%$|%%;|&$'
 42         |&%!;;;;;;|%;;;;;;;;$$;;;;;;|&&|!|%&&;  .:%&$!;;;:!$@!
 43         `%#&%!!;;;;||;;;;;!$&|;;;!%%%@&!;;;!!;;;|%!;;%@$!%@!
 44         !&!;;;;;;;;;||;;%&!;;;;;;;;;%@&!;;!&$;;;|&%;;;%@%`
 45        '%|;;;;;;;;!!|$|%&%;;;;;;;;;;|&#&|!!||!!|%$@@|'
 46        .!%%&%'`|$;       :|$#%|@#&;%#%.
 47 */
 48 #include <map>
 49 #include <set>
 50 #include <list>
 51 #include <ctime>
 52 #include <cmath>
 53 #include <stack>
 54 #include <queue>
 55 #include <string>
 56 #include <vector>
 57 #include <cstdio>
 58 #include <bitset>
 59 #include <cstdlib>
 60 #include <cstring>
 61 #include <iostream>
 62 #include <algorithm>
 63 #define  lowbit(x)  x & (-x)
 64 #define  mes(a, b)  memset(a, b, sizeof a)
 65 #define  fi         first
 66 #define  se         second
 67 #define  pii        pair<int, int>
 68 #define  INOPEN     freopen("in.txt", "r", stdin)
 69 #define  OUTOPEN    freopen("out.txt", "w", stdout)
 70 
 71 typedef unsigned long long int ull;
 72 typedef long long int ll;
 73 const int    maxn = 1e3 + 10;
 74 const int    maxm = 1e5 + 10;
 75 const int    mod  = 1e9 + 7;
 76 const ll     INF  = 1e18 + 100;
 77 const int    inf  = 0x3f3f3f3f;
 78 const double pi   = acos(-1.0);
 79 const double eps  = 1e-8;
 80 using namespace std;
 81 
 82 int n, m;
 83 int cas, tol, T;
 84 
 85 double p[maxn];
 86 double a[maxn];
 87 double b[maxn];
 88 
 89 void init(int n) {
 90     mes(a, 0);
 91     mes(b, 0);
 92     mes(p, 0);
 93 }
 94 
 95 void dfs(int i) {
 96     if(i > n)
 97         return ;
 98     if(a[i] != 0.0)
 99         return ;
100     for(int k=3; k<=tol; k++) {
101         dfs(i+k);
102         a[i] += a[i+k] * p[k];
103         b[i] += b[i+k] * p[k];
104     }
105     a[i] += p[0];
106     b[i] += 1;
107 }
108 
109 int main() {
110     scanf("%d", &T);
111     while(T--) {
112         int k1, k2, k3;
113         int x, y, z;
114         scanf("%d%d%d%d%d%d%d", &n, &k1, &k2, &k3, &x, &y, &z);
115         init(k1+k2+k3);
116         for(int i=1; i<=k1; i++) {
117             for(int j=1; j<=k2; j++) {
118                 for(int k=1; k<=k3; k++) {
119                     if(i == x && j == y && k == z)    continue;
120                     p[i+j+k] += 1.0;
121                 }
122             }
123         }
124         p[0] = 1.0;
125         for(int i=0; i<=k1+k2+k3; i++) {
126             p[i] = p[i] / (k1*k2*k3);
127         }
128         tol = k1+k2+k3;
129         dfs(0);
130         double ans = 1.0 * b[0] / (1 - a[0]);
131         printf("%.15f\n", ans);
132     }
133     return 0;
134 }
View Code

猜你喜欢

转载自www.cnblogs.com/H-Riven/p/10080250.html