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:

  1. Set the counter to 0 at first.
  2. 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.
  3. 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的更多相关文章

  1. 概率dp的迭代方式小结——zoj3329,hdu4089,hdu4035

    在推导期望方程时我们常常会遇到dp[i]和其他项有关联,那么这时候我们就难以按某个顺序进行递推 即难以通过已经确定的项来求出新的项 即未知数的相互关系是循环的 但是我们又可以确定和dp[i]相关联的项 ...

  2. ZOJ3329之经典概率DP

    One Person Game Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge There is a very ...

  3. zoj3329(概率dp)

    题目连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754 题意:有三个骰子,分别有k1,k2,k3个面. 每次掷骰子,如 ...

  4. ZOJ-3329 One Person Game (有环期望问题)

    题目大意:有3个骰子,各有k1,k2,k3个面,面值为1~ki.还有一个计数器,初始值为0,统计所有的面值和.每次同时置这三个骰子,如果第一个骰子的朝上的值为a.第二个值为b.第三个值为c,那么将计数 ...

  5. 【ZOJ3329】One Person Game

    题意 你有三枚色子,第i个色子有ki面,你有一个计数器. 1.开始的时候将计数器调至0 2.扔三个色子,如果色子1是a,色子2是b,色子3是c,则将计数器归零.否则计数器加上三个色子的和. 3.如果计 ...

  6. ZOJ3329 概率DP

    One Person Game Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge There is a very ...

  7. ZOJ3329(数学推导+期望递推)

    要点: 1.期望的套路,要求n以上的期望,则设dp[i]为i分距离终点的期望步数,则终点dp值为0,答案是dp[0]. 2.此题主要在于数学推导,一方面是要写出dp[i] = 什么,虽然一大串但是思维 ...

  8. 概率dp——逆推期望+循环迭代zoj3329

    首先要推出dp[i]的期望方程,会发现每一项都和dp[0]相关, 那我们将dp[i]设为和dp[0]有关的式子dp[i]=a[i]*dp[0]+b[i],然后再回代到原来的期望方程里 然后进行整理,可 ...

  9. 【期望DP】[zoj3329]One Person Game

    题描: 有三个均匀的骰子,分别有k1,k2,k3个面,初始分数是0, 当掷三个骰子的点数分别为a,b,c的时候,分数清零,否则分数加上三个骰子的点数和, 当分数>n的时候结束.求需要掷骰子的次数 ...

随机推荐

  1. html doctype 作用

    文档模式主要有以下两个作用: 1.告诉浏览器使用什么样的html或xhtml规范来解析html文档 2.对浏览器的渲染模式产生影响:不同的渲染模式会影响到浏览器对于 CSS 代码甚至 JavaScri ...

  2. jQuery实现购物车多物品数量的加减+总价+删除计算

    <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...

  3. Spring Cloud Eureka Server 启停状态监控

    目前发现如下的api: 当时没有找到文档 http://localhost:8761/eureka/apps 参考文章:(此文中api带有v2我自己试验不需要v2) http://blog.csdn. ...

  4. 欢迎使用skymvc框架,简单易用的php框架

    skymvc是一款轻量.简单易用的php mvc框架,经过多个项目实践改良. 特点: 1.mvc架构 2.m.v.c之间可以互相调用 3.简单的路由控制 R("/index.php" ...

  5. sql server 与C#数据类型对应表

  6. ARM中的PC和AXD的PC

    R15 (PC)总是指向“正在取指”的指令,而不是指向“正在执行”的指令或正在“译码”的指令.一般来说,人们习惯性约定将“正在执行的指令作为参考点”,称之为当前第一条指令,因此PC 总是指向第三条指令 ...

  7. AndroidStudio Gradle版本不匹配问题

    报错信息: p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } Error:(1, 1) A problem occurr ...

  8. php实例-正则获取网站音频地址的实例(Listen to this 1)

    主要用到了:file_get_contents();preg_match_all(); 这2个函数 查看地址:http://git.oschina.net/xiaoz6/phpExample

  9. C#中通过调用Dll函数时,执行一段时间后,就会报内存可能被破坏的错的解决办法

    遇到同样的问题,已经解决的:http://blog.csdn.net/youxiazzz12/article/details/24313347

  10. jQuery预加载插件

      插件描述:jQuery Fadeloader的插件可以让你轻松实现预加载到您的网站或部分使用级联渐显效果来显示特定的内容块(例如,头> MENU>内容>页脚) jQuery Fa ...