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

Author: CAO, Peng
Source: The 7th Zhejiang Provincial Collegiate Programming Contest

题意:有三个骰子,面值分别是k1,k2,k3。每次扔出的值之和加到ans上,问多少次才能ans>n;当然,当遇到k1=a,k2=b,k3=c时,ans=0;重新开始累加。

思路:

设dp[i]表示达到i分时到达目标状态的期望,pk为投掷k分的概率,p0为回到0的概率
则dp[i]=∑(pk*dp[i+k])+dp[0]*p0+1;
都和dp[0]有关系,而且dp[0]就是我们所求,为常数
设dp[i]=A[i]*dp[0]+B[i];
代入上述方程右边得到:
dp[i]=∑(pk*A[i+k]*dp[0]+pk*B[i+k])+dp[0]*p0+1
=(∑(pk*A[i+k])+p0)dp[0]+∑(pk*B[i+k])+1;
明显A[i]=(∑(pk*A[i+k])+p0) ,B[i]=∑(pk*B[i+k])+1
先递推求得A[0]和B[0]. 那么 dp[0]=B[0]/(1-A[0]);

 #include"bits/stdc++.h"

 #define db double
#define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, n) for(int i=0;i<n;i++)
using namespace std;
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db PI = acos(-1.0);
const db eps = 1e-;
const ll INF = 0x3fffffffffffffff; int t;
int n,k1,k2,k3,a,b,c;
db A[],B[];
db p[];
int main() {
ci(t);
while (t--) {
ci(n),ci(k1),ci(k2),ci(k3),ci(a),ci(b),ci(c);
memset(A,, sizeof(A));
memset(B,, sizeof(B));
memset(p,, sizeof(p));
db p0=1.0/(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]+=p0; for(int i=n;i>=;i--){
A[i]=p0,B[i]=;
for(int k=;k<=k1+k2+k3;k++) {
A[i] += p[k] * A[i + k];
B[i] += p[k] * B[i + k];
}
}
db ans=B[]/(-A[]);
printf("%.10f\n", ans);
}
return ;
}

ZOJ3329 概率DP的更多相关文章

  1. ZOJ3329之经典概率DP

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

  2. Codeforces 28C [概率DP]

    /* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队 ...

  3. HDU 4405 Aeroplane chess (概率DP)

    题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i  这个位置到达 n ...

  4. POJ 2096 Collecting Bugs (概率DP)

    题意:给定 n 类bug,和 s 个子系统,每天可以找出一个bug,求找出 n 类型的bug,并且 s 个都至少有一个的期望是多少. 析:应该是一个很简单的概率DP,dp[i][j] 表示已经从 j ...

  5. POJ 2151 Check the difficulty of problems (概率DP)

    题意:ACM比赛中,共M道题,T个队,pij表示第i队解出第j题的概率 ,求每队至少解出一题且冠军队至少解出N道题的概率. 析:概率DP,dp[i][j][k] 表示第 i 个队伍,前 j 个题,解出 ...

  6. 概率DP light oj 1030

    t组数据 n块黄金 到这里就捡起来 出发点1 到n结束  点+位置>n 重掷一次 dp[i] 代表到这里的概率 dp[i]=(dp[i-1]+dp[i-2]... )/6  如果满6个的话 否则 ...

  7. hdu 4050 2011北京赛区网络赛K 概率dp ***

    题目:给出1-n连续的方格,从0开始,每一个格子有4个状态,左右脚交替,向右跳,而且每一步的步长必须在给定的区间之内.当跳出n个格子或者没有格子可以跳的时候就结束了,求出游戏的期望步数 0:表示不能到 ...

  8. [转]概率DP总结 by kuangbin

    概率类题目一直比较弱,准备把kuangbin大师傅总结的这篇题刷一下! 我把下面的代码换成了自己的代码! 原文地址:http://www.cnblogs.com/kuangbin/archive/20 ...

  9. SGU 422 Fast Typing(概率DP)

    题目大意 某人在打字机上打一个字符串,给出了他打每个字符出错的概率 q[i]. 打一个字符需要单位1的时间,删除一个字符也需要单位1的时间.在任意时刻,他可以花 t 的时间检查整个打出来的字符串,并且 ...

随机推荐

  1. 微信公众平台开发——helloworld

    威信公众平台有两种模式:编辑模式 和 开发模式. 普通的功能可以通过编辑模式来搞定.开发模式具有更多的功能.让我们来使用开发模式开发helloword吧 步骤如下: 1.先注册一个公众号(https: ...

  2. Static 用法

    1.Static关键字含意:static译文是静态的,静止的,因此使用 static 修饰符声明属于类型本身而不是属于特定对象(new创建的对象)的静态成员. 2.修饰使用范围 static 修饰符可 ...

  3. 关于Java虚拟机内存原型的基本知识

    Java虚拟机内存原型的六个部分: 1.寄存器:我们在程序中无法控制 2.栈:存放基本类型的数据和对象的引用,但对象本身不存放在栈中,而是存放在堆中 3.堆:存放用new产生的数据 4.静态域:存放在 ...

  4. IOS SQLite函数总结

    SQL语句的种类 ●  数据定义语句(DDL:Data Definition Language) ●  包括create和drop等操作 ●  在数据库中创建新表或删除表(create table或 ...

  5. Prim算法求权数和,POJ(1258)

    题目链接:http://poj.org/problem?id=1258 解题报告: #include <iostream> #include <stdio.h> #includ ...

  6. 贪心,POJ(2709)

    题目链接:http://poj.org/problem?id=2709 解题报告: #include <stdio.h> #include <algorithm> #inclu ...

  7. jade文档声明和头尾标签

    作为一个页面,首先需要一个doctype的声明,它位于文档最上面的位置,放置html标签以前,用来告知浏览器当前这个页面用哪种html,或者xml的规范并解析页面   doctype html htm ...

  8. node入口文件分析和目录初始化

    1.需要安装的模块 npm install express npm install jade npm install mongoose npm install bower -g npm install ...

  9. U盘装CentOS6.4

    1.打开UltraISO,依次点击“文件”>“打开”,选择“CentOS-6.4-i386-bin-DVD1.iso”文件. 2.“启动”>“写入硬盘映像”,在“硬盘驱动器选择u盘”,写入 ...

  10. 移动页面请使用CSS3动画

    说到动画,我们一般会使用jQuery 中的animate(); 在PC浏览器中,是很方便的,由于PC的高性能,这种不断修改DOM的做法确实不会出现太大的问题,但是在手机端就不同了. 手机上使用jQue ...