HDU 5816 Hearthstone (状压DP)
Hearthstone
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5816
Description
Hearthstone is an online collectible card game from Blizzard Entertainment. Strategies and luck are the most important factors in this game. When you suffer a desperate situation and your only hope depends on the top of the card deck, and you draw the only card to solve this dilemma. We call this "Shen Chou Gou" in Chinese.
Now you are asked to calculate the probability to become a "Shen Chou Gou" to kill your enemy in this turn. To simplify this problem, we assume that there are only two kinds of cards, and you don't need to consider the cost of the cards.
-A-Card: If the card deck contains less than two cards, draw all the cards from the card deck; otherwise, draw two cards from the top of the card deck.
-B-Card: Deal X damage to your enemy.
Note that different B-Cards may have different X values.
At the beginning, you have no cards in your hands. Your enemy has P Hit Points (HP). The card deck has N A-Cards and M B-Cards. The card deck has been shuffled randomly. At the beginning of your turn, you draw a card from the top of the card deck. You can use all the cards in your hands until you run out of it. Your task is to calculate the probability that you can win in this turn, i.e., can deal at least P damage to your enemy.
Input
The first line is the number of test cases T (T
Output
For each test case, output the probability as a reduced fraction (i.e., the greatest common divisor of the numerator and denominator is 1). If the answer is zero (one), you should output 0/1 (1/1) instead.
Sample Input
2
3 1 2
1 2
3 5 10
1 1 1 1 1 1 1 1 1 1
Sample Output
1/3
46/273
Source
2016 Multi-University Training Contest 7
##题意:
炉石游戏:此时对手有P点血,自己手上没牌,牌库中有n张奥术智慧(抽两张牌)和m张直伤牌(伤害不同),问自己的回合神抽打死对面的概率是多少.
##题解:
由于总牌数不超过20,所以很容易想到可以压缩状态.
dp[s]:已摸到牌的集合是s且能打死对面的方案数.
对于每个状态S,可以算出此时还能摸 A-B+1 张牌.
如果此时抓到的伤害牌已经够打死对面,那么就不需要往后拓展(直接计算后面卡牌的全排列即可).
否则要枚举S中的空位置,并用dp[s]去更新新状态.
结果:
所有能打死对面的组合S: dp[s] * (剩余卡牌的全排列).
官方题解:
这题其实有O(2^M)的做法. 方法用f[i][j]表示A类牌和B类牌分别抽到i张和j张,且抽牌结束前保证i>=j的方案数,这个数组可以用O(n^2)的dp预处理得到. 接下来枚举B类牌的每个子集,如果这个子集之和不小于P,用k表示子集的1的个数,将方案总数加上取到这个集合刚好A类卡片比B类卡片少一(过程结束)的方案数:f[k-1][k] * C(n, k - 1) * (k - 1)! * k! * (n + m – 2*k + 1)! . 如果子集包含了所有的B类卡片,则还需要再加上另一类取牌结束的情况,也就是取完所有牌,此时应加上的方案数为f[n][m] * n! * m! . 最后的总方案数除以(n+m)!就是答案.
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 501000
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;
LL gcd(LL a,LL b) {
return b==0? a:gcd(b,a%b);
}
LL dp[1<<21];
int dam[21];
LL fac[21];
int main(int argc, char const *argv[])
{
//IN;
fac[0] = 1;
for(int i=1; i<21; i++)
fac[i] = fac[i-1] * i;
int t; cin >> t;
int p, a,b;
while(scanf("%d %d %d", &p,&a,&b) != EOF)
{
int n = a + b;
memset(dp, 0, sizeof(dp));
for(int i=1; i<=b; i++)
scanf("%d", &dam[i]);
dp[0] = 1;
for(int s=0; s<(1<<n); s++) {
if(!dp[s]) continue;
int A = 0, B = 0, tol_dam = 0;
for(int i=0; i<b; i++) {
if(s & (1<<i)) {
B++; tol_dam += dam[i+1];
}
}
if(tol_dam >= p) continue;
for(int i=b; i<n; i++) {
if(s & (1<<i)) {
A++;
}
}
if(A - B + 1 <= 0) continue;
for(int i=0; i<n; i++) {
if(s & (1<<i)) continue;
dp[s | (1<<i)] += dp[s];
}
}
LL ans = 0;
for(int s=0; s<(1<<n); s++) {
if(!dp[s]) continue;
int A = 0, B = 0, tol_dam = 0;
for(int i=0; i<b; i++) {
if(s & (1<<i)) {
B++; tol_dam += dam[i+1];
}
}
for(int i=b; i<n; i++) {
if(s & (1<<i)) {
A++;
}
}
if(tol_dam >= p) {
ans += dp[s] * fac[n-A-B];
}
}
LL tol = fac[n];
LL gcds = gcd(ans, tol);
printf("%lld/%lld\n", ans/gcds, tol/gcds);
}
return 0;
}
HDU 5816 Hearthstone (状压DP)的更多相关文章
- 多校7 HDU5816 Hearthstone 状压DP+全排列
多校7 HDU5816 Hearthstone 状压DP+全排列 题意:boss的PH为p,n张A牌,m张B牌.抽取一张牌,能胜利的概率是多少? 如果抽到的是A牌,当剩余牌的数目不少于2张,再从剩余牌 ...
- HDU 4284Travel(状压DP)
HDU 4284 Travel 有N个城市,M条边和H个这个人(PP)必须要去的城市,在每个城市里他都必须要“打工”,打工需要花费Di,可以挣到Ci,每条边有一个花费,现在求PP可不可以从起点1 ...
- HDU 4336 容斥原理 || 状压DP
状压DP :F(S)=Sum*F(S)+p(x1)*F(S^(1<<x1))+p(x2)*F(S^(1<<x2))...+1; F(S)表示取状态为S的牌的期望次数,Sum表示 ...
- HDU 3001 Travelling ——状压DP
[题目分析] 赤裸裸的状压DP. 每个点可以经过两次,问经过所有点的最短路径. 然后写了一发四进制(真是好写) 然后就MLE了. 懒得写hash了. 改成三进制,顺利A掉,时间垫底. [代码] #in ...
- HDU - 5117 Fluorescent(状压dp+思维)
原题链接 题意 有N个灯和M个开关,每个开关控制着一些灯,如果按下某个开关,就会让对应的灯切换状态:问在每个开关按下与否的一共2^m情况下,每种状态下亮灯的个数的立方的和. 思路1.首先注意到N< ...
- hdu 4114(状压dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4114 思路:首先是floyd预处理出任意两点之间的最短距离.dp[state1][state2][u] ...
- HDU 3091 - Necklace - [状压DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3091 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- HDU 3811 Permutation 状压dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3811 Permutation Time Limit: 6000/3000 MS (Java/Othe ...
- hdu-5816 Hearthstone(状压dp+概率期望)
题目链接: Hearthstone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 5838 (状压DP+容斥)
Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的 ...
随机推荐
- 在CentOS 6.X 上面安装 Python 2.7.X
在CentOS 6.X 上面安装 Python 2.7.X CentOS 6.X 自带的python版本是 2.6 , 由于工作需要,很多时候需要2.7版本.所以需要进行版本升级.由于一些系统工具和服 ...
- IOS刷新数据
在一个项目开发过程中为了更好的体验经常会用到下拉刷新更新数据,当然也伴随一些上拉加载更多数据的情况:当前比较火的EGOTableViewPullRefresh只实现了下拉功能,而没有上拉的功能.这里介 ...
- Unity 教程和源码
12个Unity3D游戏源码 - 新手必备 愤怒的小鸟攻略技巧秘籍 NGUI 教程收录大全:http://forum.exceedu.com/forum/forum.php?mod=viewthre ...
- java实现DES算法
import java.util.UUID; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypt ...
- BZOJ2299: [HAOI2011]向量
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2299 题解:乱搞就可以了... 不妨认为有用的只有(a,b)(a,-b)(b,a)(b,-a) ...
- 用Java Servlets代替CGI
从网络的早期阶段开始,人们就希望他们的网络服务器不仅能够显示静态文档,通用网关接口(CGI) 扩展了服务器的能力,但其代价是增加了额外的处理进程.FastCGI则把外部CGI 处理进程同网络服务器更紧 ...
- HDU 5285 wyh2000 and pupil (二分图着色)
题意: 共有n个小学生,编号为1−n.将所有小学生分成2组,每组都至少有1个人.但是有些小学生之间并不认识,而且如果a不认识b,那么b也不认识a.Wyh2000希望每组中的小学生都互相认识.而且第一组 ...
- HDU 2544 最短路 (最短路,spfa)
题意:中文题目 思路:spfa+SLF优化.关于SPFA的详情请戳我 #include <bits/stdc++.h> using namespace std; , INF=0x7f7f7 ...
- 【RMQ问题】求数组区间最大值,NYOJ-1185-最大最小值
转自:http://blog.csdn.net/lilongherolilong/article/details/6624390 先挖好坑,明天该去郑轻找虐 RMQ(Range Minimum/Max ...
- wifi详解(四)
1 IOCTL的调用逻辑 之所以要分析这个,是因为上层wpa_supplicant和WIFI驱动打交道的方式,多半是通过ioctl的方式进行的,所以看看它的调用逻辑(这里只列出其主要的调 ...