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的时候结束.求需要掷骰子的次数 ...
随机推荐
- jQuery DataTables 插件使用笔记
初始化 在页面中 <!DOCTYPE html> <html> <head> <link rel="stylesheet" type=&q ...
- 怎么样调试正在运行的exe?
最近在调虚幻的编辑器的时候遇到了一个问题. 调试模式运行UE4Editor.exe 实际上只是一个带参的命令行. 打开后,它又通过这个参数生成了一份详细配置,重新调用了自己.如图 这就悲剧了,断点都没 ...
- asp.net中生成缩略图并添加版权实例代码
这篇文章介绍了asp.net中生成缩略图并添加版权实例代码,有需要的朋友可以参考一下 复制代码代码如下: //定义image类的对象 Drawing.Image image,newimage; //图 ...
- python实现雅虎拍卖后台自动回复卖家消息
前些时间,公司让做一个自动回复卖家信息的程序,现在总结下(用python实现的) 1.登陆雅虎拍卖后台手动获取cookie文件 #coding=utf-8 import sqlite3 import ...
- mysql可视化管理工具
1.navicat for mysql 2.navicat premium 3.HeidiSQL 4.MySQLWorkbench
- MyEclipse创建WebService
使用Eclipse的话还要装web tool platform很多东西,用MyEclipse一步到位,创建WebService很方便. MyEclipse中有自己的Tomcat,要把事先在电脑上独立安 ...
- HTML DOM select() 方法
定义和用法 select() 方法用于选择该元素中的文本. 语法 textareaObject.select() 实例 下面的例子可选择文本框中的文本: <html> <head&g ...
- css3加载中
.loader { margin: 6em auto; font-size: 10px; position: relative; text-indent: -9999em; border-top: 1 ...
- BZOJ 1711: [Usaco2007 Open]Dingin吃饭
Description 农夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食. 每一头牛只喜欢吃一些食品和饮料而别的一概不吃.虽然他不一定能把所有牛喂饱,他还是想让尽可能多的牛吃到他们喜欢的食品和饮料. ...
- hdu 4722
比赛的时候这道题一直都没出来,承启提醒我之后还是一直WA: 其实规律早就找到了```` 其实这题还可以用数位dp来做,不过从来没写过,以后再贴: 代码: #include<iostream> ...