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实现的基 ...
随机推荐
- Linux&Windows下批量修改文件后缀
Linux下从给定文件夹中找出小于1M的文件,并批量添加.gif后缀 先看一下文件夹下的目录的格式 ll -Sh -rw-rw-r-- 1 yangkun yangkun 17M May 10 15: ...
- [转载]JAVA调用Shell脚本
FROM:http://blog.csdn.net/jj12345jj198999/article/details/11891701 在实际项目中,JAVA有时候需要调用C写出来的东西,除了JNI以外 ...
- Nodejs 模拟telnet
代码下载:https://files.cnblogs.com/files/xiandedanteng/nodejsTelnet.rar 效果: server.js代码: var net=require ...
- PS 如何附加增效工具.
编辑-首选项-增效工具,选择目标增效工具文件夹
- react-native 启动页(react-native-splash-screen)
用于解决iOS和Android启动白屏问题及简单的启动页面展示 下载 react-native-splash-screen yarn add react-native-splash-screen re ...
- ddmrp
DDMRP 特点 在供应链加入 mts 缓冲,解耦 lead time, 缩小 bullwhip 效应,最小化库存 buffer动态调整 buffer 分3个颜色共 4个区域[zone],为 gree ...
- 代理工具Charles使用
代理工具Charles使用 分类: MAC 2014-03-27 20:41 7810人阅读 评论(2) 收藏 举报 手机开发 一.跟踪HTTPS 1.下载官方的证书ssl.zip证书,解压成*.cr ...
- 正则表达式匹配 href 和text内容
string pattern = @"<a[^>]*href=(""(?<href>[^""]*)""|' ...
- MongoDB 常见的查询索引
常见的查询索引 _id索引 _id 索引是绝大多数集合默认建立的索引.对于每一个插入的数据.MongoDB 会自己主动生成一条唯一的 _id 字段. 1 2 3 4 5 6 7 8 9 ...
- linux 下gtest 安装
cd gtest_dir //解压后的目录 mkdir mybuild # Create a directory to hold the build output. cd mybuild cmake ...