zoj-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 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
f[i]表示已经得到i分之后距离目标的期望次数,pk表示得分为k的概率,则有f[i]=SUM{ p[k]*f[i+k] } + p0*f[0] + 1 ,因为下一次操作可能清零或者组合成其他点数,要分开讨论。这个式子无法直接递推,我们需要简化一下。
可以看出f[i]均和f[0]有关,不妨令f[i]=A[i]*f[0]+B[i] ,带入上式得到 f[i]=(sum{ p[k]*A[i+k] } + p0)*f[0]+(SUM{ pk*B[i+k] } +1 ) ,可以看出A[i]=SUM{ pk*A[i+k] }+p0 , B[i]=SUM{ pk*B[i+k] }+1 ,A[i]和B[i]可以递推得到,所以答案就是A[0]/(1-B[0]);
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<stack>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<time.h>
#include<algorithm>
using namespace std;
#define mp make_pair
#define pb push_back
#define debug puts("debug")
#define LL long long
#define pii pair<int,int>
#define eps 1e-12 double p[];
double A[],B[];
int main()
{
int n,m,i,j,k,t;
int k1,k2,k3,a,b,c;
scanf("%d",&t);
while(t--){
scanf("%d%d%d%d%d%d%d",&n,&k1,&k2,&k3,&a,&b,&c);
double p0=(double)1.0/k1/k2/k3;
memset(A,,sizeof(A));
memset(B,,sizeof(B));
memset(p,,sizeof(p));
for(i=;i<=k1;++i){
for(j=;j<=k2;++j){
for(k=;k<=k3;++k){
p[i+j+k]+=p0;
}
}
}
p[a+b+c]-=p0;
for(i=n;i>=;--i){
A[i]=p0;
B[i]=;
for(j=;j<;++j)
A[i]+=p[j]*A[i+j],
B[i]+=p[j]*B[i+j];
}
printf("%.15f\n",B[]/(-A[]));
}
return ;
}
zoj-3329-期望/dp/方程优化的更多相关文章
- ZOJ 3329 期望DP
题目大意: 给定3个已经规定好k1,k2,k3面的3个色子,如果扔到a,b,c则重新开始从1 计数,否则不断叠加所有面的数字之和,直到超过n,输出丢的次数的数学期望 我们在此令dp[]数组记录从当前数 ...
- 【bzoj5197】[CERC2017]Gambling Guide 期望dp+堆优化Dijkstra
题目描述 给定一张n个点,m条双向边的无向图. 你要从1号点走到n号点.当你位于x点时,你需要花1元钱,等概率随机地买到与x相邻的一个点的票,只有通过票才能走到其它点. 每当完成一次交易时,你可以选择 ...
- zoj 3329 概率dp
题意:有三个骰子,分别有k1,k2,k3个面.每个面值为1--kn每次掷骰子,如果三个面分别为a,b,c则分数置0,否则加上三个骰子的分数之和.当分数大于n时结束.求游戏的期望步数.初始分数为0 链接 ...
- LOJ #2541. 「PKUWC 2018」猎人杀(容斥 , 期望dp , NTT优化)
题意 LOJ #2541. 「PKUWC 2018」猎人杀 题解 一道及其巧妙的题 , 参考了一下这位大佬的博客 ... 令 \(\displaystyle A = \sum_{i=1}^{n} w_ ...
- 【BZOJ4872】[Shoi2017]分手是祝愿 数学+期望DP
[BZOJ4872][Shoi2017]分手是祝愿 Description Zeit und Raum trennen dich und mich. 时空将你我分开.B 君在玩一个游戏,这个游戏由 n ...
- poj 2096 Collecting Bugs && ZOJ 3329 One Person Game && hdu 4035 Maze——期望DP
poj 2096 题目:http://poj.org/problem?id=2096 f[ i ][ j ] 表示收集了 i 个 n 的那个. j 个 s 的那个的期望步数. #include< ...
- poj 2096 , zoj 3329 , hdu 4035 —— 期望DP
题目:http://poj.org/problem?id=2096 题目好长...意思就是每次出现 x 和 y,问期望几次 x 集齐 n 种,y 集齐 s 种: 所以设 f[i][j] 表示已经有几种 ...
- loj6171/bzoj4899 记忆的轮廊(期望dp+优化)
题目: https://loj.ac/problem/6171 分析: 设dp[i][j]表示从第i个点出发(正确节点),还可以有j个存档点(在i点使用一个存档机会),走到终点n的期望步数 那么 a[ ...
- ZOJ - 3632 DP 单调优化
题意:买瓜,每天的瓜有不同的价格和xu命时间,要求能苟到第n天的最小代价 定义DP方程\(dp[i]\),指苟到第\(i\)天的最小代价,所求即为\(dp[n]\) 那么怎么转移就是问题,这里的状态表 ...
随机推荐
- C#读取xml文件指定节点下的值
#region 读取xml文件指定节点下的值 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(result); XmlNode root ...
- Nginx配置ssl安全证书
server { listen 443; server_name www.loaclhost.com; ssl on; root /web; ssl_certificate /data/ssl/xxx ...
- 快速定位oracle故障-恩墨
首先我们要明白一点,所谓的故障,意味着相对来讲比较严重.也就是可能比不同的问题要严重一些,比如锁等待. 要能够快速的定位和解决问题,恢复业务正常:首先我们需要了解Oracle的一些常见的故障有哪些. ...
- android的一些类库的优缺点
经过本人的面试经验,以及接触的android项目,总结了一下android的一些类库的优缺点: 一,线程方面 1.AsyncTask 首先是线程优化以及缺陷方面,针对目前大多数类库来说,都有好的设计方 ...
- SQL Server 2012 AlwaysON 同步延迟时间
SELECT availability_mode_desc , role_desc , replica_server_name , last_redone_time , GETDATE() now , ...
- Spark Shuffle(三)Executor是如何fetch shuffle的数据文件(转载)
1. 前言 在前面的博客中讨论了Executor, Driver之间如何汇报Executor生成的Shuffle的数据文件,以及Executor获取到Shuffle的数据文件的分布,那么Executo ...
- 朴素贝叶斯算法原理及Spark MLlib实例(Scala/Java/Python)
朴素贝叶斯 算法介绍: 朴素贝叶斯法是基于贝叶斯定理与特征条件独立假设的分类方法. 朴素贝叶斯的思想基础是这样的:对于给出的待分类项,求解在此项出现的条件下各个类别出现的概率,在没有其它可用信息下,我 ...
- 29张截图-全新安装CentOS7.5-超详细!
目录 全新安装CentOS7.5 配置虚拟机 调整网卡名称 配置时区,分区,关闭安全工具 配置网络参数 配置root账户密码 参考链接 全新安装CentOS7.5 可以到这里下载镜像https://m ...
- SpringData_PagingAndSortingRepository接口
该接口提供了分页与排序功能 Iterable<T> findAll(Sort sort); //排序 Page<T> findAll(Pageable pageable); / ...
- 24UDP通信
使用Qt提供的QUdpSocket进行UDP通信.在UDP方式下,客户端并不与服务器建立连接,它只负责调用发送函数向服务器发送数据.类似的服务器也不从客户端接收连接,只负责调用接收函数,等待来自客户端 ...