题目

Source

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<=10).
Then come three positive integers P (P<=1000), N and M (N+M<=20), representing the enemy’s HP, the number of A-Cards and the number of B-Cards in the card deck, respectively. Next line come M integers representing X (0<X<=1000) values for the B-Cards.

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

分析

题目大概说有两种卡牌,使用A牌能从牌堆摸两张牌,使用B牌能对对方造成xi点伤害。在你的回合,你从牌堆摸一张牌,问能对对方造成p点及以上伤害的概率。

要求的其实就是能造成p点以上伤害的牌堆排列数/牌堆全排列数

全排列而且总数为20,这种就该想到尝试用状压DP。。

  • dp[S]表示已经摸的牌的集合为S的可行排列方案数

对于一个状态S,我们能从这个集合中已经摸的牌知道还能摸几张,即A的数目 * 2 - A的数目 - B的数目 + 一开始能摸的一张牌

考虑状态的转移,我是用我为人人实现的:从小到大枚举状态S,判断S是否合法,即S的方案数不为0且还能摸牌,然后通过S去更新S+i(i∉S)状态的值。另外如果S能造成的伤害已经大于等于p了,那就没必要去更新它能转移到的状态,因为还剩下的牌直接求全排列计算其贡献,这样也能避免重复计算。

最后就循环遍历各个合法的状态累加贡献,这个贡献就是dp值 * 还没摸的牌的全排列数。这样就求出能造成p点以上伤害的牌堆排列数,再和全排列数用GCD搞搞输出答案即可。

代码

#include<cstdio>
#include<cstring>
using namespace std; long long fact[22]={1}; long long gcd(long long a,long long b){
if(b==0) return a;
return gcd(b,a%b);
} int p,n,m,N,x[22];
long long d[1<<20]; int main(){
for(int i=1; i<22; ++i) fact[i]=fact[i-1]*i;
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&p,&n,&m);
N=n+m;
for(int i=n; i<N; ++i){
scanf("%d",&x[i]);
} memset(d,0,sizeof(d));
d[0]=1;
for(int s=0; s<(1<<N); ++s){
if(d[s]==0) continue; int A=0,B=0,damage=0;
for(int i=n; i<N; ++i){
if(s>>i&1){
damage+=x[i];
++B;
}
}
if(damage>=p) continue;
for(int i=0; i<n; ++i){
if(s>>i&1) ++A;
} if(A-B+1<=0) continue; for(int i=0; i<N; ++i){
if(s>>i&1) continue;
d[s^(1<<i)]+=d[s];
}
} long long xx=0,yy=fact[N];
for(int s=0; s<(1<<N); ++s){
if(d[s]==0) continue;
int A=0,B=0,damage=0;
for(int i=n; i<N; ++i){
if(s>>i&1){
damage+=x[i];
++B;
}
}
for(int i=0; i<n; ++i){
if(s>>i&1) ++A;
}
if(damage>=p){
xx+=d[s]*fact[N-A-B];
}
}
long long g=gcd(xx,yy);
printf("%I64d/%I64d\n",xx/g,yy/g);
}
return 0;
}

HDU5816 Hearthstone(状压DP)的更多相关文章

  1. 多校7 HDU5816 Hearthstone 状压DP+全排列

    多校7 HDU5816 Hearthstone 状压DP+全排列 题意:boss的PH为p,n张A牌,m张B牌.抽取一张牌,能胜利的概率是多少? 如果抽到的是A牌,当剩余牌的数目不少于2张,再从剩余牌 ...

  2. hdu-5816 Hearthstone(状压dp+概率期望)

    题目链接: Hearthstone Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Other ...

  3. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  4. nefu1109 游戏争霸赛(状压dp)

    题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...

  5. poj3311 TSP经典状压dp(Traveling Saleman Problem)

    题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...

  6. [NOIP2016]愤怒的小鸟 D2 T3 状压DP

    [NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...

  7. 【BZOJ2073】[POI2004]PRZ 状压DP

    [BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...

  8. bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)

    数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...

  9. HDU 1074 Doing Homework (状压dp)

    题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...

随机推荐

  1. September 22nd 2016 Week 39th Thursday

    Things won are done, the soul of joy lies in the doing. 得到即是完结,快乐的精髓在于过程. Things won are done, thing ...

  2. 一分钟可知css3版大白源码

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. Stanford大学机器学习公开课(三):局部加权回归、最小二乘的概率解释、逻辑回归、感知器算法

    (一)局部加权回归 通常情况下的线性拟合不能很好地预测所有的值,因为它容易导致欠拟合(under fitting).如下图的左图.而多项式拟合能拟合所有数据,但是在预测新样本的时候又会变得很糟糕,因为 ...

  4. 解决osg路径与文件名中的中文字符问题

    转至:http://blog.csdn.net/zhuqinglu/article/details/2064013 在打开或者保存一个osg模型的时候,经常遇到中文路径或者中文文件名的问题,此时会提示 ...

  5. 一个TextView内显示不同颜色的文字

    String format = "<font color='#FC8262'>%s</font>:%s"; String text = String.for ...

  6. NYOJ题目111分数加减法

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsEAAAKBCAIAAAA5i+FPAAAgAElEQVR4nO3dPXLbugMv7LsJ916Iay ...

  7. Python 自然语言处理(1) 计数词汇

    Python有一个自然语言处理的工具包,叫做NLTK(Natural Language ToolKit),可以帮助你实现自然语言挖掘,语言建模等等工作.但是没有NLTK,也一样可以实现简单的词类统计. ...

  8. 设计模式学习之桥接模式(Bridge,结构型模式)(15)

    参考地址:http://terrylee.cnblogs.com/archive/2006/02/24/336652.html 概述 在软件系统中,某些类型由于自身的逻辑,它具有两个或多个维度的变化, ...

  9. 编程风格规范google版

    python's  coding style,google 命名

  10. SQL在INNER JOIN时,也可以将子查询加入进来

    这个语法有点神奇,记下. 但觉得用处有限吧. mysql> SELECT a.account_id, a_cust_id, a.open_date, a.product_cd -> FRO ...