There is a very simple and interesting one-person game. You have 3 dice, namelyDie1Die2 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 题意: T 组数据, 每组数据一行,n, K1, K2, K3, a, b, c 代表 3 个骰子有 K1,K2,K3 个面
用这三个骰子玩游戏,首先,计数器清零,掷一次,如果三个骰子中,第一个为 a, 第二个为b,第三个为c ,计数器清零,否则,计数器累加三个骰子之和。
如此重复执行第二步 ,直到计数器和大于 n 问计数器大于 n 的游戏次数期望 要推导出个递推式子,然后发现都和 dp[0] 相关,分离系数,我也是看了这篇博客才懂的,写得很好:
http://www.cnblogs.com/kuangbin/archive/2012/10/03/2710648.html
 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; #define MAXN 600 int n, k1, k2, k3, a, b, c;
double A[MAXN],B[MAXN];
double p0;
double p[]; int main()
{
int T;
cin>>T;
while (T--)
{
scanf("%d%d%d%d%d%d%d",&n,&k1,&k2,&k3,&a,&b,&c);
memset(A,,sizeof(A));
memset(B,,sizeof(B));
memset(p,,sizeof(p)); p0=1.0/(k1*k2*k3); //单位概率,变为 0 的概率
for (int j1=;j1<=k1;j1++)
for (int j2=;j2<=k2;j2++)
for (int j3=;j3<=k3;j3++)
if(j1!=a||j2!=b||j3!=c)
p[j1+j2+j3]+=p0; //掷出某一个和的概率 for (int i=n;i>=;i--)//因为要循环到大于 n
{
for (int j=;j<=k1+k2+k3;j++)
{
A[i]+=p[j]*A[i+j];
B[i]+=p[j]*B[i+j];
}
A[i]+=p0;
B[i]+=1.0;
}
double ans = B[]/(1.0-A[]);
printf("%.15lf\n",ans);
}
return ;
}

One Person Game(概率+数学)的更多相关文章

  1. best coder #35-01<组合数学 || 概率数学>

    问题描述 一个盒子里有n个黑球和m个白球.现在DZY每次随机从盒子里取走一个球,取了n+m次后,刚好取完.DZY用这种奇怪的方法生成了一个随机的01串S[1⋯(n+m)].如果DZY第i次取出的球是黑 ...

  2. 2019暑期集训第二讲 - 组合数学&概率&数学期望

    A - 容斥原理(CodeForces - 451E) 二进制状态压缩暴力枚举哪几个花选的个数超过了总个数,卢卡斯定理求组合数,容斥原理求答案 可以先把每个花的数量当成无限个,这样就是一个多重集的组合 ...

  3. Gym - 101987G Secret Code (概率+数学积分)

    题意:有A,B,C三个人要见面,每个人在[0,S]随机选择一个时间点作为见面时间,先到的那个人要等下一个人来了之后和他确认信息,然后马上就走. 例如,假如A先到,B其次,C最后到,那么A要等B到了之后 ...

  4. 概率专题_概率/ 数学_基础题_ABEI

    上周三讲了概率和概率dp.如果没有涉及其他综合算法,概率这种题主要是思维,先把这部分的东西写完 给个题目链接:https://vjudge.net/contest/365300#problem Hea ...

  5. hdu5984概率数学

    转载 https://www.oyohyee.com/post/HDU/5984.html

  6. 算法讲堂二:组合数学 & 概率期望DP

    组合数学 1. 排列组合 1. 加法原理 完成一列事的方法有 n 类,其中第 i 类方法包括\(a_i\)种不同的方法,且这些方法互不重合,则完成这件事共有 \(a_1 + a_2 + \cdots ...

  7. ACM知识点

    基础算法 高精 模拟 分治 贪心 排序 DFS 迭代加深搜索 BFS 双向BFS 动态规划 DAG上DP 树上DP 线性DP 图算法 最短路 FLYD DJATL BF 最大流 Dinic ISAP ...

  8. 3D打印:三维智能数字化创造(全彩)

    3D打印:三维智能数字化创造(全彩)(全球第一本系统阐述3D打印与3D智能数字化的专业著作) 吴怀宇 编   ISBN 978-7-121-22063-0 2014年1月出版 定价:99.00元 42 ...

  9. breeze源码阅读心得

            在阅读Spark ML源码的过程中,发现很多机器学习中的优化问题,都是直接调用breeze库解决的,因此拿来breeze源码想一探究竟.整体来看,breeze是一个用scala实现的基 ...

随机推荐

  1. C++之纯虚函数

    1. 纯虚函数形式 class Parent { public: ; }; 代码中的func1就是纯虚函数,没有函数体,没有函数的具体实现,有virtual,在函数名后面带有“ = 0”形式: 2.对 ...

  2. DatagramPacket,DatagramSocket

    package test; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSo ...

  3. CSU - 1542 Flipping Parentheses (线段树)

    CSU - 1542 Flipping Parentheses Time Limit: 5000MS   Memory Limit: 262144KB   64bit IO Format: %lld ...

  4. 2017.3.31 spring mvc教程(二)核心流程及配置详解

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...

  5. python中ascii码和字符的转换

    将ASCII字符转换为对应的数值即‘a’-->65,使用ord函数,ord('a') 反正,使用chr函数,将数值转换为对应的ASCII字符,chr(65)

  6. 倍福TwinCAT(贝福Beckhoff)基础教程4.1 TwinCAT如何读写TXT文件

    TwinCAT提供了FB_FileRead等一系列读写文件的方法,本小程序演示的是多个贝福自带的FBD功能块连起来用的方法,跟前面讲的一样,建议在初始化的时候把所有FBD都复位,准备使用   真正的读 ...

  7. 在HTML页面中实现一个简单的Tab

    参考:http://blog.sina.com.cn/s/blog_6cccb1630100m23i.html HTML页面代码如下: <!DOCTYPE html PUBLIC "- ...

  8. Hadoop eclipse插件使用过程中出现的问题

    http://download.csdn.net/detail/java2000_wl/4326323 转自http://www.ithao123.cn/content-945210.html 由于h ...

  9. maven生成jar包

    改了部分cas源码,想重新生成jar包,只好试着脱离eclipse,学了一下maven打jar包的命令,记录如下: 1.首先下载maven (请注意自己的jdk版本,如果使用maven2建议使用jdk ...

  10. http://www.bootcss.com/p/font-awesome/design.html

    http://www.bootcss.com/p/font-awesome/design.html <li class="active"> <div class= ...