zoukankan      html  css  js  c++  java
  • Spoj 4060 A game with probability

    题目描述

    Alice and Bob play the following game. First, they collect N small stones and put them together in one pile. After that, they throw a coin one by one. Alice starts first. If a player throws heads then he takes exactly one stone from the pile. In case of tails he don't do anything. The one who takes the last stone wins. For each player, his skill of throwing a coin is known (to everyone, including himself and his opponent). More precisely, if Alice wants to throw some specific side of the coin, she always succeeds with probability P. The same probability for Bob is Q. You are to find probability that Alice will win the game if both guys play optimally.

    输入输出格式

    输入格式:

    Input starts with a line containing one integer T - a number of test cases (1 <= T <= 50). Then T test cases follow. Each of them is one line with three numbers N, P, and Q separated with a space (1 <= N <= 99999999, 0.5 <= P, Q <= 0.99999999). P and Q have not more than 8 digits after decimal point.

    输出格式:

    For each test case output one line with a probability that Alice will win the game. Your answer must be precise up to 10^-6.

    输入输出样例

    输入样例#1: 
    1
    1 0.5 0.5
    输出样例#1: 
    0.666666667



    设f[i]为 Alice 先手 Alice获胜的概率,g[i]为Bob 先手 Alice获胜的概率。
    又因为p,q>=0.5,所以他们选择最优策略获胜的概率是要大于选择不优策略的。
    显然Alice会让f[i]尽量大,而Bob会让g[i]尽量小,所以我们分类讨论一下转移,再消去后效性就可以做了。

    当然还有一个问题是,本题的n很大,这样直接dp转移对于一个询问是O(1)的,承受不了,怎么办???
    打表可以发现当p,q相同且n逐渐增大的时候,f[n]和g[n]的波动逐渐减小。
    也就是说,当n达到10^9级别的时候,把它设成10^5级别的就行了,两者之间的答案不可能差到 10^-6。



    #include<bits/stdc++.h>
    #define ll long long
    #define D double
    const int maxn=100005;
    using namespace std;
    D p,q,X,g[maxn],f[maxn];
    int T,n,m;
    
    inline void solve(){
    	g[0]=1,f[0]=0;
    	for(int i=1;i<=n;i++)
    		if(g[i-1]>=f[i-1]){
    			X=p+q-p*q;
    			f[i]=p*g[i-1]+(1-p)*q*f[i-1];
    			g[i]=q*f[i-1]+(1-q)*p*g[i-1];
    			f[i]/=X,g[i]/=X;
    		}
    		else{
    			X=1-p*q;
    			f[i]=p*(1-q)*f[i-1]+(1-p)*g[i-1];
    			g[i]=q*(1-p)*g[i-1]+(1-q)*f[i-1];
    			f[i]/=X,g[i]/=X;
    		}
    	printf("%.11lf
    ",f[n]);
    }
    
    int main(){
    	scanf("%d",&T);
    	while(T--){
    		scanf("%d%lf%lf",&n,&p,&q);
    		n=min(n,100000),solve();
    	}
    	
    	return 0;
    }
    

      

  • 相关阅读:
    【转】Visual Studio 2008中使用科学计算库GSL
    【转】[Python Tip]如何在Windows下方便地进入命令行运行程序
    【转】URL Encoding (URL转义字符)
    RDLC报表,纯文字内容,动态数据源 解决方案
    C# 数值计算
    标准库List使用注意
    VS2008 运行出现 “无法启动该程序 计算机中丢失 MSVCR90D.dll”
    数据结构在游戏中的简单应用(转)
    SQL SERVER 2005 四种排序函数
    2010.11.30
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8597145.html
Copyright © 2011-2022 走看看