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

题意

桌上放置着 \(n\) 个反面朝上的硬币,有 \(m\) 此操作,每次选择任意 \(k\) 个硬币抛向空中,每个硬币落到桌子后正面朝上和反面朝上的概率相同,求最终正面朝上的硬币的期望。

题解

概率DP 期望

期望 = 概率 * 总数

\(f(i, j)\) 表示为抛 \(i\) 枚硬币 \(j\) 枚硬币朝上的概率。则有 \(f(i, j)= 0.5 * f(i - 1, j) + 0.5 * f(i - 1, j - 1)\),其中 \(f(i, 0) = 2 ^ i\)。

\(DP(i, j)\) 表示第 \(i\) 次操作后有 \(j\) 枚正面朝上的硬币的概率,则反面硬币的个数为 \(n - j\)。

如果 \(n - j >= k\),那么只要在反面朝上的硬币中选择 \(k\) 枚抛即可。抛完 \(k\) 枚硬币后有 \(0 \sim k\) 枚硬币可能会正面朝上,递推方程为 \(DP(i + 1, j + l) = \sum_{l = 0}^{k} DP(i, j) * f(k, l)\)。

如果 \(n - j < k\),那么除了要抛 \(n - j\) 枚反面朝上的硬币,还要选择 \(k - (n - j)\) 枚正面朝上的硬币,这样最后正面朝上的个数是本来正面就朝上的 \(j-(k-(n-j))\) 枚加上抛了之后朝上的 \(l\ (0\le l\le k)\) 枚,递推方程为 \(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;
}

2017 ICPC Asia Urumqi A.coins (概率DP + 期望)的更多相关文章

  1. luogu P6835 概率DP 期望

    luogu P6835 概率DP 期望 洛谷 P6835 原题链接 题意 n + 1个节点,第i个节点都有指向i + 1的一条单向路,现在给他们添加m条边,每条边都从一个节点指向小于等于自己的一个节点 ...

  2. 2017 ICPC乌鲁木齐 A Coins 概率dp

    Coins 题意:一开始所有n个硬币都是反面朝上的,每次必须拿k个来抛,抛的人足够聪明,问m次之后向上的硬币的期望. 首先说了这个足够聪明的意思,就是只要向反面的有k个就不会sb地去拿向正面的来抛,想 ...

  3. 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 ...

  4. atcoderI - Coins ( 概率DP)

    I - Coins Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement Let NN b ...

  5. HDU5985 Lucky Coins 概率dp

    题意:给你N种硬币,每种硬币有Si个,有Pi 概率朝上,每次抛所有硬币抛起,所有反面的拿掉,问每种硬币成为最后的lucky硬币的概率. 题解:都知道是概率dp,但是模拟赛时思路非常模糊,很纠结,dp[ ...

  6. Gym 101606F - Flipping Coins - [概率DP]

    题目链接:https://codeforc.es/gym/101606/problem/F 题解: 假设 $f[i][j]$ 表示抛 $i$ 次硬币,有 $j$ 个硬币正面朝上的概率. 所以只有两种挑 ...

  7. 概率dp+期望dp 题目列表(一)

    表示对概率和期望还不是很清楚定义. 目前暂时只知道概率正推,期望逆推,然后概率*某个数值=期望. 为什么期望是逆推的,例如你求到某一个点的概率我们可以求得,然后我们只要运用dp从1~n每次都加下去就好 ...

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

    题目链接:https://www.jisuanke.com/contest/2870?view=challenges 题目大意:给出n个都正面朝下的硬币,操作m次,每次都选取k枚硬币抛到空中,求操作m ...

  9. HDU.5985.Lucky Coins(概率DP)

    题目链接 \(Description\) 有n(n<=10)种硬币,已知每种硬币的数量和它抛一次正面朝上的概率pi.进行如下过程:每次抛一次所有硬币,将正面朝下的硬币去掉.重复该过程直到只剩一种 ...

随机推荐

  1. 关于最近练习PYTHON代码的一点心得

    做测试以来,一直想学习代码,以前也断断续续的学习过,不过都是练习一些基础语法,学习的是菜鸟教程,但是效果不大. 最近在练习CODEWAR里做练习题,慢慢强化自己对一些基本语法的理解,熟悉基本的内置函数 ...

  2. mysql与navicat应用

    下载安装配置 用法 1.连接本机数据库: 打开navicat选择连接---第一个mysql---在常规下自己明明连接名和密码----确定 我这边建立了本机测试库 2. 连接阿里云服务器上的mysql ...

  3. jumpserver3.0页面配置

    一.jumpserver的启动 1 2 Jumpserver的启动和重启 [root@test-vm001 install]# /opt/jumpserver/service.sh start/res ...

  4. datetime中strptime用法

    import datetime day20 = datetime.datetime.strptime('2020-01-01 0:0:0', '%Y-%m-%d %H:%M:%S')nowdate = ...

  5. [轉]Exploit The Linux Kernel NULL Pointer Dereference

    Exploit The Linux Kernel NULL Pointer Dereference Author: wztHome: http://hi.baidu.com/wzt85date: 20 ...

  6. hbuilder模拟器端口

    模拟器 | 端口 夜神安卓模拟器夜神安卓模拟器     62001 逍遥安卓模拟器逍遥安卓模拟器     21503 BlueStacks(蓝叠安卓模拟器)BlueStacks(蓝叠安卓模拟器)    ...

  7. 从零开始搭建系统2.8——HDFS安装及配置

    从零开始搭建系统2.8——HDFS安装及配置

  8. 笔记61 Spring Boot快速入门(一)

    IDEA+Spring Boot快速搭建 一.IDEA创建项目 略 项目创建成功后在resources包下,属性文件application.properties中,把数据库连接属性加上,同时可以设置服 ...

  9. Nginx---配置系统(自己总结)

    1.Nginx配置系统 Nginx的配置系统   由  一个主配置文件  和  其他一些辅助的配置文件  构成: 这些文件均为纯文本文件,全部位于nginx安装目录下的conf目录下: Nginx配置 ...

  10. loadrunner自定义函数

    https://zhangfy068.iteye.com/blog/1614794 Loadruner 有四种实现自定义函数的方式,根据脚本编写方便性进行选择不同的方式. (1)直接引用法: Acti ...