【Gym - 101606F】Flipping Coins(概率dp)

题干:

Here’s a jolly and simple game: line up a row of N identical coins, all with the heads facing down onto the table and the tails upwards, and for exactly K times take one of the coins, toss it into the air, and replace it as it lands either heads-up or heads-down. You may keep all of the coins that are face-up by the end.

Being, as we established last year, a ruthless capitalist, you have resolved to play optimally to win as many coins as you can. Across all possible combinations of strategies and results, what is the maximum expected (mean average) amount you can win by playing optimally?

Input

One line containing two space-separated integers:

  N(1<=N<=400), the number of coins at your mercy;

  K(1<=K<=400), the number of flips you must perform.

Output

Output the expected number of heads you could have at the end, as a real number. The output

must be accurate to an absolute or relative error of at most 10-6.

题目大意:

给出N个硬币(N<=400),开始均反面朝上。每次挑出其中一个抛,连续抛K次(K<=400),求正面朝上的最大数学期望。

解题报告:

因为他说了是采取最佳策略,而不是每轮操作都随机选择一个,所以这题可以dp而不是直接算期望。

dp[i][j] = dp[i-1][j]\times 0.5 + dp[i-1][j-1]\times 0.5.

特别的,你会发现dp[i-1]这个维度中,只有dp[i-1][n]只有0.5倍转移了,还有0.5没有被转移

(不然你试试会发现最后\sum dp[k][i] \neq 1.0)那么那0.5去哪了呢?

其实是因为每轮操作必须选择一个(这样才是古典概型嘛,如果已经都正面朝上就可以结束了,那就是几何概型了就不能dp了?我猜),所以转移到dp[i][n-1] 那里去了。

例:n=2 时的dp转移表格(抛币次数为2)

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 = 2e5 + 5;
int n,k;
double dp[555][555];
int main()
{cin>>n>>k;dp[0][0]=1;for(int i = 1; i<=k; i++) {for(int j = 0; j<=n; j++) {dp[i][j] = (j>=1 ? dp[i-1][j-1]*0.5 : 0) + dp[i-1][j]*0.5;}dp[i][n-1] += dp[i-1][n]*0.5;}double ans = 0;for(int i = 0; i<=n; i++) ans += dp[k][i]*i;printf("%.10f\n",ans);return 0 ;
}

举一反三:

 

n个正面朝下的硬币,每轮随机挑选一个硬币并上抛让它自由下落,这样进行K轮,问正面朝上的硬币个数的期望是多少?

 

n个正面朝下的硬币,每轮随机挑选一个硬币并翻转,这样进行K轮,问正面朝上的硬币个数的期望是多少?

答:dp(i,j)表示i次后有j个朝上的概率,因为有j个朝上,所以转移到dp(i+1,j-1)的概率是j/n,另一个转移类似。这种dp的方法不需要太动脑,至于更好的数学方法,推起来还是麻烦的。

n个正面朝下的硬币,每轮随机挑选一个硬币并翻转,进行K轮停止或硬币全部朝上停止,问正面朝上的硬币个数的期望是多少?

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

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

相关文章

ROS actionlib学习(一)

actionlib是ROS中一个很重要的功能包集合&#xff0c;尽管在ROS中已经提供了srevice机制来满足请求—响应式的使用场景&#xff0c;但是假如某个请求执行时间很长&#xff0c;在此期间用户想查看执行的进度或者取消这个请求的话&#xff0c;service机制就不能满足了&#xff0c…

机器学习笔记(十五):推荐系统

目录 1&#xff09;Problem formulation 2&#xff09;Content-based recommendations 3&#xff09;Collaborative filtering 4&#xff09;Collaborative filtering algorithm 5&#xff09;Vectorization: Low rank matrix factorization 6&#xff09;Implementation…

*【CodeForces - 280C】Game on Tree(期望模型,期望的线性性)

题干&#xff1a; Momiji has got a rooted tree, consisting of n nodes. The tree nodes are numbered by integers from 1 to n. The root has number 1. Momiji decided to play a game on this tree. The game consists of several steps. On each step, Momiji chooses…

武侠风云(基础版)

基本任务&#xff1a; 1 建立角色类&#xff0c;角色拥有生命值的属性和攻击的方法&#xff0c;攻击值是随机的。 2 建立职业子类&#xff0c;刀客&#xff0c;&#xff08;伤害少&#xff0c;血量多&#xff09;剑客&#xff08;伤害正常&#xff0c;血量正常&#xff0c;有几…

机器学习笔记(十六):大规模机器学习

目录 1&#xff09;Learning with large datasets 2&#xff09;Stochastic gradient descent 3&#xff09;Mini-batch gradient descent 4&#xff09;Stochastic gradient descent convergence 1&#xff09;Learning with large datasets 回顾一下我们之前提到的这句…

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

题干&#xff1a; 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, K…

智能聊天机器人系统

# 智能聊天机器人系统 # 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…

【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;但…

【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…

【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请求包

【LightOJ - 1038】Race to 1 Again(概率dp,数学期望)

题干&#xff1a; Rimi learned a new thing about integers, which is - any positive integer greater than 1 can be divided by its divisors. So, he is now playing with this property. He selects a number N. And he calls this D. In each turn he randomly choose…

使用matplotlib进行简单的数据展示

import numpy as np import matplotlib.pyplot as plt# 解决中文乱码 plt.rcParams[font.sans-serif] [SimHei] # 用来正常显示中文标签 plt.rcParams[axes.unicode_minus] False # 用来正常显示负号# 建立一个坐标系 plt.subplot(1, 1, 1) # 指明x和y值 x np.array([1, 2…

由浅到深理解ROS(6)-坐标转换

转自 ROS 中对于多坐标系的处理是使用树型表示&#xff0c;在机器人自主导航中&#xff0c;ROS会构建这几个很重要的坐标系&#xff1a; base_link: 一般位于tf tree的最根部&#xff0c;物理语义原点一般为表示机器人中心&#xff0c;为相对机器人的本体的坐标系。(base_foot…