首页 > 代码库 > UVa 12585 Poker End Games

UVa 12585 Poker End Games

题意:Alice和Bob这对狗男女又开始玩游戏了!!!!一开始前者有a元,后者有b元,每次玩,每个人赢得概率是对半开的,令c为a,b中的最小值,那么每次玩,谁输了就给赢的人c元,问Alice赢的概率和游戏的盘数期望值!

思路:貌似会迭代!公式也不好推。。。发现概率是0.5  然后误差范围是1e-5, 那么只要到达一定深度以后概率就可以忽略不计了!

    #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
#define REP(_,a,b) for(int _ = (a); _ <= (b); _++)
int a,b;
int gcd(int a,int b) {
    if(!b) return a;
    else return gcd(b,a%b);
}
double dfs(int x,int y,int dep) {
    if(dep > 30 || x==0) return 0.0;
    if(y==0) return 1;
    if(x > y) {
        return dfs(x-y,y*2,dep+1)/2+dfs(x+y,0,dep+1)/2;
    }else{
        return dfs(0,y+x,dep)/2+dfs(x+x,y-x,dep+1)/2;
    }
}
double dfs1(int x,int y,int dep) {
    if(dep > 30) return 1;
    if(x==0) return 0;
    if(y==0) return 0;
    if(x > y) {
        return 1+dfs1(x-y,y*2,dep+1)/2;
    }else{
        return 1+dfs1(x*2,y-x,dep+1)/2;
    }
}
int main(){
    int ncase,T=1;
    cin >> ncase;
    while(ncase--) {
        scanf("%d%d",&a,&b);
        if(a==b) {
            printf("Case %d: 1.000000 0.500000\n",T++);
        }else{
            int g = gcd(a,b);
            int t1 = a/g;
            int t2 = b/g;
            double p = dfs(t1,t2,0);
            double e = dfs1(t1,t2,1);
            printf("Case %d: %.6lf %.6lf\n",T++,e,p);
        }
    }
    return 0;
}


UVa 12585 Poker End Games