One Person Game(概率+数学)
There is a very simple and interesting one-person game. You have 3 dice, namelyDie1, Die2 and Die3. Die1 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 K1, K2, K3 is exactly 1 / K1, 1 / K2 and 1 / K3. You have a counter, and the game is played as follow:
- Set the counter to 0 at first.
- 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.
- 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 n, K1, K2, K3, a, b, c (0 <= n <= 500, 1 < K1, K2, K3 <= 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(概率+数学)的更多相关文章
- best coder #35-01<组合数学 || 概率数学>
问题描述 一个盒子里有n个黑球和m个白球.现在DZY每次随机从盒子里取走一个球,取了n+m次后,刚好取完.DZY用这种奇怪的方法生成了一个随机的01串S[1⋯(n+m)].如果DZY第i次取出的球是黑 ...
- 2019暑期集训第二讲 - 组合数学&概率&数学期望
A - 容斥原理(CodeForces - 451E) 二进制状态压缩暴力枚举哪几个花选的个数超过了总个数,卢卡斯定理求组合数,容斥原理求答案 可以先把每个花的数量当成无限个,这样就是一个多重集的组合 ...
- Gym - 101987G Secret Code (概率+数学积分)
题意:有A,B,C三个人要见面,每个人在[0,S]随机选择一个时间点作为见面时间,先到的那个人要等下一个人来了之后和他确认信息,然后马上就走. 例如,假如A先到,B其次,C最后到,那么A要等B到了之后 ...
- 概率专题_概率/ 数学_基础题_ABEI
上周三讲了概率和概率dp.如果没有涉及其他综合算法,概率这种题主要是思维,先把这部分的东西写完 给个题目链接:https://vjudge.net/contest/365300#problem Hea ...
- hdu5984概率数学
转载 https://www.oyohyee.com/post/HDU/5984.html
- 算法讲堂二:组合数学 & 概率期望DP
组合数学 1. 排列组合 1. 加法原理 完成一列事的方法有 n 类,其中第 i 类方法包括\(a_i\)种不同的方法,且这些方法互不重合,则完成这件事共有 \(a_1 + a_2 + \cdots ...
- ACM知识点
基础算法 高精 模拟 分治 贪心 排序 DFS 迭代加深搜索 BFS 双向BFS 动态规划 DAG上DP 树上DP 线性DP 图算法 最短路 FLYD DJATL BF 最大流 Dinic ISAP ...
- 3D打印:三维智能数字化创造(全彩)
3D打印:三维智能数字化创造(全彩)(全球第一本系统阐述3D打印与3D智能数字化的专业著作) 吴怀宇 编 ISBN 978-7-121-22063-0 2014年1月出版 定价:99.00元 42 ...
- breeze源码阅读心得
在阅读Spark ML源码的过程中,发现很多机器学习中的优化问题,都是直接调用breeze库解决的,因此拿来breeze源码想一探究竟.整体来看,breeze是一个用scala实现的基 ...
随机推荐
- 工具分享:GitHub的克隆工具Cl0neMast3r,轻松搞定各种测试
GitHub,相信大家并不陌生,咱搞技术的应该都会用到它,GitHub主要是进行代码工具的存储.下载等工作.今天介绍一款让我们操作GitHub相关工作变的更简单的工具, GitHub的克隆工具. Cl ...
- 2017.7.21 linux下进程管理工具supervisord的安装与使用
参考来自:http://blog.haohtml.com/archives/15145 0 操作环境 1 supervisord的介绍 Supervisord是用Python实现的一款非常实用的进程管 ...
- Node.js 文件系统流pipe到Http响应流中
// 内置http模块,提供了http服务器和客户端功能(path模块也是内置模块,而mime是附加模块) var http=require("http"); var fs=req ...
- UVA 111 (复习dp, 14.07.09)
History Grading Background Many problems in Computer Science involve maximizing some measure accor ...
- @property和@x.setter和@x.deleter表示可读可写可删除
@property可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/deleter也是需要的.1>只有@property表示只读.2>同时有@ ...
- Ant Design Mobile 使用 rc-form
引入: import { createForm } from 'rc-form'; 步骤一:绑定 form // 将form表单的api绑定到props,便于调用 const EditHeaderWr ...
- 多trac的安装和配置
其他相关网页: trac+svn: http://wenku.baidu.com/view/84389a81ec3a87c24028c43f.html apache(GCI):http://hi.ba ...
- DirectorySearcher LDAP
1.从LDAP服务器上面获取用户名 sAMAccountName是个人的CN结点中的一个属性,例如个人的CN的sAMAccountName的值为:Amy.我命名它为shortname,即短名 publ ...
- Dephi泛型generic的应用
Dephi泛型generic的应用 泛型在C++, C#中已有广泛应用,Delphi自2009版本也引入泛型,典型的应用如TList,TDictionary.如果你熟悉C#,其用法十分类似. 比如 ...
- Memcache针对不同场景数据应用缓存策略
Memcache主要的作用是为减轻大访问量对数据库的冲击,所以一般的逻辑是首先从memcache中读取数据,如果没有就从数据库中读取数据写入到memcache中,等下一次读取的时候就可以从memcac ...