zoj 3329 概率dp

看了这么多,也就是个递推

 1 /*
 2 ZOJ 3329
 3 题意:有三个骰子,分别有k1,k2,k3个面。
 4 每次掷骰子,如果三个面分别为a,b,c则分数置0,否则加上三个骰子的分数之和。
 5 当分数大于n时结束。求游戏的期望步数。初始分数为0
 6
 7 设dp[i]表示达到i分时到达目标状态的期望,pk为投掷k分的概率,p0为回到0的概率
 8 则dp[i]=∑(pk*dp[i+k])+dp[0]*p0+1;
 9 都和dp[0]有关系,而且dp[0]就是我们所求,为常数
10 设dp[i]=A[i]*dp[0]+B[i];
11 代入上述方程右边得到:
12 dp[i]=∑(pk*A[i+k]*dp[0]+pk*B[i+k])+dp[0]*p0+1
13      =(∑(pk*A[i+k])+p0)dp[0]+∑(pk*B[i+k])+1;
14      明显A[i]=(∑(pk*A[i+k])+p0)
15      B[i]=∑(pk*B[i+k])+1
16      先递推求得A[0]和B[0].
17      那么  dp[0]=B[0]/(1-A[0]);
18 */
19 #include<stdio.h>
20 #include<string.h>
21 #include<iostream>
22 #include<algorithm>
23 using namespace std;
24
25 double A[600],B[600];
26 double p[100];
27 int main()
28 {
29     int T;
30     int k1,k2,k3,a,b,c;
31     int n;
32     scanf("%d",&T);
33     while(T--)
34     {
35         scanf("%d%d%d%d%d%d%d",&n,&k1,&k2,&k3,&a,&b,&c);
36         double p0=1.0/k1/k2/k3;
37         memset(p,0,sizeof(p));
38         for(int i=1;i<=k1;i++)
39           for(int j=1;j<=k2;j++)
40             for(int k=1;k<=k3;k++)
41               if(i!=a||j!=b||k!=c)
42                 p[i+j+k]+=p0;
43         memset(A,0,sizeof(A));
44         memset(B,0,sizeof(B));
45         for(int i=n;i>=0;i--)
46         {
47             A[i]=p0;B[i]=1;
48             for(int j=1;j<=k1+k2+k3;j++)
49             {
50                 A[i]+=A[i+j]*p[j];
51                 B[i]+=B[i+j]*p[j];
52             }
53         }
54         printf("%.16lf\n",B[0]/(1-A[0]));
55     }
56     return 0;
57 }
时间: 2024-10-14 01:08:32

zoj 3329 概率dp的相关文章

zoj 3329 概率dp 环

一个游戏,你手上有三个骰子,分别有k1, k2, k3面.每次投出这三个骰子,得到三个面x, y, z.并且你有一个计数器,如果投出a, b, c, 则计数器归零,否则计数器加上三面之和,计数器初始为零.如果计数器的值大于 n 则游戏胜利.求胜利所需投骰子次数的期望. 以计数器的值为状态,dp[i] 表述计数器的值为i的情况下投骰子的期望.得到转移方程 p[k] 表示投出点数总和为k的概率,k=0时表示投出计数器归零的概率. dp[i] = p[0]*dp[0] + Σ(dp[i+k]*p[k]

zoj 3299 概率dp

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3329 回头重推式子 题解:http://blog.csdn.net/morgan_xww/article/details/6775853#reply 学到: 1.目前做的两道期望的状态转移方程都是从大向小推,定义方式:dp[i][j][k]....  满足i,j,k时,要达到upbound(i),upbound(j),upbound(k),需要XX的期望 2.待定系数的方

zoj 3640 概率dp

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4808 Background     If thou doest well, shalt thou not be accepted? and if thou doest not well, sin lieth at the door. And unto thee shall be his desire, and thou shalt rule over him. And Cai

ZOJ Problem Set - 3329(概率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 3822概率dp

Domination Time Limit: 8 Seconds Memory Limit: 131072 KB Special Judge Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboard with N r

zoj 3822 概率dp

1 /* 2 题目大意:一个n*m的棋盘,每天放一个棋子,每行每列至少有一个棋子时结束.求达到每行每列至少有一个棋子的天数的数学期望. 3 */ 4 #include <iostream> 5 #include <cstdio> 6 #include <cstring> 7 using namespace std; 8 9 const int maxn=55; 10 double dp[maxn*maxn][maxn][maxn];//放i颗棋子,j行有棋子,k列有棋子

ZOJ 3329 期望DP

题目大意: 给定3个已经规定好k1,k2,k3面的3个色子,如果扔到a,b,c则重新开始从1 计数,否则不断叠加所有面的数字之和,直到超过n,输出丢的次数的数学期望 我们在此令dp[]数组记录从当前数值到结束的数学期望 假如有3个面数都为2的色子 那么dp[i] = 1.0 / 2/2/2 * dp[0] + 1.0/8*dp[i+3] +3.0/8*dp[i+4]+3.0/8*dp[i+5]+1.0/8*dp[i+6] + 1 当然那些下标大于i的dp值均为0 可是我们这样从后往前推会导致无法

zoj 3735 概率dp

Josephina and RPG Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge A role-playing game (RPG and sometimes roleplaying game) is a game in which players assume the roles of characters in a fictional setting. Players take responsibil

[ACM] ZOJ 3329 One Person Game (概率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