赌徒破产问题,做tc时遇到,顺便拿来好好研究下

英文原版地址为:Gambler's Ruin

问题如下:

一个赌徒有h枚金币,每次有概率a获得一枚金币或者概率(1-a)丢掉一枚金币,直到其所有的金币总数达到N或0则游戏结束,求赌徒最终赢得N枚金币的概率P(N|h)。

对于两个状态我们可以确定,即P(N|N)=1、P(N|0)=0。同时得出状态转移公式(概率的推导和普通的DP还是很不一样的,好好体会下):

P(N|h) = a*P(N|h+1) + (1-a)*P(N|h-1)

这类公式可以表示为二阶线性递归关系,其特征多项式为(自行百度):

x^2 - 1/a * x + (1-a)/a = 0

求出特征方程的根为1和r=(1-a)/a,针对a==1/2的情况需要特殊处理。得到公式的通解为:

P(N|h) = A*(1^h) + B*(r^h)

根据已知条件P(N|N)=1、P(N|0)=0得:

1 = A + B*(r^N)

0 = A + B

A = -1/(r^N - 1)、B = 1/(r^N - 1)

得到最终解 P(N|h) = (r^h - 1)/(r^N - 1)

但是当a==1/2时,特征方程有重根,因此这种情况下通解为

P(N|h) = A+B*h

A = 0、B=1/N

即 P(N|h) = h/N

再来看topcoder srm 667 div1 500的题

Problem Statement

 

There are N cats sitting around a circle. The cats are numbered 0 throughN-1 in clockwise order. Note that as they sit around a circle, catN-1 is adjacent to cat 0. The cats are playing a game and the winner will get a prize!

The game looks as follows:

  • There is a single ball. Initially, cat 0 holds the ball.
  • In each round of the game, the cat who currently holds a ball flips a biased coin. The coin will come up heads with probabilityp/1,000,000,000 and tails with probability 1-(p/1,000,000,000).
  • If the coin came up heads, the current cat will hand the ball to the next cat clockwise, otherwise the current cat will hand the ball to the next cat counterclockwise. Formally, if the current cat is cat j, heads means that the ball goes to cat (j+1) modN and tails means that it goes to cat (j-1) mod N.
  • The game is played until each cat held the ball at least once. The cat who holds the ball at the end of the game is the winner.

In other words, the winner is the last cat to touch the ball. Note that cat 0 holds the ball at the beginning, and this does count as holding the ball. Hence, if there is more than one cat, cat 0 can never win the game.

Cat K wonders what is the probability that she will win the prize. You are given the intsN,K, and p. Return the probability that catK wins.

Definition

 
Class: CatsOnTheCircle
Method: getProb
Parameters: int, int, int
Returns: double
Method signature: double getProb(int N, int K, int p)
(be sure your method is public)

Limits

 
Time limit (s): 2.000
Memory limit (MB): 256
Stack limit (MB): 256

Notes

- Your return value must have an absolute or relative error smaller than or equal to 1e-6

Constraints

- N will be between 3 and 1,000,000,000, inclusive.
- K will be between 1 and N-1, inclusive.
- p will be between 1 and 999,999,999, inclusive.

Examples

0)  
 
3
1
300000000
Returns: 0.6999999999999985
This game has N=3 cats, labeled 0, 1, 2. We havep=30,000,000, hence the coin will come up heads with probability 30,000,000/1,000,000,000 = 0.3 and tails with probability 0.7. The game can look as follows:

  1. Cat 0 is given the ball.
  2. Cat 0 flips the coin. The coin comes up tails.
  3. Cat 0 hands the ball to cat (0-1) mod 3 = cat 2.
  4. Cat 2 flips the coin. The comes up tails again.
  5. Cat 2 hands the ball to cat (2-1) mod 3 = cat 1.
  6. At this moment, each cat has held the ball. The game ends and cat 1 gets the prize.

This particular sequence of events has probability 0.7*0.7 of occuring. It can be shown that the probability that cat 1 wins the game is 0.7.

1)  
 
6
2
500000000
Returns: 0.2
The coin that is flipped will come up heads with probability 1/2, and tails with probability 1/2.
2)  
 
6
5
500000000
Returns: 0.2
3)  
 
10
2
666666666
Returns: 0.00391389439551009
4)  
 
999999999
999999996
777777777
Returns: 0.05830903870125612
5)  
 
1000000000
4
300000000
Returns: 0.044981259448371
6)  
 
534428790
459947197
500000000
Returns: 1.871156682766205E-9

题意:

N只猫围成一圈玩游戏,顺时针编号0~N-1,N-1与0相邻。游戏规则如下:

、一开始编号0的猫拿着一个球

、每个回合中手里拿球的猫抛硬币,该硬币有P/1000000000的概率正面朝上,(1-P/1000000000)的概率反面朝上

、如果硬币正面朝上,则该猫 j 把球传给编号为(1+j)%N的猫,否则传给编号为(j-1+N)%N的猫

、该游戏持续进行直到每只猫至少拿到一次球。且最终拿球的猫赢得游戏

现在给定N K P,求出编号为K的猫赢得游戏的概率。

分析:

1. 如果最终猫K拿到球并结束游戏,那么之前一回合必然是猫K-1或K+1拿球,且除K外的猫都至少拿过一次球。则最终的结果为P(K+1,K-1) + P(K-1,K+1),既猫K+1先拿到球的前提下K-1拿到球的概率加上猫K-1先拿到球的前提下K+1拿到球的概率。这样就可以了,因为当全局只剩下K没有拿过球,K必然是最后一个拿到球的。

2. 这种情况和赌徒破产问题有什么类似之处呢?再来回顾下赌徒破产问题,该问题求的是当前有h枚金币的情况下,赢得N枚金币的概率。不如我们换一种表述方式,即该赌徒一开始最多能连续输掉h枚金币。放到这题的环境中,我们假设顺时针走等于金币加一,逆时针走等于金币减一。

3. 以求解P(K-1,K+1)为例,需要将其拆分为两种概率的乘积:P(a)=从0出发向左走最多到达K+2,且向右走必然到达K-1;P(b)=从K-1出发向右最多到达K-1,且向左走必然到达K+1;这样一来就可以套赌徒破产问题了。

4. 大于1.0的浮点数求幂可能会爆,需要控制一下

总结:

概率真是tm的神奇

#include <cstdio>
#include <iostream>
#include <string>
#include<assert.h>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <set>
typedef long long int ll;
#define rp(i,b) for(int i=(0),__tzg_##i=(b);i<__tzg_##i;++i)
#define rep(i,a,b) for(int i=(a),__tzg_##i=(b);i<__tzg_##i;++i)
#define repd(i,a,b) for(int i=(a),__tzg_##i=(b);i<=__tzg_##i;++i)
#define mst(a,b) memset(a,b,sizeof(a))
using namespace std;
const double Denominator = 1e9;
const double eps = 1/Denominator;
struct CatsOnTheCircle {double gamblers_ruin(int n, int h, double p) {double q = 1.0-p;if (fabs(p-q) < eps)return 1.0*h/n;if (q > p)return 1-gamblers_ruin(n, n-h, q);double r = q/p;return (pow(r,h)-1)/(pow(r,n)-1);}double getProb(int N, int K, int _p){double p = _p/Denominator;double q = 1.0-p;double o = gamblers_ruin(N-2, N-K-1, p);double u = gamblers_ruin(N-2, K-1, q);return o*gamblers_ruin(N-1, 1, q) + u*gamblers_ruin(N-1, 1, p);}
};

Gambler's Ruin(赌徒破产问题 概率论)相关推荐

  1. matlab赌徒输完问题,Gambler's Ruin(赌徒破产问题 概率论)

    赌徒破产问题,做tc时遇到,顺便拿来好好研究下 问题如下: 一个赌徒有h枚金币,每次有概率a获得一枚金币或者概率(1-a)丢掉一枚金币,直到其所有的金币总数达到N或0则游戏结束,求赌徒最终赢得N枚金币 ...

  2. 从酒鬼失足到赌徒破产,悲剧收场为何注定

    很多看似不相关的事物,在背后却能有着相似的数学原理.它们之间的联系时常让人觉得诧异.比如酒鬼和赌徒背后就有着神奇的相同之处.本文从一个荒诞的酒鬼掉下悬崖的故事开始,最终讲述了在赌场里看上去违背常理的举 ...

  3. 【Wannafly挑战赛8】 D Alice和Bob赌糖果 【赌徒破产模型】

    链接:https://www.nowcoder.com/acm/contest/57/D 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  4. [Introduction to programming in Java 笔记] 1.3.8 Gambler's ruin simulation 赌徒破产模拟

    赌徒赢得机会有多大? public class Gambler {public static void main(String[] args){ // Run T experiments that s ...

  5. 赌徒破产理论(Gambler's Ruin)

    来自:https://www.jianshu.com/p/88abc4082745 在组队训练时,碰到了一题God of Gamblers,所以大致了解了一番,做个粗略总结记录一下. 定理 对于一个赌 ...

  6. matlab赌徒破产模型转移矩阵,[转载]【转】生成土地利用变化转移矩阵的方法

    这里是网上搜到的生成土地利用变化转移矩阵的几种方法,以飨来者: A 栅格数据做转移矩阵 "一般习惯列为早期的数据,行为近期的数据,就如你说的早期的在上,晚期的在下." B 矢量数据 ...

  7. 一些概率论所揭示的道理

    一些概率论所揭示的道理 嗯,今天想写一点不太严肃的东西.主要原因是近日的春光很美,不想学习. 在我最初接触神经网络的时候,忘记是哪个大牛抛出了这样一个观点,神经网络想要真的模仿到人类的智慧,就一定要抛 ...

  8. 赌徒输光 酒鬼回家 长期双方竞赛问题

    前言:仅个人小记. 一. 问题原型 赌徒手里有 x 元,每一局输的概率恒定为 p ,请问赌徒最终输光的概率? 酒鬼徘徊(在坐标轴上左右移动)回家,目前酒鬼在坐标轴上 x 处,家在原点 0 处,请问酒鬼 ...

  9. 初等概率论期中复习(1)基本概念

    本文为小姚的初等概率论复习资料,内容节选自清华大学邓老师的初概授课内容,并穿插有自己的理解,希望能对大家有所帮助. 0.写在前面 参照去年的期中题目,我总结了一下.初概期中考试的考题形式主要有判断题. ...

最新文章

  1. JavaScript初学者编程题(19)
  2. java不四舍五入_JAVA-四舍五入之坑
  3. 推荐5个免费项目管理工具
  4. [转] 值得推荐的C/C++框架和库
  5. IBM X60/X61无光驱安装XP
  6. maven学习笔记第一节一-maven install 模块之间相互引用
  7. jq实现文字个数限制_限制字符输入数功能(jquery版和原生JS版)
  8. DBeaverEE for Mac(数据库管理工具)v21.2.1中文版
  9. Celery使用数据库代替rabbitmq
  10. yarn 安装使用小记
  11. 自定义MVC项目02
  12. 77. 组合 - 递归
  13. 自然语言理解和自然语言处理_4种自然语言处理和理解的方法
  14. linux、ubuntu如何查看网速
  15. 什么是POSIX system
  16. Python计算机视觉(中英文版本)pdf+源代码
  17. 多商家父订单子订单_70多份订单被退回,商家查看信息傻眼了,美团:封店180天...
  18. ITパスポート5天学习笔记④_Rx
  19. svg图片在vue项目中的应用
  20. c语言编程彩票中奖30选7,中国福利彩票30选7(七乐彩)中奖号码表

热门文章

  1. 易连云打印机PHP接口
  2. Spring系列:2021年Java春招面试经历,内容太过真实
  3. Data too long for column ‘xxx‘ at row 1 ——数据库字段长度太短
  4. 用Git上传代码到华为云(图解详细)
  5. 【内推】阿里云 云原生团队 2022 届秋招
  6. extjs6 清除grid中combo列的值
  7. 查看计算机.net环境版本,电脑怎么查看.NET Framework版本号?
  8. 2018 SUCTF招新赛
  9. 灰色预测方法预测温度matlab,灰色预测matlab代码怎么写
  10. 2021-06-22ctf学习wp模板