【bzoj2318】Spoj4060 game with probability Problem 概率dp
题目描述
Alice和Bob在玩一个游戏。有n个石子在这里,Alice和Bob轮流投掷硬币,如果正面朝上,则从n个石子中取出一个石子,否则不做任何事。取到最后一颗石子的人胜利。Alice在投掷硬币时有p的概率投掷出他想投的一面,同样,Bob有q的概率投掷出他相投的一面。
现在Alice先手投掷硬币,假设他们都想赢得游戏,问你Alice胜利的概率为多少。
输入
第一行一个正整数t,表示数据组数。
对于每组数据,一行三个数n,p,q。
输出
对于每组数据输出一行一个实数,表示Alice胜利的概率,保留6位小数。
样例输入
1
1 0.5 0.5
样例输出
0.666667
提示
概率dp
这题真是巨坑。。。
f[i]表示i块石头先投者获胜的概率,g[i]表示i块石头后投者获胜的概率。
易推出:
$f[i]=\frac{p_0·g[i-1]+(1-p_0)·q_0·f[i-1]}{1-(1-p_0)·(1-q_0)}$
$g[i]=\frac{q_0·f[i-1]+(1-q_0)·p_0·g[i-1]}{1-(1-p_0)·(1-q_0)}$
然而这里$p_0$和$q_0$都是目标概率,而题目中的p和q都是几率,
所以需要根据情况决定是否想要正面朝上。
根据方程的推导:
A想让自己获胜的概率最大,即让$f[i]$最大。
假设$g[i-1]-f[i-1]$不等于$0$,把$f[i]$的推导式展开,得:
$f[i]=\frac{p_0·g[i-1]+(1-p_0)·q_0·f[i-1]}{1-(1-p_0)·(1-q_0)}\\\ \ \ \ \ \ =\frac{(p_0+q_0-p_0·q_0)·f[i-1]+p_0(g[i-1]-f[i-1])}{p_0+q_0-p_0·q_0}\\\ \ \ \ \ \ =f[i-1]+\frac{p_0(g[i-1]-f[i-1])}{p_0+q_0-p_0·q_0}\\\ \ \ \ \ \ =f[i-1]+\frac1{\frac{p_0+q_0-p_0·q_0}{p_0(g[i-1]-f[i-1])}}\\\ \ \ \ \ \ =f[i-1]+\frac1{\frac{1-q_0+\frac{q_0}{p_0}}{g[i-1]-f[i-1]}}$
显然当$g[i-1]-f[i-1]>0$时,$p_0$越大越好;当$g[i-1]-f[i-1]<0$时,$p_0$越小越好。
$q_0$的推导同理。
于是可以得到结论:
当f[i-1]<g[i-1]时,都想要正面朝上,$p_0=p$,$q_0=q$;
当f[i-1]>g[i-1]时,都不想要正面朝上,$p_0=1-p$,$q_0=1-q$。
但是n太大肿么办?
于是用到概率黑科技:
当n越来越大时,f[n]逐渐趋近于一个定值,而且题目中只要求保留6位小数。
所以就此题而言f[1000+k]可以近似等于f[1000]。
#include <cstdio>
#include <cstring>
double f[1001] , g[1001];
int main()
{
int t;
scanf("%d" , &t);
while(t -- )
{
int n , i;
double p , q;
scanf("%d%lf%lf" , &n , &p , &q);
memset(f , 0 , sizeof(f));
memset(g , 0 , sizeof(g));
if(n > 1000)
n = 1000;
f[0] = 0;
g[0] = 1;
for(i = 1 ; i <= n ; i ++ )
{
if(f[i - 1] > g[i - 1])
p = 1 - p , q = 1 - q;
f[i] = (p * g[i - 1] + (1 - p) * q * f[i - 1]) / (1 - (1 - p) * (1 - q));
g[i] = (q * f[i - 1] + (1 - q) * p * g[i - 1]) / (1 - (1 - p) * (1 - q));
if(f[i - 1] > g[i - 1])
p = 1 - p , q = 1 - q;
}
printf("%.6lf\n" , f[n]);
}
return 0;
}
【bzoj2318】Spoj4060 game with probability Problem 概率dp的更多相关文章
- BZOJ 2318: Spoj4060 game with probability Problem( 概率dp )
概率dp... http://blog.csdn.net/Vmurder/article/details/46467899 ( from : [辗转山河弋流歌 by 空灰冰魂] ) 这个讲得很好 , ...
- BZOJ 2318: Spoj4060 game with probability Problem (概率dp)(博弈论)
2318: Spoj4060 game with probability Problem Description Alice和Bob在玩一个游戏.有n个石子在这里,Alice和Bob轮流投掷硬币,如果 ...
- 【BZOJ2318】Spoj4060 game with probability Problem 概率
[BZOJ2318]Spoj4060 game with probability Problem Description Alice和Bob在玩一个游戏.有n个石子在这里,Alice和Bob轮流投掷硬 ...
- BZOJ2318: Spoj4060 game with probability Problem
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #i ...
- 【BZOJ2318】【spoj4060】game with probability Problem 概率DP
链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...
- 嘴巴题8 BZOJ2318: Spoj4060 game with probability Problem
Time Limit: 1 Sec Memory Limit: 128 MB Submit: 555 Solved: 273 [Submit][Status][Discuss] Description ...
- 【BZOJ 2318】 2318: Spoj4060 game with probability Problem(概率DP)
2318: Spoj4060 game with probability Problem Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 371 Sol ...
- 2318: Spoj4060 game with probability Problem
2318: Spoj4060 game with probability Problem Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 356 Sol ...
- Bzoj 2318 Spoj4060 game with probability Problem
2318: Spoj4060 game with probability Problem Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 524 Sol ...
随机推荐
- sql,lambda,linq语句
实例 Code 查询Student表的所有记录. select * from student Linq: from s in Students select s Lambda: Students.Se ...
- 深圳Uber优步司机奖励政策(12月28日到1月3日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 广州Uber优步司机奖励政策(1月4日~1月10日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- DATA 转 16 进制
// 转 16进制 编码 NSData *data = [NSData dataWithBytes:(const void *)dataOut length:(NSUInteger)dataOutMo ...
- Unbuntu安装RVM
apt-get install curl #安装rvm curl -L https://get.rvm.io | bash #执行启动 source /home/mafei/.rvm/scripts/ ...
- 2019年猪年海报PSD模板-第六部分
14套精美猪年海报,免费猪年海报,下载地址:百度网盘,https://pan.baidu.com/s/1WdlIiIdj1VVWxI4je0ebKw
- Linux命令应用大词典-第33章 X Window
33.1 xhost:X服务器的访问控制程序 33.2 xinit:X Window系统初始化 33.3 Xlsclients:在显示器中列出正在运行的客户端应用程序 33.4 xlsfonts:显示 ...
- jquery datatable 常用例子
在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够提高性能,达到事半功倍的效果,现对DataTable的使用技巧进行一下总结. 一.DataTable简 ...
- 小程序之web-view打开外部链接
小程序之web-view - 传送门 web-view 组件是一个可以用来承载网页的容器,会自动铺满整个小程序页面.个人类型与海外类型的小程序暂不支持使用. 一:小程序使用web-view打开链接的前 ...
- Python—列表(一个“打了激素”的数组)
我们在C语言中会使用数组来将一大堆数据类型一样的数据挨个摆在一起,但是数组有一个基本的要求,就是数据类型必须是一致的,我们知道Python的变量由于没有数据类型,也就是说Python没有数组这一概念, ...