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 Die1Die2 and Die3Die1 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 K1K2K3 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 nK1K2K3abc (0 <= n <= 500, 1 < K1K2K3 <= 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

    f[i]表示已经得到i分之后距离目标的期望次数,pk表示得分为k的概率,则有f[i]=SUM{ p[k]*f[i+k] } + p0*f[0] + 1 ,因为下一次操作可能清零或者组合成其他点数,要分开讨论。这个式子无法直接递推,我们需要简化一下。

可以看出f[i]均和f[0]有关,不妨令f[i]=A[i]*f[0]+B[i] ,带入上式得到  f[i]=(sum{ p[k]*A[i+k] }  + p0)*f[0]+(SUM{ pk*B[i+k] } +1 ) ,可以看出A[i]=SUM{ pk*A[i+k] }+p0 , B[i]=SUM{ pk*B[i+k] }+1 ,A[i]和B[i]可以递推得到,所以答案就是A[0]/(1-B[0]);

    

 #include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<stack>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<time.h>
#include<algorithm>
using namespace std;
#define mp make_pair
#define pb push_back
#define debug puts("debug")
#define LL long long
#define pii pair<int,int>
#define eps 1e-12 double p[];
double A[],B[];
int main()
{
int n,m,i,j,k,t;
int k1,k2,k3,a,b,c;
scanf("%d",&t);
while(t--){
scanf("%d%d%d%d%d%d%d",&n,&k1,&k2,&k3,&a,&b,&c);
double p0=(double)1.0/k1/k2/k3;
memset(A,,sizeof(A));
memset(B,,sizeof(B));
memset(p,,sizeof(p));
for(i=;i<=k1;++i){
for(j=;j<=k2;++j){
for(k=;k<=k3;++k){
p[i+j+k]+=p0;
}
}
}
p[a+b+c]-=p0;
for(i=n;i>=;--i){
A[i]=p0;
B[i]=;
for(j=;j<;++j)
A[i]+=p[j]*A[i+j],
B[i]+=p[j]*B[i+j];
}
printf("%.15f\n",B[]/(-A[]));
}
return ;
}

zoj-3329-期望/dp/方程优化的更多相关文章

  1. ZOJ 3329 期望DP

    题目大意: 给定3个已经规定好k1,k2,k3面的3个色子,如果扔到a,b,c则重新开始从1 计数,否则不断叠加所有面的数字之和,直到超过n,输出丢的次数的数学期望 我们在此令dp[]数组记录从当前数 ...

  2. 【bzoj5197】[CERC2017]Gambling Guide 期望dp+堆优化Dijkstra

    题目描述 给定一张n个点,m条双向边的无向图. 你要从1号点走到n号点.当你位于x点时,你需要花1元钱,等概率随机地买到与x相邻的一个点的票,只有通过票才能走到其它点. 每当完成一次交易时,你可以选择 ...

  3. zoj 3329 概率dp

    题意:有三个骰子,分别有k1,k2,k3个面.每个面值为1--kn每次掷骰子,如果三个面分别为a,b,c则分数置0,否则加上三个骰子的分数之和.当分数大于n时结束.求游戏的期望步数.初始分数为0 链接 ...

  4. LOJ #2541. 「PKUWC 2018」猎人杀(容斥 , 期望dp , NTT优化)

    题意 LOJ #2541. 「PKUWC 2018」猎人杀 题解 一道及其巧妙的题 , 参考了一下这位大佬的博客 ... 令 \(\displaystyle A = \sum_{i=1}^{n} w_ ...

  5. 【BZOJ4872】[Shoi2017]分手是祝愿 数学+期望DP

    [BZOJ4872][Shoi2017]分手是祝愿 Description Zeit und Raum trennen dich und mich. 时空将你我分开.B 君在玩一个游戏,这个游戏由 n ...

  6. poj 2096 Collecting Bugs && ZOJ 3329 One Person Game && hdu 4035 Maze——期望DP

    poj 2096 题目:http://poj.org/problem?id=2096 f[ i ][ j ] 表示收集了 i 个 n 的那个. j 个 s 的那个的期望步数. #include< ...

  7. poj 2096 , zoj 3329 , hdu 4035 —— 期望DP

    题目:http://poj.org/problem?id=2096 题目好长...意思就是每次出现 x 和 y,问期望几次 x 集齐 n 种,y 集齐 s 种: 所以设 f[i][j] 表示已经有几种 ...

  8. loj6171/bzoj4899 记忆的轮廊(期望dp+优化)

    题目: https://loj.ac/problem/6171 分析: 设dp[i][j]表示从第i个点出发(正确节点),还可以有j个存档点(在i点使用一个存档机会),走到终点n的期望步数 那么 a[ ...

  9. ZOJ - 3632 DP 单调优化

    题意:买瓜,每天的瓜有不同的价格和xu命时间,要求能苟到第n天的最小代价 定义DP方程\(dp[i]\),指苟到第\(i\)天的最小代价,所求即为\(dp[n]\) 那么怎么转移就是问题,这里的状态表 ...

随机推荐

  1. pta 习题集 5-2 找出不是两个数组共有的元素 (5分)

    给定两个整型数组,本题要求找出不是两者共有的元素. 输入格式: 输入分别在两行中给出两个整型数组,每行先给出正整数NN(≤20≤20),随后是NN个整数,其间以空格分隔. 输出格式: 在一行中按照数字 ...

  2. JavaCSV之写CSV文件

    与JavaCSV读CSV文件相对应,JavaCSV也可以用来写数据到CSV文件中. 1.准备工作 (1)第三方包库下载地址:https://sourceforge.net/projects/javac ...

  3. javascript 执行环境,作用域链和闭包

    首先看下这条语句: (function($) {…})(jQuery): 1.原理: function(arg){…}这就定义了一个匿名函数,参数为arg 而调用函数时,是在函数后面写上括号和实参的, ...

  4. http和socket之长连接和短连接区别

    TCP/IP TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层. 在网络层有IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议. 在传输层中有TCP协议与UDP协议. 在应 ...

  5. 什么是虚拟DOM?

    (摘抄自一篇文章,觉得这里写得非常不错,所以单独放出来,希望能对大家有帮助.)React为啥这么大?因为它实现了一个虚拟DOM(Virtual DOM).虚拟DOM是干什么的?这就要从浏览器本身讲起 ...

  6. IOS 自己定义UITableView

    依据不同须要,须要使用tableview的结构,可是里面每个cell,又须要自己的样式.所以学习了一下如何把自定义的cell加到tableview里面 首先要自己创建一个类,继承UITableView ...

  7. Openstack(十四)创建虚拟机

    启动虚拟机之前需要先做一些前期准备,比如网络和IP地址分配.虚拟机:类型创建等等,具体如下: 14.1网络规划及IP划分 #官网安装文档:https://docs.openstack.org/ocat ...

  8. [py]多态的理解

    多态 不同的数据类型,执行相同的方法,产生的状态不同 不同对象调用相同的方法(运行时候的绑定状态) #!/usr/bin/env python # coding=utf-8 class H2O: de ...

  9. Docker学习笔记(一):在本地安装和配置Docker

      由于公司里测试服务器时常会有变动,每次变动之后都需要在新的服务器上配置一遍环境,实在是麻烦.后来我突然想到了在网上看到的资料中说Docker能快速部署可移植的容器,所以我就试着用Docker搭建了 ...

  10. springcloud6---Eureka的配置:

    Eureka的配置: 自我保护:表示eureka进入了自我保护模式,eureka启动的时候会从高可用其他节点获取注册表信息,eureka client会每30秒发送心跳,如果eureka server ...