链接:

#include <stdio.h>
int main()
{
puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢");
puts("网址:blog.csdn.net/vmurder/article/details/46467899");
}

题解:

fi 表示剩 i 个石头、 A 先手的获胜概率。

gi 表示剩 i 个石头、 A 后手的获胜概率。

假设想选,对于 fi:

有 p 的概率进入 gi−1 ;有 1−p 的概率进入 gi

所以 fi=p∗gi−1+(1−p)∗gi

假设想选。对于 g(i):

有 q 的概率进入 fi−1 ;有 1−q 的概率进入 fi

所以 gi=q∗fi−1+(1−q)∗fi

整理得:

fi=p∗gi−1+(1−p)∗q∗fi−11−(1−p)∗(1−q)

gi=q∗fi−1+(1−q)∗p∗gi−11−(1−p)∗(1−q)

然后剩 i 个石头时A的想不想选的意愿与 fi−1、gi−1 的大小关系有关。

fi−1>gi−1 都不想选。

fi−1<gi−1 都想选。

然后对于不想选的情况。那么 p=1−p,q=1−q 即可了。

然而这样就没法用矩阵乘法了。

就须要黑科技,,当n非常大时,事实上概率已经基本不动了。。让n=min(n,1000)就好了Qwq。

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 1010
using namespace std;
double f[N],g[N],p,q; int main()
{
int i,T,n;
for(scanf("%d",&T);T--;)
{
scanf("%d",&n),n=min(n,1000);
scanf("%lf%lf",&p,&q);
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的更多相关文章

  1. BZOJ 2318: Spoj4060 game with probability Problem( 概率dp )

    概率dp... http://blog.csdn.net/Vmurder/article/details/46467899 ( from : [辗转山河弋流歌 by 空灰冰魂] ) 这个讲得很好 , ...

  2. BZOJ 2318: Spoj4060 game with probability Problem (概率dp)(博弈论)

    2318: Spoj4060 game with probability Problem Description Alice和Bob在玩一个游戏.有n个石子在这里,Alice和Bob轮流投掷硬币,如果 ...

  3. 【bzoj2318】Spoj4060 game with probability Problem 概率dp

    题目描述 Alice和Bob在玩一个游戏.有n个石子在这里,Alice和Bob轮流投掷硬币,如果正面朝上,则从n个石子中取出一个石子,否则不做任何事.取到最后一颗石子的人胜利.Alice在投掷硬币时有 ...

  4. 【BZOJ2318】Spoj4060 game with probability Problem 概率

    [BZOJ2318]Spoj4060 game with probability Problem Description Alice和Bob在玩一个游戏.有n个石子在这里,Alice和Bob轮流投掷硬 ...

  5. BZOJ2318: Spoj4060 game with probability Problem

    #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #i ...

  6. 嘴巴题8 BZOJ2318: Spoj4060 game with probability Problem

    Time Limit: 1 Sec Memory Limit: 128 MB Submit: 555 Solved: 273 [Submit][Status][Discuss] Description ...

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

  8. 2318: Spoj4060 game with probability Problem

    2318: Spoj4060 game with probability Problem Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 356  Sol ...

  9. Bzoj 2318 Spoj4060 game with probability Problem

    2318: Spoj4060 game with probability Problem Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 524  Sol ...

随机推荐

  1. Hibernate解决高并发问题之:悲观锁 VS 乐观锁

    高并发问题是程序设计所必须要解决的问题,解决此类问题最主要的途径就是对对程序进行加锁控制.hibernate对加锁机制同样做出了实现,常用加锁方式为悲观锁和乐观锁.悲观锁指的是对数据被外界(包括本系统 ...

  2. ↗☻【PHP与MySQL程序设计 #BOOK#】第3章 PHP基础

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  3. Java [leetcode 35]Search Insert Position

    题目描述: Given a sorted array and a target value, return the index if the target is found. If not, retu ...

  4. ccr test

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. jenkins+ant+ssh远程部署服务glassfish

    jenkins安装可以参考官网或自己百度,这里不再说明: jenkins版本2.19.2 这里先说一下目的:主要是通过jenkins实现glassfish的部署,源码使用的是svn,编译是使用ant, ...

  6. 【转】vnc centos

    原文:http://www.cnblogs.com/niocai/archive/2011/11/02/2233332.html 我的CentOS版本是6.0,下述方法在i386和x86_64中均适用 ...

  7. 深入浅出 JavaScript 变量、作用域和内存 v 0.5

    本文主要从原理入手分享变量和作用域的相关知识,最后结合本文所分享知识,再次深入了解下闭包的运行原理. 主要参考<JS高级程序设计> <JS权威指南> <高性能 JS> ...

  8. dataStructure@ Check whether a given graph is Bipartite or not

    Check whether a given graph is Bipartite or not A Bipartite Graph is a graph whose vertices can be d ...

  9. [NOI2005]维修数列 Splay tree 区间反转,修改,求和,求最值

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1500 Description Input 输入文件的第1行包含两个数N和M,N表示初始时数 ...

  10. CoreSeek中文检索引擎

    目的:安装coreseek中文检索引擎,配置MySQL数据库访问接口,使用PHP程序实现中文检索. CoreSeek官方网站: http://www.coreseek.cn/ http://www.c ...