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, namelyDie1,
Die2 and Die3. Die1 hasK1 faces.
Die2has K2 faces.Die3 has
K3
faces. All the dice are fair dice, so the probability of rolling each value, 1 toK1,
K2, K3is 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 isa, the up-facing number of
    Die2 is b and the up-facing number ofDie3 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 integersn,
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

Author: CAO, Peng

Source: The 7th Zhejiang Provincial Collegiate Programming Contest

概率dp, 方程很好推, dp[i] 表示cnt = i时,达到目标状态的期望值

dp[i] = sigma(pk * dp[i+k]) + p0 * dp[0] + 1;

然后我就没想法了,因为涉及了dp[0], 然后参考了kuangbin博客才知道要转换求系数

看来概率dp还是练习的不够

/*************************************************************************
    > File Name: zoj3329.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2014年12月24日 星期三 16时06分54秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

double A[510], B[510];
int k1, k2, k3;
int a, b, c, n;

int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		double p0;
		scanf("%d%d%d%d%d%d%d", &n, &k1, &k2, &k3, &a, &b, &c);
		memset (A, 0, sizeof(A));
		memset (B, 0, sizeof(B));
		p0 = (double)(1.0 / k1 / k2 / k3);
		for (int i = n; i >= 0; --i)
		{
			A[i] = p0;
			B[i] = 1;
			for (int j = 1; j <= k1; ++j)
			{
				for (int k = 1; k <= k2; ++k)
				{
					for (int l = 1; l <= k3; ++l)
					{
						if (j == a && k == b && l == c)
						{
							continue;
						}
						A[i] += p0 * A[i + j + k + l];
						B[i] += p0 * B[i + j + k + l];
					}
				}
			}
		}
		printf("%.15f\n", B[0] / (1 - A[0]));
	}
	return 0;
}
时间: 2024-10-26 17:52:08

zoj3329---One Person Game的相关文章

ZOJ3329之经典概率DP

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

ZOJ-3329 One Person Game (有环期望问题)

题目大意:有3个骰子,各有k1,k2,k3个面,面值为1~ki.还有一个计数器,初始值为0,统计所有的面值和.每次同时置这三个骰子,如果第一个骰子的朝上的值为a.第二个值为b.第三个值为c,那么将计数器置为零.直到计数器的值大于n时结束,求次数的期望值. 题目分析:这道题的状态转移方程不难写.定义状态dp(i)表示计数器值为 i 时还可以置几次,另外定义pk表示一次置出的3个骰子之和为k的概率,p0表示置出a.b.c的概率.则状态转移方程为:dp(i)= ∑pk*dp(i+k)+p0*dp(0)

zoj3329(概率dp)

题目连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754 题意:有三个骰子,分别有k1,k2,k3个面. 每次掷骰子,如果三个面分别为a,b,c则分数置0,否则加上三个骰子的分数之和. 当分数大于n时结束.求游戏结束时的期望步数. 分析:这题状态转移方程挺容易想,但递推化简时又是困难重重,还是得多练习. 设dp[i]表示在i分时到达目标状态的期望,pk为投掷k分的概率,p0为回到0的概率 则dp[i]=∑(pk*dp

ZOJ3329 概率DP变形

哇哦,感觉有点难哦,三个骰子,分别具有k1,k2,k3个面,抛骰子,若向上的一面分别对应为a,b,c,那么得分归0,否则得分加上三个骰子向上那一面数字之和,求得分超过n的时候抛骰子的次数的期望 一开始很容易想到常规的做法,以目标状态为边界,当前状态到目标状态所需要的期望为方程,dp[i]代表 当前到目标分数的期望,这是发现状态转移是这个样子的 dp[i] = dp[0] * p0 + sigma(pk * dp[i + k]) + 1: p0,代表抛到分数归0 的概率,pk代表抛到分数为k的概率

概率dp——逆推期望+循环迭代zoj3329

首先要推出dp[i]的期望方程,会发现每一项都和dp[0]相关, 那我们将dp[i]设为和dp[0]有关的式子dp[i]=a[i]*dp[0]+b[i],然后再回代到原来的期望方程里 然后进行整理,可以发现两个系数a[i],b[i]是可以逆推的,并且通过求出a[0],b[0]可以求出dp[0] #include<bits/stdc++.h> using namespace std; #define maxn 1050 double A[maxn],B[maxn],p[maxn]; int ma

ACM 中的期望,概率 问题

一个简易的入门:点击打开链接 大神的总结:点击打开链接     zerolock 我搞的题目:点击打开链接 前段时间一直在做概率的题目. 一.期望 其中求解期望问题刚开始一直不理解.后来做得多了有感觉. 例:(有放回) 在5件产品有4件正品,1件次品,从中任取2件,记其中含正品的个数个数为随机变量ξ,则ξ的数学期望Eξ是 1.6 在5件产品有4件正品,1件次品,从中任取2件,记其中含正品的个数个数为随机变量ξ,则ξ=2时所取次数的数学期望是1.16 第一个例子是平常学的期望,做一件事情各个结果的

[算法]概率与期望DP

前言 前两节主要针对题目分析,没时间的珂以跳过. 初步 首先举一道简单.经典的好题: [Lightoj1038]Race to 1 Again 懒得单独写,安利一下DennyQi同学的博客:https://www.cnblogs.com/qixingzhi/p/9346307.html. 很显然很多期望题的状态是和自己有关的,怎么办呢,难道不停的搜索自己? 上面的方法显然行不通,于是我们只能简单变形一下. 很容易列出方程: \[f[n]= \frac{\sum_{i=1}^{m}f[fac_n^