ACM-ICPC 2017 Asia Urumqi:A. Coins(DP) 组合数学

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)t(1≤t≤1000) which is the total number of cases.

For each case, a line contains three space-separated integers nn, m (1 \le n, m \le 100)m(1≤n,m≤100)and k (1 \le k \le n)k(1≤k≤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 33 digits.

样例输入

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

样例输出

0.500
1.250
3.479
3.000
5.500
5.000

题目来源

ACM-ICPC 2017 Asia Urumqi

题意:有n枚朝下的硬币,我们可以投掷这些硬币m次,每次投掷 t 枚硬币,问最后朝上硬币的期望

分析:最优的策略一定是:当有至少 k 枚硬币面朝下时,则选 k 枚面朝下的硬币去抛掷(任意k 枚都可以);如果不足 k 枚面朝下,则在选择所有面朝下的硬币的基础上再额外选择若干面朝上的硬币。

于是有动态规划,记 dp[i][j]表示抛掷了 i 次后,有 j 枚硬币面朝上的概率。他们应该满足dp[i][0]+dp[i][1]+...+dp[i][n]=1。转移时,考虑从当前状态(i,j)出发,抛掷的 k 枚硬币的所有可能结果:分别有 0~k 枚面朝上。其中 k 枚硬币抛掷后有 l 枚面朝上的概率为 C(k,l)/2k。时间复杂度 O(nmk)。

参考博客:https://blog.csdn.net/mitsuha_/article/details/79307065

AC代码:

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 205;
const ll mod = 1e9 + 7;
double dp[maxn][maxn], p[maxn], c[maxn][maxn];
int main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    c[0][0] = 1;
    for( ll i = 1; i <= 100; i ++ ) {
        c[i][0] = 1;
        for( ll j = 1; j <= i; j ++ ) {
            c[i][j] = c[i-1][j-1] + c[i-1][j];  //打表前一百个的组合数
        }
    }
    p[0] = 1;
    for( ll i = 1; i <= 100; i ++ ) {
        p[i] = p[i-1]/2;   //几枚硬币朝上的概率
    }
    ll T;
    cin >> T;
    while( T -- ) {
        ll n, m, t;
        cin >> n >> m >> t;
        memset( dp, 0, sizeof(dp) );
        dp[0][0] = 1;   //记录投掷i次有j枚硬币朝上的概率
        for( ll i = 0; i < m; i ++ ) {
            for( ll j = 0; j <= n; j ++ ) {
                if( dp[i][j] == 0 ) {
                    continue;
                }
                for( ll k = 0; k <= t; k ++ ) {
                    if( n-j >= t ) {   //还有硬币没有朝上的情况
                        dp[i+1][j+k] += dp[i][j]*c[t][k]*p[t];
                    } else {   //已经有n枚硬币朝上了还得投掷的情况,这时会使n枚变少或者不变
                        dp[i+1][n-t+k] += dp[i][j]*c[t][k]*p[t];  //n-t代表会改变t枚硬币的情况,k代表改变的情况朝上的情况
                    }
                }
            }
        }
        double ans = 0;
        for( ll i = 1; i <= n; i ++ ) {
            ans += dp[m][i]*i;       //计算期望
        }
        printf("%.3lf\n",ans);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/l609929321/p/9387663.html

时间: 2024-08-05 03:54:27

ACM-ICPC 2017 Asia Urumqi:A. Coins(DP) 组合数学的相关文章

ACM-ICPC 2017 Asia Urumqi A. Coins

Alice and Bob are playing a simple game. They line up a row of n identical coins, all with the heads facing down onto the table and the tails upward. For exactly mm times they select any k of the coins and toss them into the air, replacing each of th

ACM-ICPC 2017 Asia Urumqi A. Coins【期望dp】

题目链接:https://www.jisuanke.com/contest/2870?view=challenges 题目大意:给出n个都正面朝下的硬币,操作m次,每次都选取k枚硬币抛到空中,求操作m次后,硬币向上的期望值. 思路: 1.期望跟概率还是有点不同的,期望要枚举出抛的所有的情况,然后求sigma(i * dp[][]) 2.dp[i][j]表示进行i次操作后,有j枚硬币向上的概率.这样就可以求最后的硬币向上的期望了. 3.值得注意的是,预处理的组合数要开 double 型. 代码:

2017 ICPC Asia Urumqi A.coins (概率DP + 期望)

题目链接:Coins 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

ACM-ICPC 2017 Asia Urumqi(第八场)

A. Coins Alice and Bob are playing a simple game. They line up a row of nnn identical coins, all with the heads facing down onto the table and the tails upward. For exactly mmm times they select any kkk of the coins and toss them into the air, replac

zoj 3662 第37届ACM/ICPC长春赛区H题(DP)

题目:给出K个数,使得这K个数的和为N,LCM为M,问有多少种 f[i][j][k]表示选i个数,总和为j,最小公倍数为k memery卡的比较紧,注意不要开太大,按照题目数据开 这种类型的dp也是第一次做 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue&g

UPC5431/acm icpc 2017 Tehran Column Addition

题目链接:http://exam.upc.edu.cn/problem.php?cid=1326&pid=7 题意:给你一个可能存在错误的加法等式,问最少删除多少列能使等式成立. eg: 思考:如果某一列已经成立,如上图的1+4=5,他一定可以加到前面最长的成立的等式上,类似于最长上升子序列,不过要n^2扫. 做题时WA了几发,因为没考虑到等式的最高位不能有进位,以及可能某一列一开始没有进位,但是后来由于前面低位的进位导致了自己的进位(这种情况只会出现在初始和为9,低位又进一的情况). 1 #i

hdu6206 Apple 2017 ACM/ICPC Asia Regional Qingdao Online

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6206 题目: Apple Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 530    Accepted Submission(s): 172 Problem Description Apple is Taotao's favouri

2016 ACM/ICPC Asia Regional Shenyang Online 1007/HDU 5898 数位dp

odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 388    Accepted Submission(s): 212 Problem Description For a number,if the length of continuous odd digits is even and the length

2016 ACM/ICPC Asia Regional Shenyang Online 1009/HDU 5900 区间dp

QSC and Master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 859    Accepted Submission(s): 325 Problem Description Every school has some legends, Northeastern University is the same. Enter