概率dp...

http://blog.csdn.net/Vmurder/article/details/46467899 ( from : [辗转山河弋流歌 by 空灰冰魂] )

这个讲得很好 , 推推公式就可以 O( n ) , 但是 n 最大是99999999 , 怎么破....其实 n 很大时概率基本不动了...所以只需计算到某一个较大值时就可以停下来了...

-------------------------------------------------------------------------------------

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
 
#define clr( x , c ) memset( x , c , sizeof( x ) )
#define rep( i , n ) for( int i = 0 ; i < n ; ++i )
#define f( i ) ( p * g[ i - 1 ] + ( 1 - p ) * q * f[ i - 1 ] ) / ( 1 - ( 1 - p ) * ( 1 - q ) )
#define g( i ) ( q * f[ i - 1 ] + ( 1 - q ) * p * g[ i - 1 ] ) / ( 1 - ( 1 - p ) * ( 1 - q ) )
 
using namespace std;
 
const int maxn = 1005;
 
double f[ maxn ] , g[ maxn ]; // f : A first
 
int main() {
freopen( "test.in" , "r" , stdin );
int t , n;
double p , q;
for( scanf( "%d" ,&t ) ; t-- ; ) {
scanf( "%d%lf%lf" , &n , &p , &q );
n = min( n , 1000 );
f[ 0 ] = 0 , g[ 0 ] = 1;
for( int i = 1 ; i <= n ; i++ ) {
if( f[ i - 1 ] > g[ i - 1 ] ) 
   p = 1 - p , q = 1 - q;
f[ i ] = f( i );
g[ i ] = g( i );
if( f[ i - 1 ] > g[ i - 1 ] )
   p = 1 - p , q = 1 - q;
}
printf( "%.6lf\n" , f[ n ] );
}
    return 0; 

-------------------------------------------------------------------------------------

2318: Spoj4060 game with probability Problem

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 178  Solved: 66
[Submit][Status][Discuss]

Description

Alice和Bob在玩一个游戏。有n个石子在这里,Alice和Bob轮流投掷硬币,如果正面朝上,则从n个石子中取出一个石子,否则不做任何事。取到最后一颗石子的人胜利。Alice在投掷硬币时有p的概率投掷出他想投的一面,同样,Bob有q的概率投掷出他相投的一面。

现在Alice先手投掷硬币,假设他们都想赢得游戏,问你Alice胜利的概率为多少。

Input

第一行一个正整数t,表示数据组数。

对于每组数据,一行三个数n,p,q。

Output

对于每组数据输出一行一个实数,表示Alice胜利的概率,保留6位小数。

Sample Input

1

1 0.5 0.5

Sample Output

0.666667

HINT

数据范围:

1<=t<=50

0.5<=p,q<=0.99999999

对于100%的数据 1<=n<=99999999

Source

BZOJ 2318: Spoj4060 game with probability Problem( 概率dp )的更多相关文章

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

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

  2. Bzoj 2318 Spoj4060 game with probability Problem

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

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

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

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

  5. 2318: Spoj4060 game with probability Problem

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

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

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

  7. 【BZOJ2318】【spoj4060】game with probability Problem 概率DP

    链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...

  8. BZOJ2318: Spoj4060 game with probability Problem

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

  9. 【bzoj2318】Spoj4060 game with probability Problem

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

随机推荐

  1. xen虚拟机操作整理

    1,登陆物理机器 2,查看物理机建立虚拟机的列表 root:~ # xm li Name ID Mem VCPUs State Time(s) Domain-0 0 49450 8 r----- 52 ...

  2. [置顶] Asp.Net底层原理(二、写自己的Asp.Net框架)

    我们介绍过了浏览器和服务器之间的交互过程,接下来介绍Asp.net处理动态请求. 写自己的Asp.Net框架,我们不会引用System.Web这个程序集,我们只需要创建要给自己的类库,所以在接下来的程 ...

  3. 都div在所有li的html()值被设置&quot;哈哈&quot;,当点击设置&quot;我被点击&quot;,其余的还是不点击设置“哈哈”

    <1> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  4. HtmlUnit+Jsoup 解决爬虫无法解析执行javascript的问题

    本人最近在研究爬虫.作为一个新手.研究了些爬虫框架,发现所有开源的爬虫框架很多,功能也很齐全,但唯独遗憾的是,目前还没有发现那个爬虫对js完美的解释并执行.看了浅谈网络爬虫爬js动态加载网页(二)之后 ...

  5. 【ActiveMQ】设置自动重连

    <property name="brokerURL" value="tcp://localhost:61616"/> <property na ...

  6. BlogUI的使用

  7. Mac Yosemite下Android Studio环境问题集合

    1. java not found 在mac Yosemite下,因jre升级到1.8,导致Android Studio无法启动.报错:"JAVA not found". 解决方法 ...

  8. 关于RMAN的配置信息存储和控制文件的关系

    没有使用catalog时,rman中的所有配置信息都会记入在 控制文件中 控制文件中dump出来的信息: *********************************************** ...

  9. Python学习之路——函数

    一.Python2.X内置函数表: 注:以上为pyton2.X内置函数,官方网址:https://docs.python.org/2/library/functions.html 二.Python3. ...

  10. 高级UNIX环境编程5 标准IO库

    标准IO库都围绕流进进行的 <stdio.h><wchar.h> memccpy 一般用汇编写的 ftell/fseek/ftello/fseeko/fgetpos/fsetp ...