ZOJ Problem Set - 3329(概率DP)

时间:2022-09-02 15:38:50

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[0]相关。所有可以分离出系数。

dp[i]表示达到i还要掷几次的期望,每一项都和dp[0]有关,且可表示成dp[i]=A[i]*dp[0]+B[0];

所以只要求出dp[0]的系数A,B就可以求出dp[0]=B[0]/(1-A[0]);

dp[n] = dp[0]/k1/k1/k1+1;

然后递推可推出dp[0]的系数;

 #include<iostream>
#include<cstdio>
#include<cstring>
#define M(a,b) memset(a,b,sizeof(a)) using namespace std; double A[],B[];
int n,k1,k2,k3,a,b,c; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
M(A,);
M(B,);
scanf("%d%d%d%d%d%d%d",&n,&k1,&k2,&k3,&a,&b,&c);
A[n] = 1.0/(k1*k2*k3);
B[n] = ;
for(int i = n-;i>=;i--)
{
for(int p = ;p<=k1;p++)
for(int q = ;q<=k2;q++)
for(int r = ;r<=k3;r++)
{
if(p!=a||q!=b||r!=c)
{
A[i] += A[i+p+q+r]/(k1*k2*k3);
B[i] += B[i+p+q+r]/(k1*k2*k3);
}
//cout<<A[i]<<' '<<B[i]<<endl;
}
A[i]+=(1.0/(k1*k2*k3));
B[i]+=;
//cout<<A[i]<<' '<<B[i]<<endl;
}
double ans = B[]/(-A[]);
printf("%.16f\n",ans);
}
return ;
}

ZOJ Problem Set - 3329(概率DP)的更多相关文章

  1. zoj 3329 概率dp

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

  2. Code Jam 2008 APAC local onsites Problem C&period; Millionaire —— 概率DP

    题意: 你有X元钱,进行M轮赌博游戏.每一轮可以将所持的任意一部分钱作为赌注(赌注为0元表示这一轮不押),赌注可以是小数的,不是一定要整数.每一轮 赢的概率为P,赢了赌注翻倍,输了赌注就没了.如果你最 ...

  3. ZOJ 3822 Domination &lpar;三维概率DP&rpar;

    E - Domination Time Limit:8000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submi ...

  4. ZOJ Problem Set - 3329 One Person Game

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

  5. zoj 3822(概率dp)

    ZOJ Problem Set - 3822 Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Ju ...

  6. ZOJ Problem Set - 3822Domination&lpar;DP&rpar;

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

  7. BZOJ 2318&colon; Spoj4060 game with probability Problem&lpar; 概率dp &rpar;

    概率dp... http://blog.csdn.net/Vmurder/article/details/46467899 ( from : [辗转山河弋流歌 by 空灰冰魂] ) 这个讲得很好 , ...

  8. ZOJ Problem Set - 2563 Long Dominoes 【如压力dp】

    称号:ZOJ Problem Set - 2563 Long Dominoes 题意:给出1*3的小矩形.求覆盖m*n的矩阵的最多的不同的方法数? 分析:有一道题目是1 * 2的.比較火.链接:这里 ...

  9. ZOJ Problem Set - 2297 Survival 【状压dp】

    题目:ZOJ Problem Set - 2297 Survival 题意:给出一些怪,有两个值,打他花费的血和能够添加的血,然后有一个boss,必须把小怪全部都打死之后才干打boss,血量小于0会死 ...

随机推荐

  1. npm 私有模块的管理使用

    你可以使用 NPM 命令行工具来管理你在 NPM 仓库的私有模块代码,这使得在项目中使用公共模块变的更加方便. 开始前的工作 你需要一个 2.7.0 以上版本的 npm ,并且需要有一个可以登陆 np ...

  2. 一种简单的实现:Android一键换肤功能

    现在的APP开发,通常会提供APP的换肤功能,网上流传的换肤代码和实现手段过于复杂,我把原作者的代码重新整理抽取出来,转换成Eclipse项目,重新整理成正确.可直接运行的项目. 代码运行结果如图. ...

  3. 第 29 章 CSS3 弹性伸缩布局&lbrack;下&rsqb;

    学习要点: 1.新版本 主讲教师:李炎恢 本章主要探讨 HTML5 中 CSS3 提供的用来实现未来响应式弹性伸缩布局方案,这里做一个初步的了解. 一.新版本 新版本的 Flexbox 模型是 201 ...

  4. iOS 进阶 第九天&lpar;0408&rpar;

    0408 makekeyAndVisible解释 一个程序可以有多个Window,但只有一个窗口能够成为主窗口.如图中所示,此时的window2是主窗口.主窗口用处大了.从iOS7开始无论是主窗口还是 ...

  5. Photoshop图象切片保存为网页HTML(DIV&plus;CSS布局)的方法

    首先,制作图象切片(以一张图片为例子) 一.选择“切片”工具,在图像上拖动以分割图像(例如:一张图像切割2次就形成3个切片)切片后如下图 二.设置切片选项(如大小.目标链接.图片说明等等):选择“切片 ...

  6. &lbrack;Oracle&rsqb;Audit(二)--清理Audit数据

    在上一篇,初步了解了Audit的作用以及如何使用Audit,本篇记录如何手动清理Audit数据. (一) 概述 Audit的数据主要存储在sys.aud$表中,该表默认位于system表空间中,我们根 ...

  7. MySQL大表优化方案

    转:https://segmentfault.com/a/1190000006158186?hmsr=toutiao.io&utm_medium=toutiao.io&utm_sour ...

  8. 【小o地图Excel插件版】计算两点间驾车路径,获取途径道路、驾车距离、耗时等信息

    小o地图Excel插件版:一款基于Excel软件开发的地图软件,提供基于Excel表格进行地理数据挖掘.地理数据分析.地图绘制.地图图表等功能的工具类软件.具有易用.高效.稳定的特点,能够满足地理数据 ...

  9. Docker 国内仓库和镜像

    Docker 国内仓库和镜像 由于网络原因,我们在pull Image 的时候,从Docker Hub上下载会很慢...所以,国内的Docker爱好者们就添加了一些国内的镜像(mirror),方便大家 ...

  10. 5285&colon; &lbrack;Hnoi2018&rsqb;寻宝游戏

    5285: [Hnoi2018]寻宝游戏 链接 分析: 从下面依次确定运算符号,然后在确定的过程中,需要确定的位数会逐渐减少.比如最后有一个1,如果在从下往上确定了一个or 1,那么再往前可以随便选了 ...