2017南宁现场赛E The Champion

Bob is attending a chess competition. Now the competition is in the knockout phase. There are 2^r2r players now, and they will play over rr rounds.

In each knockout round, the remaining players would be divided into pairs, and the winner of each pair would advance to the next knockout round. Finally only one player would remain and be declared the champion.

Bob has already assigned all players in an order while he assigned himself to the k-th site. A better player is located at a site with a smaller number indicating a higher order. The probability that a player with higher order wins a player with lower order is p (0 \le p \le 1)p(0p1).

Bob notices that arrangement of matches is crucial for the final result.

For example, if there are 44 players and Bob is the second best player (he is the second site), and p = 0.9p=0.9. In the first round, if Bob meets the best player, he will have only 0.1 \times 0.9 = 0.090.1×0.9=0.09 probability to become the champion. However if he does not meet the best player in the first round, he will have 0.9 \times (0.9 \times 0.1 + 0.1 \times 0.9) = 0.1620.9×(0.9×0.1+0.1×0.9)=0.162 probability to become the champion. Now Bob wants to know, what is the winning probability for him in the best arrangement of matches.

Input

The first line in the input contains an integer t (1 \le t \le 63000)t(1t63000) which is the number of test cases.

For each case, there is only one line containing two integers rr and k(1 \le r < 64,1 \le k \le 2^r)(1r<64,1k2r) and a float-point number p (0 \le p \le 1)p(0p1) as described above.

Output

For each case, calculate the winning probability for Bob in the best arrangement. Output the probability with the precision of 66 digits.

样例输入

2
1 1 0.8
2 2 0.9

样例输出

0.800000
0.162000

题意:2^r个人打比赛,一共比r伦决出冠军,主角的实力排在第k位,并且对于所有人,打败比他弱的人概率是p,打败比他强的人概率是(1-p);主角要尽可能的提高获胜的概率,求这个概率即可
思路:如果当前这一轮还有比主角弱的人,主角选择和弱的人对决,若只剩比主角强的人,就只能和强的人比赛。dfs记得记忆化搜索。。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstring>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define EPS 1e-7
typedef unsigned long long ll;
const int N_MAX = 10000+10;
const int MOD = 1e10+7;
int r;
ll k, qiang, ruo, num;
map<pair<ll, ll>, double>mp;//记忆化搜素
double p;

double dfs(ll qiang,ll ruo) {
    if (qiang == 0 && ruo == 0)return 1;
    if (mp[make_pair(qiang, ruo)] != 0)return mp[make_pair(qiang, ruo)];
    ll nxt_qiang = qiang >> 1, nxt_ruo = ruo >> 1;
    if (ruo & 1) {//弱的为奇数个,说明强的为偶数个,只要主角和一个弱的打就行了
        return mp[make_pair(qiang, ruo)] =p*dfs(nxt_qiang, nxt_ruo);
    }
    else {//否则弱的是偶数个,强的奇数个
        if (ruo != 0) {//弱的个数不为0,此时主角还是选择和弱的打,但是总会有一个强的会多出来和弱的打,所以这两者谁赢谁输就要分两种情况
            return mp[make_pair(qiang, ruo)] = p*(p*dfs(nxt_qiang + 1, nxt_ruo - 1) + (1 - p)*dfs(nxt_qiang, nxt_ruo));
        }
        else {//没有弱的选手了,主角只能和强的打
            return mp[make_pair(qiang, 0)] = (1 - p)*dfs(nxt_qiang, 0);
        }
    }
}

int main() {
    int t; scanf("%d",&t);
    while (t--) {
        scanf("%d%lld%lf",&r,&k,&p);
        mp.clear();
        num = 1LL << r;
        qiang = k - 1, ruo = num - k;
        double res=dfs(qiang, ruo);
        printf("%.6f\n",res);
    }
    return 0;
}

 

posted on 2018-10-09 20:56  ZefengYao  阅读(617)  评论(0编辑  收藏  举报

导航