概率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. Objective-c 类实现 (@implementation)

    在用@interface声明类之后,可以使用@implementation进行实类的实现.类的实现的具体语法如下: @implementation 类名 方法实现代码; @end; 实例: @impl ...

  2. android 中文 api (72) —— BluetoothSocket[蓝牙]

    前言 本章内容是   android.bluetooth.BluetoothSocket,为Android蓝牙部分的章节翻译.蓝牙通讯套接字,代表了与远端设备的连接点,使用socket本地程序可以通过 ...

  3. CSS的position(位置)

    position: 位置,absolute绝对位置,相对于浏览器边界的位置:relative相对位置,相对于它本应该出现的位置.fixed:固定位置,它不会随着滚动. 设置好position之后,就可 ...

  4. string.Format()字符串格式化

    Format()基本语法:     {索引[,对齐][:格式字符串]}     ·索引:表示引用的对象列表中的第n个对象参数.     ·对齐(可选):设置宽度与对齐方式,该参数为带符号的整数.正数为 ...

  5. KMP算法解析

    介绍一种高效的KMP算法:代码可以直接运行 #include <iostream> #include <iomanip> using namespace std; void p ...

  6. [置顶] 关于CSDN2013博客之星的一些看法

    最近一个周,最火的话题当然要数CSDN2013博客之星拉票了. 实话实说,从12月14日开始,我连续5天拉票. 通过QQ群.QQ好友.CSDN文章.给CSDN粉丝发私信等多种方式拉票,真是累死我了. ...

  7. PLSQL developer登录身份证明检索失败的解决办法

    全都在一个下图中:

  8. JQuery标签去重与数组去重

    如图所示: 代码: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head ...

  9. 【转载】国内网站博客数据统计选免费Google Analytics还是百度统计

    [转载]国内网站博客数据统计选免费Google Analytics还是百度统计 Google Analytics谷歌统计是我用的第一个网站统计工具,当然现在也一直在用.Google Analytics ...

  10. QLabel设置行间距(使用html的语法,比较巧妙)

    1.设置行间距 QLabel没有设置行间距的函数,所以这种办法是行不通的.只能采用其它类似的方法来实现,例如设置行高,使用样式代码如下: <p style='line-height:18px'& ...