ZOJ3329One Person Game(循环型 数学期望)
There is a very simple and interesting one-person game. You have 3 dice, namely Die1, 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).
<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(循环型 数学期望)的更多相关文章
- 【整理】简单的数学期望和概率DP
数学期望 P=Σ每一种状态*对应的概率. 因为不可能枚举完所有的状态,有时也不可能枚举完,比如抛硬币,有可能一直是正面,etc.在没有接触数学期望时看到数学期望的题可能会觉得很阔怕(因为我高中就是这么 ...
- [BZOJ 3143][HNOI2013]游走(数学期望)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3143 分析: 易得如果知道了每条边经过的数学期望,那就可以贪心着按每条边的期望的大小赋 ...
- 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)的是 ...
- 数学期望和概率DP题目泛做(为了对应AD的课件)
题1: Uva 1636 Headshot 题目大意: 给出一个000111序列,注意实际上是环状的.问是0出现的概率大,还是当前是0,下一个还是0的概率大. 问题比较简单,注意比较大小: A/C & ...
- [2013山东ACM]省赛 The number of steps (可能DP,数学期望)
The number of steps nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; ...
- 【BZOJ2134】单位错选(数学期望,动态规划)
[BZOJ2134]单位错选(数学期望,动态规划) 题面 BZOJ 题解 单独考虑相邻的两道题目的概率就好了 没了呀.. #include<iostream> #include<cs ...
- 【BZOJ1415】【NOI2005】聪聪和可可(动态规划,数学期望)
[BZOJ1415][NOI2005]聪聪和可可(动态规划,数学期望) 题面 BZOJ 题解 先预处理出当可可在某个点,聪聪在某个点时 聪聪会往哪里走 然后记忆化搜索一下就好了 #include< ...
- 【Luogu1291】百事世界杯之旅(动态规划,数学期望)
[Luogu1291]百事世界杯之旅(动态规划,数学期望) 题面 洛谷 题解 设\(f[i]\)表示已经集齐了\(i\)个名字的期望 现在有两种方法: 先说我自己的: \[f[i]=f[i-1]+1+ ...
- 【BZOJ4872】分手是祝愿(动态规划,数学期望)
[BZOJ4872]分手是祝愿(动态规划,数学期望) 题面 BZOJ 题解 对于一个状态,如何求解当前的最短步数? 从大到小枚举,每次把最大的没有关掉的灯关掉 暴力枚举因数关就好 假设我们知道了当前至 ...
随机推荐
- Css(样式)
CSS三种样式 1.行内样式 ①将css样式与html,完全糅杂在一起,不符合w3c关于“内容与表现分离”的基本规范,不利于后期维护. ②优先级最高. 2.内部样式表 ...
- 梅森素数应用 nefu 120
梅森素数 定义: if m是一个正整数 and 2^m-1是一个素数 then m是素数 if m是一个正整数 and m是一个素数 then M(m)=2^m-1被称为第m个梅森数 if p是一个素 ...
- 自学Jav测试代码三 Math类 & Date & GregorianCalendar类
2017-08-23 20:30:08 writer: pprp package test; import java.util.Date; import java.util.*; public cla ...
- 使用ssm整合是项目启动tomcat报错java.lang.IndexOutOfBoundsException
解决办法:删除.m2文件夹下的全部仓库,然后重启myeclipse,对项目进行maven project.问题解决. 在没有这样做时,除了tomcat启动会失败,项目还有会报如下错误: ①cvc-co ...
- ajax实现用户注册
需求分析 页面中给出注册表单: 在username input标签中绑定onblur事件处理函数. 当input标签失去焦点后获取 username表单字段的值,向服务端发送AJAX请求: djang ...
- 设计模式---工厂方法C++实现
工厂方法C++实现 1描述 定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法使一个类的实例化延迟到其子类 2类图描述 3代码实现: class Product { protected: ...
- python 数组中如何根据值,获取索引,如何根据索引删除值 , 以及如何根据值删除值
假设有一数组 s = [1,2,3,4,5,6,7,8,9] (1)如何根据值获取索引 ,如果值为5 , 那对应的索引为? (2)如何根据索引删除值 , 删除数组中索引5对应的值: (3)根据数组中的 ...
- 【问题解决记录】无法识别的标志“-sdlMode”,在“p2”中
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/ 这是本人第一次使用MarkDown编辑器,试试看效果-w-,下面是正文: 一.报错原因 昨 ...
- PHP---初探PHP
初探PHP 虽然说前后端分离,但作为前端还是要跟数据打交道的,所以对后台语言的了解还是很有必要的.今天要学的就是PHP. 什么是PHP? PHP(外文名:PHP: Hypertext Preproce ...
- 更改gitlab默认端口
若linux服务器的80和8080端口都已经被使用,则需修改gitlab监听的端口 修改路径文件:vim /etc/gitlab/gitlab.rb 1.修改external_url 'http:// ...