zoj3329 One Person Game
One Person Game
Time Limit: 1 Second Memory Limit: 32768 KB Special Judge
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).
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
我们可以推出公式dp[i]=sum{pk*dp[i+k]}+p0*dp[0]+1,dp[i]表示当前分数是i是时的到游戏结束的期望,那么dp[0],就是要求的,其实,我们可以发现一个归律,求期望都是从后住前推的,比如,这里,是由i+k推到i的,求概率的时候是刚好相反的,但我们发现求出的这个式子,是个环形的,所以要变形,因为,每一个都是和dp[0]相关的,我们可以设,dp[i]=a[i]*dp[0]+b[i],那么dp[0]不就是,b[i]/(1-a[i])了么,我们,把这个式子代入上式就可以得到,dp[i]=sum{pk*a[i+k]}+p0,b[i]=sum{pk*b[i+k]}+1,这样,就可以马上求结果来了!
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAXN 550
double pa[MAXN],pb[MAXN],dp[MAXN],p[MAXN];
int main()
{
int tcase,i,j,k,n,a,b,c,k1,k2,k3;
double p0;
scanf("%d",&tcase);
while(tcase--)
{
scanf("%d%d%d%d%d%d%d",&n,&k1,&k2,&k3,&a,&b,&c);
p0=1.0/k1/k2/k3;
memset(pa,0,sizeof(pa));
memset(pb,0,sizeof(pb));
memset(dp,0,sizeof(dp));
memset(p,0,sizeof(p));
for(i=1;i<=k1;i++)
for(j=1;j<=k2;j++)
for(k=1;k<=k3;k++)
if(i!=a||j!=b||k!=c)//不同时相等
p[i+j+k]+=p0;
int temp=k1+k2+k3;
for(i=n;i>=0;i--)
{
pa[i]=p0;
pb[i]=1;
for(k=1;k<=temp;k++)
{
pa[i]+=pa[i+k]*p[k];
pb[i]+=pb[i+k]*p[k];
}
}
printf("%.15f\n",pb[0]/(1.0-pa[0]));
}
return 0;
}
zoj3329 One Person Game的更多相关文章
- 概率dp的迭代方式小结——zoj3329,hdu4089,hdu4035
在推导期望方程时我们常常会遇到dp[i]和其他项有关联,那么这时候我们就难以按某个顺序进行递推 即难以通过已经确定的项来求出新的项 即未知数的相互关系是循环的 但是我们又可以确定和dp[i]相关联的项 ...
- ZOJ3329之经典概率DP
One Person Game Time Limit: 1 Second Memory Limit: 32768 KB Special Judge There is a very ...
- zoj3329(概率dp)
题目连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754 题意:有三个骰子,分别有k1,k2,k3个面. 每次掷骰子,如 ...
- ZOJ-3329 One Person Game (有环期望问题)
题目大意:有3个骰子,各有k1,k2,k3个面,面值为1~ki.还有一个计数器,初始值为0,统计所有的面值和.每次同时置这三个骰子,如果第一个骰子的朝上的值为a.第二个值为b.第三个值为c,那么将计数 ...
- 【ZOJ3329】One Person Game
题意 你有三枚色子,第i个色子有ki面,你有一个计数器. 1.开始的时候将计数器调至0 2.扔三个色子,如果色子1是a,色子2是b,色子3是c,则将计数器归零.否则计数器加上三个色子的和. 3.如果计 ...
- ZOJ3329 概率DP
One Person Game Time Limit: 1 Second Memory Limit: 32768 KB Special Judge There is a very ...
- ZOJ3329(数学推导+期望递推)
要点: 1.期望的套路,要求n以上的期望,则设dp[i]为i分距离终点的期望步数,则终点dp值为0,答案是dp[0]. 2.此题主要在于数学推导,一方面是要写出dp[i] = 什么,虽然一大串但是思维 ...
- 概率dp——逆推期望+循环迭代zoj3329
首先要推出dp[i]的期望方程,会发现每一项都和dp[0]相关, 那我们将dp[i]设为和dp[0]有关的式子dp[i]=a[i]*dp[0]+b[i],然后再回代到原来的期望方程里 然后进行整理,可 ...
- 【期望DP】[zoj3329]One Person Game
题描: 有三个均匀的骰子,分别有k1,k2,k3个面,初始分数是0, 当掷三个骰子的点数分别为a,b,c的时候,分数清零,否则分数加上三个骰子的点数和, 当分数>n的时候结束.求需要掷骰子的次数 ...
随机推荐
- C++相关资源
http://www.cnblogs.com/xi52qian/p/4186983.html语言ISO/IEC JTC1/SC22/WG21 - The C++ Standards Committee ...
- HTML5:一个拖拽网页元素的例子
关键字:HTML5, Drag&Drop, JavaScript, CSS 运行环境:Chrome <!DOCTYPE html> <html> <head> ...
- c++11 auto_ptr介绍
在代码里面看到了auto_ptr这个东西,正好以前一哥们曾经问过我这个问题..所以特意去搜了搜帖子,学习学习 http://www.cnblogs.com/gaoxianzhi/p/4451803.h ...
- hdu10007
题意,很简单,给n个点的坐标,求距离最近的一对点之间距离的一半. 第一行是一个数n表示有n个点,接下来n行是n个点的x坐标和y坐标.实数. 这个题目其实就是求最近点对的距离 #include< ...
- css小心德
内部块最好用百分比,微调时方便
- 详解C/C++函数指针声明
要理解一个C程序,仅仅理解组成该程序的符号是不够的.程序员还必须理解这些符号是如何组合成声明.表达式.语句和程序的. 我们先来看看下面的一个语句: 1 ( *( void(*)())0)(); 这是当 ...
- C# 两个获得程序运行路径的函数
EXE文件的存储路径,不太受调用时环境变量的影响: Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Lo ...
- leetcode 第二题Add Two Numbers java
链接:http://leetcode.com/onlinejudge Add Two Numbers You are given two linked lists representing two n ...
- 【HDU 5370】 Tree Maker(卡特兰数+dp)
Tree Maker Problem Description Tree Lover loves trees crazily. One day he invents an interesting gam ...
- C#利用NPOI生成具有精确列宽行高的Excel文件
前言 NPOI是操作Excel的神器,导出导入快如闪电, 但是SetColumnWidth函数个人感觉不会用,怎么弄都无法控制好,因为他是以字符数量去设置宽度,实际上Excel列宽还有个像素的概念,更 ...