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 题解 对于一个状态,如何求解当前的最短步数? 从大到小枚举,每次把最大的没有关掉的灯关掉 暴力枚举因数关就好 假设我们知道了当前至 ...
随机推荐
- 【卷积神经网络】对BN层的解释
前言 Batch Normalization是由google提出的一种训练优化方法.参考论文:Batch Normalization Accelerating Deep Network Trainin ...
- mybatis的一对多
1.配置文件 db.properties db.driver=com.mysql.jdbc.Driver db.url=jdbc:mysql://localhost:3306/demo?useUnic ...
- 从U盘安装linux(前人踩坑后人乘凉)
今天踩了一个大坑,网上的教程从u盘安装linux少了一个关键步骤导致我挣扎了两个小时 废话不多说,开始需要准备一些东西 1.从官网下载一个Ubuntu 10.04的镜像 2.一个大于等于1G的支持启动 ...
- HDU 3594 Cactus(仙人掌问题)
http://acm.hdu.edu.cn/showproblem.php?pid=3594 题意: 一个有向图,判断是否强连通和每条边只在一个环中. 思路: 仙人掌问题. 用Tarjan算法判断强连 ...
- LA 5135 井下矿工(点—双连通分量模板题)
https://vjudge.net/problem/UVALive-5135 题意:在一个无向图上选择尽量少的点涂黑,使得任意删除一个点后,每个连通分量至少有一个黑点. 思路: 首先dfs遍历求出割 ...
- Linux内核、 TCP/IP、Socket参数调优
/proc/sys/net目录 所有的TCP/IP参数都位于/proc/sys/net目录下(请注意,对/proc/sys/net目录下内容的修改都是临时的,任何修改在系统重启后都会丢失),例如下面这 ...
- shell统计各省的百强县
原始数据在最后 baiqiang.txt文件中 shell命令: cat baiqiang.txt | grep -P "^国|^☆" | awk -F" " ...
- 无法启动此程序,因为计算机丢失MSVCP120.dll
这种错误是由于未安装** vcredist **引起的(而且版本是 2013版):https://www.microsoft.com/zh-CN/download/details.aspx?id=40 ...
- ContentControl和ContentPresenter的应用
1:wpf中,所有的内容控件都继承自“ContentControl” ,所以我们可以直接应用“ContentControl”自定义我们“需要的”内容控件. 2:ContentControl具有Cont ...
- JS触发服务器控件的单击事件
<script src="../Js/jquery-1.4.2.min.js" type="text/javascript"></script ...