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也是第 ...
 
随机推荐
- 关于程序null值的见解
			
今天遇到了一个问题,查询一条数据,返回用list接,发现少了2个值(ssh框架).执行SQL少的这两个字段的值为null.上图说明一下: 可以看到第一次查询没有角标38.39的值. 是同一条SQL,第 ...
 - 多态、继承、this、super
			
先放一下多态的定义: (360词典上的哈) 多态(Polymorphism)按字面的意思就是"多种状态".在面向对象语言中,接口的多种不同的实现方式即为多态.引用Charlie C ...
 - Spark 系列(九)—— Spark SQL 之 Structured API
			
一.创建DataFrame和Dataset 1.1 创建DataFrame Spark 中所有功能的入口点是 SparkSession,可以使用 SparkSession.builder() 创建.创 ...
 - POI通用导出Excel数据(包括样式设计)
			
前言 前一段时间我写过通用的导入Excel,前几天也写了导出pdf格式的,还有我之前搞得导出Word,我在之前的博客也都介绍了导出和导入是一个道理,无非是一个获取一个是赋值.昨天有一位同仁看了我的Ex ...
 - fatal: remote origin already exists.解决方法
			
git remote add origin1 http://github.com/xxx/xxx.git origin名字冲突,换一个名字 遇到这种问题时表示已经有一个origin,冲突了,可能原因是 ...
 - Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解
			
如需转载,请注明出处:Flutter学习笔记(15)--MaterialApp应用组件及routes路由详解 最近一段时间生病了,整天往医院跑,也没状态学东西了,现在是好了不少了,也该继续学习啦!!! ...
 - Spark 系列(十一)—— Spark SQL 聚合函数 Aggregations
			
一.简单聚合 1.1 数据准备 // 需要导入 spark sql 内置的函数包 import org.apache.spark.sql.functions._ val spark = SparkSe ...
 - 8.15 day33 进程池与线程池_协程_IO模型(了解)
			
进程池和线程池 开进程开线程都需要消耗资源,只不过两者比较的情况线程消耗的资源比较少 在计算机能够承受范围之内最大限度的利用计算机 什么是池?  在保证计算机硬件安全的情况下最大限度地利用计算机  ...
 - grep文本搜索工具详解
			
############grep命令############这个命令属于文本处理三大命令之一,强大的文本搜索工具(贪婪模式)全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达 ...
 - ZooKeeper实现生产-消费者队列
			
[欢迎关注公众号:程序猿讲故事 (codestory),及时接收最新文章] 生产-消费者队列,用于多节点的分布式数据结构,生产和消费数据.生产者创建一个数据对象,并放到队列中:消费者从队列中取出一个数 ...