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 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 + 期望)的更多相关文章
- luogu P6835 概率DP 期望
luogu P6835 概率DP 期望 洛谷 P6835 原题链接 题意 n + 1个节点,第i个节点都有指向i + 1的一条单向路,现在给他们添加m条边,每条边都从一个节点指向小于等于自己的一个节点 ...
- 2017 ICPC乌鲁木齐 A Coins 概率dp
Coins 题意:一开始所有n个硬币都是反面朝上的,每次必须拿k个来抛,抛的人足够聪明,问m次之后向上的硬币的期望. 首先说了这个足够聪明的意思,就是只要向反面的有k个就不会sb地去拿向正面的来抛,想 ...
- 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 ...
- atcoderI - Coins ( 概率DP)
I - Coins Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement Let NN b ...
- HDU5985 Lucky Coins 概率dp
题意:给你N种硬币,每种硬币有Si个,有Pi 概率朝上,每次抛所有硬币抛起,所有反面的拿掉,问每种硬币成为最后的lucky硬币的概率. 题解:都知道是概率dp,但是模拟赛时思路非常模糊,很纠结,dp[ ...
- Gym 101606F - Flipping Coins - [概率DP]
题目链接:https://codeforc.es/gym/101606/problem/F 题解: 假设 $f[i][j]$ 表示抛 $i$ 次硬币,有 $j$ 个硬币正面朝上的概率. 所以只有两种挑 ...
- 概率dp+期望dp 题目列表(一)
表示对概率和期望还不是很清楚定义. 目前暂时只知道概率正推,期望逆推,然后概率*某个数值=期望. 为什么期望是逆推的,例如你求到某一个点的概率我们可以求得,然后我们只要运用dp从1~n每次都加下去就好 ...
- ACM-ICPC 2017 Asia Urumqi A. Coins【期望dp】
题目链接:https://www.jisuanke.com/contest/2870?view=challenges 题目大意:给出n个都正面朝下的硬币,操作m次,每次都选取k枚硬币抛到空中,求操作m ...
- HDU.5985.Lucky Coins(概率DP)
题目链接 \(Description\) 有n(n<=10)种硬币,已知每种硬币的数量和它抛一次正面朝上的概率pi.进行如下过程:每次抛一次所有硬币,将正面朝下的硬币去掉.重复该过程直到只剩一种 ...
随机推荐
- 使用函数指针模拟C++多态
#include <iostream> using namespace std; class Base { public : void display() { cout << ...
- shell变量相关知识
环境变量和普通变量 一.几个常用命令: 1. set : 输出所有变量,包含全局变量和局部变量 2. env : 只显示全局变量 3. declare : 输出所有的变量,函数,整数和已经导出的变量 ...
- 转-C++之string判断字符串是否包含某个子串
转自:https://blog.csdn.net/zhouxinxin0202/article/details/77862615/ 1.string类函数find C++的string类提供了字符串中 ...
- main()和代码块
main方法 * main()方法的使用说明 * main方法是程序的主入口(一个主程序 先从main开始进行执行) * * * main方法也可以是一个普通的静态方法 代码块 代码块也是类的成员变量 ...
- Windows环境下Oracle数据库的自动备份脚本自动删除30天前的备份
@echo off echo ================================================ echo Windows环境下Oracle数据库的自动备份脚本 echo ...
- 发送验证码倒计时60s
var wait=60; function time(o) { if (wait == 0) { o.removeClass("gray"); o.text("发送验证码 ...
- 2015 ACM-ICPC 亚洲区上海站 A - An Easy Physics Problem (计算几何)
题目链接:HDU 5572 Problem Description On an infinite smooth table, there's a big round fixed cylinder an ...
- upc组队赛5 Ground Defense【枚举】
Ground Defense 题目描述 You are a denizen of Linetopia, whose n major cities happen to be equally spaced ...
- Promise篇
Promise 原理解析与实现(遵循Promise/A+规范) 1 什么是Promise? Promise是JS异步编程中的重要概念,异步抽象处理对象,是目前比较流行Javascript异步编程解 ...
- Vue学习之路之登录注册实例代码
Vue学习之路之登录注册实例代码:https://www.jb51.net/article/118003.htm vue项目中路由验证和相应拦截的使用:https://blog.csdn.net/wa ...