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#代码自动修改解决方案下任意文件
命名空间 using EnvDTE;using EnvDTE80; private DTE2 _applicationObject; public void AutoAddControl(插件 v_f ...
- demo——06弹性和制作骰子
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- init.d文件夹
/etc/init.d 是 /etc/rc.d/init.d 的软链接(soft link). [root@asus ~]# ll /etc/init.d lrwxrwxrwx. 1 root roo ...
- nginx 反向代理 odoo, 速度提升
1: sudo apt-get install nginx 2: add server: 在/etc/nginx/sites-enabled 中增加一个可用的server. 文件如下图所示 3:重 ...
- Windows API 常量定义
Windows 常量定义在winuser.h中可以找到,如果了安装了visual studio 2010,winuser.h所在目录为C:\Program Files (x86)\Microsoft ...
- Sqlserver 原生 MD5 函数(转)
--创建md5函数CREATE FUNCTION [dbo].[MD5](@src varchar(255) )RETURNS varchar(255)ASBEGIN DECLARE @md5 ...
- Linux下find指令
Linux是一个“一切皆文件”的操作系统,find(查找文件)指令就尤为重要了.find命令在目录结构中搜索文件,并执行指定的操作. 1.命令格式: find pathname -options [- ...
- Java 泛型快速排序 以sdut 1196为例
oj链接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1196 Java中,Arrays.so ...
- CodeFirst数据库迁移小记
打开“程序包管理器控制台”菜单项一.Enable-Migrations -ContextTypeName Code_First_数据迁移.Models.T_DbContext成功后提示:已在项目“Co ...
- sublime部署开发环境
安装nodejs 去官网下载安装,暂时不建议安装5.0以上版本. 命令行运行以下命令查看是否安装成功: 全局安装gulp $ npm install -g gulp 全局安装requirejs 压缩资 ...