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实现的基 ...
随机推荐
- leetcode题解:Search for a Range (已排序数组范围查找)
题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...
- How to initialize th rasp berry PI
WHAT YOU WILL NEED REQUIRED SD Card We recommend an 8GB class 4 SD card – ideally preinstalled with ...
- 【Hadoop】Hadoop mr wordcount基础
1.基本概念 2.Mapper package com.ares.hadoop.mr.wordcount; import java.io.IOException; import java.util.S ...
- zabbix_sender高效模式
1.zabbix_sender介绍 zabbix获取key值有超时时间,如果自定义的key脚本一般需要执行很长时间,这根本没法去做监控,获取数据有超时时间,如果一些数据需要执行比较长的时间才能获取的话 ...
- 系统封装 EasyBoot如何将WIN7安装版提取到光盘
1 将WIN7光盘中的文件提取到Easyboot根目录,注意不要autorun.inf和setup.exe这两个文件.我们这里的Easyboot已经有了一些其他东西(XP的安装版文件,PE的文件等等, ...
- Quaternion 四元数
Quaternions are used to represent rotations. 四元数用于表示旋转. They are compact, don't suffer from gimbal l ...
- sone1动态树
这尼吗桑心病狂的动态树:http://www.lydsy.com/JudgeOnline/problem.php?id=3153 终于让哥以一种碉堡的姿势过了: 牛B轰轰的最后两个都是我的...无法超 ...
- 【引用】python 静态函数 类函数 实例函数
1.关于定义类的一些奇特之处 今天在Python中定义一个类,很奇怪,不需要事先声明它的成员变量吗?暂时不知,先记录下来: class Account(object): "一个简单的 ...
- 深入浅出java静态代理和动态代理
首先介绍一下.什么是代理: 代理模式,是经常使用的设计模式. 特征是.代理类与托付类有同样的接口,代理类主要负责为托付类预处理消息.过滤消息.把消息转发给托付类.以及事后处理消息. 代理类和托付类,存 ...
- 远程链接mysql数据库
mysql -P3306 -uroot -proot 显示最大连接数 show variables like '%max_connections%'; 设置最大链接数 ;//默认100--只对当前进程 ...