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
题目来源
题意:有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;
}
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 ...
- ACM-ICPC 2017 Asia Urumqi A. Coins【期望dp】
题目链接:https://www.jisuanke.com/contest/2870?view=challenges 题目大意:给出n个都正面朝下的硬币,操作m次,每次都选取k枚硬币抛到空中,求操作m ...
- ACM ICPC 2017 Warmup Contest 9 I
I. Older Brother Your older brother is an amateur mathematician with lots of experience. However, hi ...
- ACM ICPC 2017 Warmup Contest 9 L
L. Sticky Situation While on summer camp, you are playing a game of hide-and-seek in the forest. You ...
- ACM-ICPC 2017 Asia Urumqi G. The Mountain
All as we know, a mountain is a large landform that stretches above the surrounding land in a limite ...
- 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 c ...
- ACM-ICPC 2017 Asia Urumqi:A. Coins(DP)
挺不错的概率DP,看似基础,实则很考验扎实的功底 这题很明显是个DP,为什么???找规律或者算组合数这种概率,N不可能给的这么友善... 因为DP一般都要在支持N^2操作嘛. 稍微理解一下,这DP[i ...
- 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 wit ...
- zoj 3662 第37届ACM/ICPC长春赛区H题(DP)
题目:给出K个数,使得这K个数的和为N,LCM为M,问有多少种 f[i][j][k]表示选i个数,总和为j,最小公倍数为k memery卡的比较紧,注意不要开太大,按照题目数据开 这种类型的dp也是第 ...
随机推荐
- scroll-苹果滑动卡顿
2018年08月02日,程序小bug. 在移动端html中经常出现横向/纵向滚动的效果,但是在iPhone中滚动速度很慢,感觉不流畅,有种卡卡的感觉,但是在安卓设备上没有这种感觉; 一行代码搞定: - ...
- 在 alpine 中使用 NPOI
在 alpine 中使用 NPOI Intro 在 .net 中常使用 NPOI 来做 Excel 的导入导出,NPOI 从 2.4.0 版本开始支持 .netstandard2.0,对于.net c ...
- 中间件增强框架之-CaptureFramework框架
一.背景 应用服务监控是智能运维系统的重要组成部分.在UAV系统中,中间件增强框架(MOF)探针提供了应用画像及性能数据收集等功能,其中数据收集功能主要采集四类数据:实时数据.画像数据.调用链接数据生 ...
- Web前端开发——Ionic 3.0【爱创课堂专业前端培训】
前端开发——Ionic 3.0 一.Ionic 移动端有三种开发方向 源生APP开发, 移动端web开发 混合开发(介于以上两者之间的) 类微信小程序 reactNative,用react语法,开发a ...
- MapReduce 运行全过程解析
关注公众号,大家可以在公众号后台回复“博客园”,免费获得作者 Java 知识体系/面试必看资料. 前言 前面我们讲了 MapReduce 的编程模型,我们知道他主要分成两大阶段来完成一项任务,一是 m ...
- HiveQL DDL 常用QL示例资料
hive-version2.1.1 DDL操作 Create/Drop/Alter/Use Database 创建数据库 //官方指导 CREATE (DATABASE|SCHEMA) [IF NOT ...
- LeetCode :2.两数相加 解题报告及算法优化思路
题目连接:2.两数相加 题意 题目难度标为 中等, 因为题意上有一部分理解难度,以及需要数据结构的链表基础. 还不知道到链表的童鞋可以粗略的看下百度百科或者是翻出数据结构的书看一看,通俗一点的语言来解 ...
- Samba:基于公网 IP 的服务访问
写在前面的话 由于使用过程中,发现如果 Samba 只用于内网访问,同事在外面甚至其它不是一个网段的同事就无法访问了.这显然不符合我们最终的需求,最后没法,只能把访问部署到云服务器上面去,此时问题来了 ...
- eclipse解决properties文件中文乱码(两种方试)
第一种:大多数网上搜到的情况(不靠谱) 第一步:windows-->properties-->General-->Content Types-->text(如下图) 第二步:p ...
- 【Kubernetes 系列四】Kubernetes 实战:管理 Hello World 集群
目录 1. 创建集群 1.1. 安装 kubectl 1.1.1. 安装 kubectl 到 Linux 1.1.2. 安装 kubectl 到 macOS 1.1.3. 安装 kubectl 到 W ...