【ZOJ - 3329】One Person Game(带循环的概率dp,数学期望,高斯消元,数学)

题干:

There is a very simple and interesting one-person game. You have 3 dice, namely Die1Die2 and Die3Die1 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 K1K2K3 is 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 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.
  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 integers nK1K2K3abc (0 <= n <= 500, 1 < K1K2K3 <= 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

题目大意:有三个均匀的骰子,分别有k1,k2,k3个面分别代表点数1~k1,1~k2,1~k3。初始分数是0。每一回合掷一波骰子,当掷三个骰子的点数分别为a,b,c的时候,分数清零,否则分数加上三个骰子的点数和,当分数>n的时候结束。求需要掷骰子的次数的期望。

解题报告:

自己讲的也不好,也是参考一个题解吧:(链接)

写出的公式是有后续性的。我们需要弄一个递推公式,消去后续性。

本题通过代换系数,化简后求系数。一般形成环的用高斯消元法求解。但是此题都是和dp[0]相关。所有可以分离出系数。
设dp[i]表示达到i分时到达目标状态的期望,pk为投掷k分的概率,p0为回到0的概率
则dp[i]=∑(pk*dp[i+k])+dp[0]*p0+1;
都和dp[0]有关系,而且dp[0]就是我们所求,为常数
设dp[i]=A[i]*dp[0]+B[i];
代入上述方程右边得到:
dp[i]=∑(pk*A[i+k]*dp[0]+pk*B[i+k])+dp[0]*p0+1=(∑(pk*A[i+k])+p0)dp[0]+∑(pk*B[i+k])+1;明显A[i]=(∑(pk*A[i+k])+p0)B[i]=∑(pk*B[i+k])+1先递推求得A[0]和B[0].那么  dp[0]=B[0]/(1-A[0]);

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 1000 + 5;
ll ans;
int k1,k2,k3,a,b,c,n;
int ci[666];
double A[MAX],B[MAX],dp[MAX];//dp[i]代表当前分数为i时,结束游戏所要掷骰子的次数的期望值 
int main()
{int t;cin>>t;while(t--) {scanf("%d%d%d%d%d%d%d",&n,&k1,&k2,&k3,&a,&b,&c);memset(ci,0,sizeof ci);for(int i = 0; i<MAX; i++) A[i]=B[i]=0;for(int i = 1; i<=k1; i++) {for(int j = 1; j<=k2; j++) {for(int k = 1; k<=k3; k++) {if(i == a && j == b && k == c) continue;ci[i+j+k]++;}}}double p0=1.0/(k1*k2*k3);for(int i = n; i>=0; i--) {for(int j = 3; j<=k1+k2+k3; j++) {A[i] += ci[j]*1.0/(k1*k2*k3) * A[i+j];B[i] += ci[j]*1.0/(k1*k2*k3) * B[i+j];}A[i] += p0;B[i] += 1;}printf("%.12f\n",B[0]/(1-A[0]));}return 0 ;
}

总结:

刚开始WA了半天,发现是初始化的时候直接把ci和A数组放到一起用for初始化了,但是其实这样是不行的,因为你MAX=1000,但是ci只开到了666,所以就越界了呀。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440063.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

智能聊天机器人系统

# 智能聊天机器人系统 # 1.系统简介 # 随着社会的各个公司以及大学对人工智能技术的深入研究和快速发展&#xff0c;人工智能技术将逐步应用到 # 方方面面。智能聊天机器人系统是基于各类传感器收集人类语音数据&#xff08;智能电视、智能空调&#xff0c; # 智能冰箱、智能音…

机器学习笔记:总结

下面是我们本课程学到的要点&#xff1a; 1&#xff09;监督学习&#xff1a;线性回归&#xff0c;逻辑回归&#xff0c;神经网络&#xff0c;SVM&#xff1b; 2&#xff09;无监督学习&#xff1a;k均值&#xff0c;PCA&#xff0c;异常检测 3&#xff09;特别应用&#xf…

ROS探索总结(十二)——坐标系统

在机器人的控制中&#xff0c;坐标系统是非常重要的&#xff0c;在ROS使用tf软件库进行坐标转换。 相关链接&#xff1a;http://www.ros.org/wiki/tf/Tutorials#Learning_tf 一、tf简介 我们通过一个小小的实例来介绍tf的作用。 1、安装turtle包 <span>$ rosdep instal…

【BZOJ - 3036】绿豆蛙的归宿(概率DAG图dp,拓扑排序,概率dp,期望的线性性)

题干&#xff1a; 随着新版百度空间的下线&#xff0c;Blog宠物绿豆蛙完成了它的使命&#xff0c;去寻找它新的归宿。 给出一个有向无环的连通图&#xff0c;起点为1终点为N&#xff0c;每条边都有一个长度。绿豆蛙从起点出发&#xff0c;走向终点。 到达每一个顶点时&#x…

python菜鸟100例精选

# 有四个数字&#xff1a;1、2、3、4&#xff0c;能组成多少个互不相同且无重复数字的三位数&#xff1f;各是多少&#xff1f; def four_number():n 0for i in range(1,5):for j in range(1,5):for k in range(1,5):if (i ! j) and (i!k) and (j!k):n 1# print(i,j,k)print(…

机器学习笔记:Overview

1&#xff09;机器学习笔记&#xff08;1&#xff09;&#xff1a;Introduction 2&#xff09;机器学习笔记&#xff08;2&#xff09;&#xff1a;单变量线性回归 3&#xff09;机器学习笔记&#xff08;3&#xff09;&#xff1a;线性代数回顾 4&#xff09;机器学习笔记&…

【LightOJ - 1030】Discovering Gold(概率dp,数学期望,期望的线性性)

题干&#xff1a; You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave can contain any amount of gold. Initially you are in position 1. Now each turn you throw a perfect 6 sided dice. If you get X in the dice …

【LightOJ - 1079】Just another Robbery(概率dp,概率背包)

题干&#xff1a; As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated risk, and grab as much money as possible. But his friends - Hermi…

强烈推荐的TensorFlow、Pytorch和Keras的样例资源(深度学习初学者必须收藏)

目录 一、TensorFlow 二、Keras 三、Pytorch 总结 本文转自微信公众号&#xff1a;机器学习初学者 原创&#xff1a; 机器学习初学者 机器学习初学者 TensorFlow、Keras和Pytorch是目前深度学习的主要框架&#xff0c;也是入门深度学习必须掌握的三大框架&#xff0c;但…

由浅到深理解ROS(1)

ROS机器人操作系统 &#xff08; Robot Operating System 或简称 ROS&#xff09;&#xff0c;可以帮助提高机器人软件的开发效率。ROS能够提供类似传统操作系统的诸多功能&#xff0c;如硬件抽象、底层设备控制、常用功能实现、进程间消息传递和程序包管理等。此外&#xff0c…

Centos7换yum源

1.进入到yum的源目录下 cd /etc/yum.repos.d/2.将原来的CentOS-Base.repo进行备份 mv CentOS-Base.repo CentOS-Base.repo_back3.下载阿里源 http://mirrors.aliyun.com/repo/Centos-7.repo 到/etc/yum.repos.d/目录下 4.改变名字 mv Centos-7.repo CentOS-Base.repo5.生成…

(3)链表

目录 下面是关于链表的一些知识点&#xff1a; 链表的操作&#xff1a; 创建链表 遍历链表 判断链表是否为空 求链表长度 插入元素 删除元素 排序链表 这篇笔记是根据郝斌老师的上课讲义整理而得&#xff1a; 前面介绍了连续的存储结构&#xff1a;数组&#xff0c;今…

【LightOJ - 1027】A Dangerous Maze(概率dp,数学期望)

题干&#xff1a; You are in a maze; seeing n doors in front of you in beginning. You can choose any door you like. The probability for choosing a door is equal for all doors. If you choose the ith door, it can either take you back to the same position wh…

由浅到深理解ROS(2)

ROS文件系统 用户可以直接参看官网&#xff1a; http://wiki.ros.org/ROS/Tutorials/NavigatingTheFilesystem ROS文件系统中的两个最基本的概念&#xff1a;Package和Manifest&#xff0c;即包和清单文件。 &#xff08;1&#xff09;Package是组织ROS代码的最基本单位&…

Django员工管理系统

Django员工管理系统&#xff08;ems&#xff09; 需求分析&#xff1a; 1.实现管理员的注册、登陆页面注册&#xff1a;用户名、真实名字、密码、确认密码、性别、验证码用户名需要判断是否合法、是否存在&#xff0c;loading图片提示密码和确认密码是否相同&#xff0c;load…

(4)栈

目录 栈的知识点&#xff1a; 栈的操作&#xff1a; 创建栈&#xff1a; 压栈&#xff1a; 遍历栈&#xff1a; 判断栈是否为空&#xff1a; 出栈 清空栈 这篇笔记是根据郝斌老师的上课讲义整理而得。 栈的知识点&#xff1a; 栈的定义&#xff1a;一种可以实现"…

【LightOJ - 1104】Birthday Paradox(概率,思维)

题干&#xff1a; Sometimes some mathematical results are hard to believe. One of the common problems is the birthday paradox. Suppose you are in a party where there are 23 people including you. What is the probability that at least two people in the party…

爬虫小记

中国商标网 找到正确的入口 在此页面加入全部data数据获取xhr请求包

由浅到深理解ROS(3)-命名空间

全局命名空间&#xff1a; /rosout前面的反斜杠“/”表明该节点名称属于全局命名空间。之所以叫做全局名称因为它们在任何地方&#xff08;包括代码、命令行工具、图形界面工具等的任何地方&#xff09;都可以使用。无论这些名称用作众多命令行工具的参数还是用在节点内部&…

(5)队列

目录 队列知识点&#xff1a; 循环队列&#xff1a; 队列的操作&#xff1a; 创建队列&#xff1a; 判断队列是否已满&#xff1a; 入队&#xff1a; 遍历队列&#xff1a; 判断队列是否为空&#xff1a; 出队&#xff1a; 这篇笔记是根据郝斌老师的上课讲义整理而得&…