概率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. 【转】利用Ajax.BeginForm提交文件

    Ajax.BeginForm @using (Ajax.BeginForm("YourAction", "YourController", new AjaxOp ...

  2. ThinkPHP - 登录模块,核心代码

    /** * 登录成功 * @return [type] [description] */ public function checkLogin($data) { $user = M($this-> ...

  3. iOS开发之第三方登录微博-- 史上最全最新第三方登录微博方式实现

    相关资源地址: 本项目demo地址 :  https://github.com/zhonggaorong/weiboSDKDemo 最新SDK下载:  最新微博SDK 官网注册地址:点击打开链接 最新 ...

  4. C# Best Practices - Creating Good Properties

    Coding Properties Code in the Getter Check the user's credentials Check application state Format the ...

  5. (转)WINDOWS内核对象

    WINDOWS内核对象 原文地址:http://blog.csdn.net/misterliwei/article/details/976988  支持原创 一.前言 Windows中有很多像进程对象 ...

  6. 《Java TCP/IP Socket 编程 》读书笔记之十一:深入剖析socket——TCP套接字的生命周期

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/16113083 建立TCP连接      新的Socket实例创建后,就立即能用于发送和接收 ...

  7. webviewactivity

    WebView注意点,注释里有说明 package com.example.suneyaenews; import com.example.http.HttpThread; import androi ...

  8. Hadoop平台安装前准备

    集群配置 准备工作 1.  Iptables #chkconfig iptables –list #chkconfig iptables –level 3456off #service iptable ...

  9. asp.net 开发注意的几点

    WIN7中组件服务中的DCOM配置找不到Microsoft Excel应用程序的解决办法: 这主要是64位系统的问题,excel是32位的组件,所以在正常的系统组件服务里是看不到的 可以通过在运行里面 ...

  10. C# List 转Datatable

    最近在做Excel导出,看到了这个方法,虽不是自己写的,但值得收藏,但是忘记从那摘抄的,没写原文作者看到望见谅! #region 导出Excel /// <summary> /// lis ...