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;
    }
    

      

  • 相关阅读:
    Syntax error, insert "]" to complete MemberExpression XXX.js (Java Web Project 导入Jquery的文件后报错)
    Unicode 转换成 Ascii (把Unicode 中文字符串输入到文本中)
    Static控件响应鼠标事件
    在Window 7 64位操作系统上安装Oracle 10g 及 配置PLSQL Developer 8.0.4图解
    LPSTR LPCSTR LPWSTR LPCWSTR区别
    C++ Builder 全部API函数列表
    CPropertySheet标签页 实现各个CPropertyPage页面之间的切换
    C#中如何从字符串中提取数字
    如何用SQL统计某个字符在一个字符串中出现的次数
    MVC中--异常详细信息: System.ArgumentNullException: 值不能为 null。 参数名: value
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8597145.html
Copyright © 2011-2022 走看看