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. linux用户态与内核态

    系统调用:如何中用户态切换到内核态 在linux中,系统调用是通过0x86体系结构中的软件中断实现的.这个软件中断与通常说的硬件中断不同之处在于,它是通过软件指令触发,而不是外部设备,这是程序员可以触 ...

  2. maven项目中使用redis集群报错: java.lang.NumberFormatException: For input string: "7006@17006"

    Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [redis.client ...

  3. 02_zookeeper集群安装

    zookeeper集群安装 (1)   下载zookeeper安装包,并上传到要组成zookeeper集群的多个机器上 我放置的目录:/usr/local/src/zookeeper-3.4.5.ta ...

  4. 记一次mogodb占用cpu高问题

    公司服务器上安装了contly,是一个开源的node.js项目,用于统计手机app使用情况,后端数据储存使用的mongodb,使用的时候经常发现mongodb占用cpu非常高,打到了210%的爆表值 ...

  5. Java循环跳转语句之 continue

    continue 的作用是跳过循环体中剩余的语句执行下一次循环. 例如,打印 1--10 之间所有的偶数,使用 continue 语句实现代码为: 运行结果: continue与break的区别就是, ...

  6. RestTemplate请求https忽略证书认证

    RestTemplate是Spring提供的用于访问Rest服务的客户端,提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率.RestTemplate 默认使用J2SE提供的方式( ...

  7. MySQL Order By Rand()效率分析

    最近研究了一下MYSQL的随机抽取实现方法.举个例子,要从tablename表中随机提取一条记录,大家一般的写法就是:SELECT * FROM tablename ORDER BY RAND() L ...

  8. angular 当使用ng-repeat 时出现 $$hashKey的键值对

    小问题 把: ng-repeat="item in items " 改成 : ng-repeat="item in items track by $index"

  9. SQLServer 2005 和自增长主键identity说再见——NEWSEQUENTIALID()

    ``code 在SQLServer2005环境下,表的主键应该怎样设计. 目前主要用到的主键方案共三种 自动增长主键 手动增长主键 UNIQUEIDENTIFIER主键 1.先说自动增长主键,它的优点 ...

  10. javascript中的__proto__ 和prototype

    不错的一张图