zoj3329 One Person Game
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:
- Set the counter to 0 at first.
- 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.
- 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的更多相关文章
- 概率dp的迭代方式小结——zoj3329,hdu4089,hdu4035
在推导期望方程时我们常常会遇到dp[i]和其他项有关联,那么这时候我们就难以按某个顺序进行递推 即难以通过已经确定的项来求出新的项 即未知数的相互关系是循环的 但是我们又可以确定和dp[i]相关联的项 ...
- ZOJ3329之经典概率DP
One Person Game Time Limit: 1 Second Memory Limit: 32768 KB Special Judge There is a very ...
- zoj3329(概率dp)
题目连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754 题意:有三个骰子,分别有k1,k2,k3个面. 每次掷骰子,如 ...
- ZOJ-3329 One Person Game (有环期望问题)
题目大意:有3个骰子,各有k1,k2,k3个面,面值为1~ki.还有一个计数器,初始值为0,统计所有的面值和.每次同时置这三个骰子,如果第一个骰子的朝上的值为a.第二个值为b.第三个值为c,那么将计数 ...
- 【ZOJ3329】One Person Game
题意 你有三枚色子,第i个色子有ki面,你有一个计数器. 1.开始的时候将计数器调至0 2.扔三个色子,如果色子1是a,色子2是b,色子3是c,则将计数器归零.否则计数器加上三个色子的和. 3.如果计 ...
- ZOJ3329 概率DP
One Person Game Time Limit: 1 Second Memory Limit: 32768 KB Special Judge There is a very ...
- ZOJ3329(数学推导+期望递推)
要点: 1.期望的套路,要求n以上的期望,则设dp[i]为i分距离终点的期望步数,则终点dp值为0,答案是dp[0]. 2.此题主要在于数学推导,一方面是要写出dp[i] = 什么,虽然一大串但是思维 ...
- 概率dp——逆推期望+循环迭代zoj3329
首先要推出dp[i]的期望方程,会发现每一项都和dp[0]相关, 那我们将dp[i]设为和dp[0]有关的式子dp[i]=a[i]*dp[0]+b[i],然后再回代到原来的期望方程里 然后进行整理,可 ...
- 【期望DP】[zoj3329]One Person Game
题描: 有三个均匀的骰子,分别有k1,k2,k3个面,初始分数是0, 当掷三个骰子的点数分别为a,b,c的时候,分数清零,否则分数加上三个骰子的点数和, 当分数>n的时候结束.求需要掷骰子的次数 ...
随机推荐
- ES6笔记-正则表达式和字符串正则方法
RegExp构造函数 在ES5中,RegExp构造函数的参数有两种情况. 第一种情况是,参数是字符串,这时第二个参数表示正则表达式的修饰符(flag). var regex = new RegExp( ...
- html 头部正常用法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Mvvm Light Toolkit for WPF/Silverlight系列之搭建mvvmlight开发框架
Mvvm Light Toolkit for WPF/Silverlight系列之搭建mvvmlight开发框架 本章节,我将通过示例介绍如何搭建mvvmlight开发环境.示例中的我会针对wpf ...
- 【转】【Top 100 Best Blogs for iOS Developers】
原文地址:http://www.softwarehow.com/best-blogs-for-ios-developers/ (by JP Zhang | Last updated: Apr 5, 2 ...
- “\n”与“\r”的区别
ASCII中“\n”代表着换行,“\r”代表着将光标移动到当前显示行的最左边.
- Bootstrap 轮播(Carousel)插件
Bootstrap 轮播(Carousel)插件是一种灵活的响应式的向站点添加滑块的方式.除此之外,内容也是足够灵活的,可以是图像.内嵌框架.视频或者其他您想要放置的任何类型的内容. 如果您想要单独引 ...
- django celery redis简单测试
希望在下一版中,能用这个小芹菜,来实现异步的多任务并行哈. 安装REDIS之类的不表,只说在DJANGO当中要注意配置的事项. 0,安装插件 yum install redis-server pip ...
- Android Canvas使用drawBitmap绘制图片
1.基本的绘制图片方法 //Bitmap:图片对象,left:偏移左边的位置,top: 偏移顶部的位置 drawBitmap(Bitmap bitmap, float left, float top, ...
- 【CF】86 B. Petr#
误以为是求满足条件的substring总数(解法是KMP分别以Sbeg和Send作为模式串求解满足条件的position,然后O(n^2)或者O(nlgn)求解).后来发现是求set(all vali ...
- 产品设计中先熟练使用铅笔 不要依赖Axure
在互联网产品领域,Axure已成为产品经理.产品设计师以及交互设计师的必备工具,从某种程度讲,Axure帮助我们建立低保真模型,便于与用户的需求验证,也帮助我们构思交互细节,使前端和开发人员更容易理解 ...