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).

<b< dd="">

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。每次扔出的值之和加到ans上,问多少次才能ans>n;当然,当遇到k1=a,k2=b,k3=c时,ans=0;重新开始累加。

思路:

和之前Maze一个题型。写出的公式是有后续性的。我们需要弄一个递推公式,消去后续性。

本题通过代换系数,化简后求系数。

一般形成环的用高斯消元法求解。但是此题都是和dp[]相关。所有可以分离出系数。
设dp[i]表示达到i分时到达目标状态的期望,pk为投掷k分的概率,p0为回到0的概率
则dp[i]=∑(pk*dp[i+k])+dp[]*p0+;
都和dp[]有关系,而且dp[]就是我们所求,为常数
设dp[i]=A[i]*dp[]+B[i];
代入上述方程右边得到:
dp[i]=∑(pk*A[i+k]*dp[]+pk*B[i+k])+dp[]*p0+
=(∑(pk*A[i+k])+p0)dp[]+∑(pk*B[i+k])+;
明显A[i]=(∑(pk*A[i+k])+p0)
B[i]=∑(pk*B[i+k])+
先递推求得A[]和B[].
那么 dp[]=B[]/(-A[]);

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=;
double A[maxn],B[maxn],P[maxn];
int main()
{
int T,n,k1,k2,k3,a,b,c,i,j,k;
scanf("%d",&T);
while(T--){
memset(A,,sizeof(A));memset(B,,sizeof(B));memset(P,,sizeof(P));
scanf("%d%d%d%d%d%d%d",&n,&k1,&k2,&k3,&a,&b,&c);
P[]=1.0/k1/k2/k3;
for(i=;i<=k1;i++)
for(j=;j<=k2;j++)
for(k=;k<=k3;k++)
if(!(i==a&&j==b&&k==c))
P[i+j+k]+=P[];
for(i=n;i>=;i--){
A[i]=P[];B[i]=;
for(j=;j<=k1+k2+k3;j++) A[i]+=P[j]*A[i+j];
for(j=;j<=k1+k2+k3;j++) B[i]+=P[j]*B[i+j];
}
printf("%.15lf\n",B[]/(-A[]));
}
return ;
}

ZOJ3329One Person Game(循环型 数学期望)的更多相关文章

  1. 【整理】简单的数学期望和概率DP

    数学期望 P=Σ每一种状态*对应的概率. 因为不可能枚举完所有的状态,有时也不可能枚举完,比如抛硬币,有可能一直是正面,etc.在没有接触数学期望时看到数学期望的题可能会觉得很阔怕(因为我高中就是这么 ...

  2. [BZOJ 3143][HNOI2013]游走(数学期望)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3143 分析: 易得如果知道了每条边经过的数学期望,那就可以贪心着按每条边的期望的大小赋 ...

  3. Codeforces Round #259 (Div. 2) C - Little Pony and Expected Maximum (数学期望)

    题目链接 题意 : 一个m面的骰子,掷n次,问得到最大值的期望. 思路 : 数学期望,离散时的公式是E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn) p(xi)的是 ...

  4. 数学期望和概率DP题目泛做(为了对应AD的课件)

    题1: Uva 1636 Headshot 题目大意: 给出一个000111序列,注意实际上是环状的.问是0出现的概率大,还是当前是0,下一个还是0的概率大. 问题比较简单,注意比较大小: A/C & ...

  5. [2013山东ACM]省赛 The number of steps (可能DP,数学期望)

    The number of steps nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...

  6. 【BZOJ2134】单位错选(数学期望,动态规划)

    [BZOJ2134]单位错选(数学期望,动态规划) 题面 BZOJ 题解 单独考虑相邻的两道题目的概率就好了 没了呀.. #include<iostream> #include<cs ...

  7. 【BZOJ1415】【NOI2005】聪聪和可可(动态规划,数学期望)

    [BZOJ1415][NOI2005]聪聪和可可(动态规划,数学期望) 题面 BZOJ 题解 先预处理出当可可在某个点,聪聪在某个点时 聪聪会往哪里走 然后记忆化搜索一下就好了 #include< ...

  8. 【Luogu1291】百事世界杯之旅(动态规划,数学期望)

    [Luogu1291]百事世界杯之旅(动态规划,数学期望) 题面 洛谷 题解 设\(f[i]\)表示已经集齐了\(i\)个名字的期望 现在有两种方法: 先说我自己的: \[f[i]=f[i-1]+1+ ...

  9. 【BZOJ4872】分手是祝愿(动态规划,数学期望)

    [BZOJ4872]分手是祝愿(动态规划,数学期望) 题面 BZOJ 题解 对于一个状态,如何求解当前的最短步数? 从大到小枚举,每次把最大的没有关掉的灯关掉 暴力枚举因数关就好 假设我们知道了当前至 ...

随机推荐

  1. thinkphp3.2.3定时任务 不能获取本模块config, 不能获取本模块的其他配置

    一开始创建就有一个home模块再创建一个Data模块 定时任务在/Application/Common/Conf/crons.php中,这里不讲怎么创建定时任务. Data模块的配置文件路径如下/Ap ...

  2. 【性能测试】服务器性能监控、数据采集工具nmon安装使用详解

    nmon nmon是一种在AIX与各种Linux操作系统上广泛使用的监控与分析工具,它能在系统运行过程中实时地捕捉系统资源的使用情况,并且能输出结果到文件中,然后通过nmon_analyzer工具产生 ...

  3. C# 终于写完了简单的ORM

    实现单表 增 .删. 改 .查 使用接口定义表实体数据.实体对象动态创建,使用 Email 进行数据读取. 存储过程调用示例

  4. HDU 4828 逆元+catalan数

    Grids Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  5. OBS插件学习入门:一个非常简单的、调节音量的filter

    一个非常简单的.调节音量的filter,非线性调节: #include <obs-module.h> #include <math.h> struct volume_data ...

  6. 【Error】:10061由于目标计算机积极拒绝,无法连接

    之前Windows上连接mongodb的时候首先用mongod.exe启动程序之后,用mongo.exe来连接数据库.但是在连接的时候,出现如下错误: error:10061 由于目标计算机积极拒绝, ...

  7. halocn/C++ (第一篇)

        在使用C++编写halcon之前,确定自己有较好的C++基础,并熟悉一套开发平台如VC   Programmers_guide.pdf chapter7中有关于creating Aplicat ...

  8. hystrix -hystrix常用配置介绍

    配置官网介绍地址:https://github.com/Netflix/Hystrix/wiki/Configuration hystrix.command.default.execution.iso ...

  9. windows配置redis(转)

    此文章全部是转的,我之前是woidows启动redis无法加载配置找到的:原文链接:http://www.cnblogs.com/smileyearn/articles/4749746.html 在w ...

  10. CSS: Grid Layout Module

    Grid Layout The CSS Grid Layout Module offers a grid-based layout system, with rows and columns, mak ...