2017 ICPC Asia Urumqi A.coins (desired probability DP +)

Topic links: Coins is

Description

Alice and Bob are playing a simple game. They line up a row of nn identical coins, all with the heads facing down onto the table and the tails upward.

For exactly mm times they select any kk of the coins and toss them into the air, replacing each of them either heads-up or heads-down with the same possibility. Their purpose is to gain as many coins heads-up as they can.

Input

The input has several test cases and the first line contains the integer \(t (1 \le t \le 1000)\) which is the total number of cases.

For each case, a line contains three space-separated integers \(n\), \(m (1 \le n, m \le 100)\) and \(k (1 \le k \le n)\).

Output

For each test case, output the expected number of coins heads-up which you could have at the end under the optimal strategy, as a real number with the precision of \(3\) digits.

Sample input

6
2 1 1
2 3 1
5 4 3
6 2 3
6 100 1
6 100 2

Sample output

0.500
1.250
3.479
3.000
5.500
5.000

Solution

The meaning of problems

The placing table \ (n-\) tails up coins, there \ (m \) this operation, each select any \ (K \) coins thrown into the air, after each of the coin fall face up tables and the probability of tails of the same coin seeking the ultimate face-up expectations.

answer

DP expected probability

* The total number of expected probability =

\ (f (i, j) \) is represented as throwing \ (i \) coins \ (j \) probability of gold coins up. There \ (F (I, J) = 0.5 * F (I -. 1, J) + 0.5 * F (I -. 1, J -. 1) \) , where \ (f (i, 0) = 2 ^ i \) .

\ (DP (i, j) \) represents the \ (I \) a rear operations \ (J \) number of pieces of the probability of the coin face up, then a coin is \ (n-- J \) .

If \ (n-- J> K = \) , as long as the tails of the coin selecting the \ (K \) pieces to throw. Parabolic End \ (K \) after the coins have \ (0 \ sim k \) coins may face up, the recurrence equation of \ (DP (i + 1, j + l) = \ sum_ {l = 0 the DP} ^ {K} (I, J) * F (K, L) \) .

If \ (the n-- J <k \) , then in addition to throw \ (n - j \) gold coin tails, also select \ (k - (n - j ) \) gold coins face up, so that the last number is the original face up on the front upward \ (j- (k- (nj) ) \) pieces plus the upward after throwing \ (l \ (0 \ le l \ le k) \) pieces, the recurrence equation of \ (DP (i + 1, j - (k - (n - j)) + l) = \ sum_ {l = 0} ^ {k} DP (i, j) * f (k, L) \) .

Code

#include <bits/stdc++.h>

using namespace std;
const int maxn = 110;
double dp[maxn][maxn];
double f[maxn][maxn];
int n, k, m;

void init() {
    f[0][0] = 1;
    for (int i = 1; i <= 100; ++i) {
        f[i][0] = pow(0.5, i);
        for (int j = 1; j <= 100; ++j) {
            f[i][j] = (f[i - 1][j] + f[i - 1][j - 1]) / 2.0;
        }
    }
}

int main() {
    init();
    int T;
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d%d", &n, &m, &k);
        memset(dp, 0, sizeof(dp));
        dp[0][0] = 1;
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j <= n; ++j) {
                for (int l = 0; l <= k; ++l) {
                    if (n - j >= k) {
                        dp[i + 1][j + l] += dp[i][j] * f[k][l];
                    } else {
                        dp[i + 1][j + l - (k - (n - j))] += dp[i][j] * f[k][l];
                    }
                }
            }
        }
        double ans = 0;
        for (int i = 1; i <= n; ++i) {
            ans += 1.0 * i * dp[m][i];
        }
        printf("%.3f\n", ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/wulitaotao/p/11332908.html