One Person Game

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

code

大米饼的博客

 #include<cstdio>
#include<algorithm>
#include<cstring> using namespace std;
const int N = ;
double p[N],x[N],y[N],t; int main() { int T,n,k1,k2,k3,a,b,c,sum;
scanf("%d",&T);
while (T--) {
scanf("%d%d%d%d%d%d%d",&n,&k1,&k2,&k3,&a,&b,&c);
memset(p,,sizeof(p));
memset(x,,sizeof(x));
memset(y,,sizeof(y)); t = 1.0/(double(k1*k2*k3));
sum = k1+k2+k3; for (int i=; i<=k1; ++i)
for (int j=; j<=k2; ++j)
for (int k=; k<=k3; ++k)
if (i!=a || j!=b || k!=c) p[i+j+k] += t; for (int i=n; i>=; --i) {
x[i] = t;y[i] = 1.0;
for (int k=; k<=sum; ++k)
x[i] += p[k]*x[i+k],y[i] += p[k]*y[i+k];
} printf("%.15lf\n",y[]/(1.0-x[])); }
return ;
}

ZOJ 3329 Problem Set (期望dp)的更多相关文章

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

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

  2. 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< ...

  3. ZOJ Problem Set - 3822Domination(DP)

    ZOJ Problem Set - 3822Domination(DP) problemCode=3822">题目链接 题目大意: 给你一个n * m的棋盘,每天都在棋盘上面放一颗棋子 ...

  4. ZOJ 3822 Domination 期望dp

    Domination Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem ...

  5. poj 2096 Collecting Bugs(期望 dp 概率 推导 分类讨论)

    Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other ...

  6. [CF697D]Puzzles 树形dp/期望dp

    Problem Puzzles 题目大意 给一棵树,dfs时随机等概率选择走子树,求期望时间戳. Solution 一个非常简单的树形dp?期望dp.推导出来转移式就非常简单了. 在经过分析以后,我们 ...

  7. B20J_1419_Red Is Good_期望DP

    B20J_1419_red is good_期望DP 题意:有R张红牌和B张黑牌,一张一张地翻牌,翻到红牌得到1美元,黑牌则付出1美元.可以随时停止翻牌,在最优策略下平均能得到多少钱. 分析:期望DP ...

  8. 概率和期望dp

    概率和期望dp 概率和期望好神啊,完全不会. 网上说概率要顺着推,期望要逆着推,然而我目前做的概率期望题正好都与此相反2333   概率: 关于概率:他非常健康 初中概率题非常恐怖.现在来思考一道题: ...

  9. poj2096 Collecting Bugs[期望dp]

    Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 5394   Accepted: 2670 ...

随机推荐

  1. CODESOFT条码设计软件如何隐藏数据源方法

    作为强大的条码标签设计软件,用户在用CODESOFT设计条码标签时,有时需要根据实际情况,将条码数据源隐藏,也就是使设计与打印出来的条形码下不带有数据.那么这要怎么在CODESOFT中实现呢?下面,小 ...

  2. 使用Atom编写Makedown

    Atom 是 Github 专门为程序员推出的一个跨平台文本编辑器. Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式. 作为一个Gi ...

  3. C. Tanya and Toys_模拟

    C. Tanya and Toys time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. grep的使用

    http://www.eguidedog.net/linux-tutorial/05-grep.php grep apple fruitlist.txt:在fruitlist.txt中查找apple字 ...

  5. sz 命令

    sz命令 下载文件命令 sz  文件名

  6. Bean的初始化和销毁

    在我们实际开发的时候,经常会遇到在Bean在使用之前或者之后做些必要的操作,Spring对Bean的生命周期的操作提供了支持.在使用Java配置和注解配置下提供如下两种方式.    1.Java配置方 ...

  7. svn: 处于冲突状态

    svn: 提交失败(细节如下):svn: 提交终止: “/home/usa/svn/aispeech/air201102/branches/opt-vite/wvite” 处于冲突状态 删除文件夹wv ...

  8. Win10预览版激活信息

    微软在10月2日零点正式公开了Win10预览版的下载地址,这个时间大家应该逐步开始安装工作了,因此提出下面两个问题的用户特别多,IT之家再稍作告知一下.1.Win10预览版安装密钥是什么?答:NKJF ...

  9. JAVA文件操作工具类(读、增、删除、复制)

    使用JAVA的JFinal框架 1.上传文件模型类UploadFile /** * Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com). * ...

  10. 关于discuz 不能全文搜索的问题

    这个问题客服反馈很多次了,以为discuz 默认搜索只能搜标题,除非配置了sphinx全文搜索引擎. 但是之前比较老的员工说以前能用的,也就是discuz老版本. 今天突然想到是不是discuz纵横搜 ...