hdu 5816 Hearthstone 状态dp

来源:互联网 发布:剑灵女灵族捏脸数据图 编辑:程序博客网 时间:2024/05/21 08:56

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

题意:给p,n,m三个数,分别表示敌人的血量,可抽A牌的数量(A牌可以再抽两张),可抽B牌的数量(B牌对敌人造成a[i]点伤害),问现在轮到我抽一张牌(抽了A牌后又可以抽2张,如此往复),问最后打死敌人的概率。

分析:分母是所有牌的全排列
分子是可行的方案数,考虑n,m不大,状压dp,把所有可行状态转移过来。只要A>当前的B+1,那么证明是还可以转移到下一个状态的,但是如果当前状态就已经可行的话,就不需要转移了,因为如果转移的话会和后面成功的状态后剩下的牌的全排列重复。

#include <bits/stdc++.h>using namespace std;typedef long long ll;ll dp[(1<<20)+100];int val[21];ll f[21];void init(){    f[0]=1;    for(int i=1;i<=20;i++)        f[i]=f[i-1]*(i*1LL);}ll gcd(ll a,ll b){    return b==0?a:gcd(b,a%b);}int main(){    int t;    init();    scanf("%d",&t);    while(t--){        int p,n,m;        scanf("%d%d%d",&p,&n,&m);        int N = (n+m);        int end=(1<<N);        for(int i=0;i<m;i++)        {            scanf("%d",&val[i]);        }        memset(dp,0,sizeof(dp));        dp[0]=1;        for(int st=0;st<end;st++)        {            if(dp[st]==0) continue;            int A=0,B=0,sum=0;            for(int j=0;j<m;j++)            {                if((1<<j)&st)                 {                    sum+=val[j];                    B++;                }            }            if(sum>=p) continue;            for(int j=m;j<N;j++)            {                if((1<<j)&st)                {                    A++;                }            }            if(A-B+1>0)            {                for(int j=0;j<N;j++)                {                    if(!((1<<j)&st))                    {                        dp[st^(1<<j)]+=dp[st];//当前状态牺牲一张A可以达到的后续,状态转移是每次转移一步                    }                }            }        }        ll up=0,down=f[N];        for(int st=0;st<end;st++)        {            if(dp[st]==0) continue;//无用的状态直接跳过 达到剪枝的效果            int sum=0,A=0,B=0;            for(int j=0;j<m;j++)            {                if((1<<j)&st)                 {                    sum+=val[j];                    B++;                }            }            for(int j=m;j<N;j++)            {                if((1<<j)&st)                {                    A++;                }            }            if(sum>=p)            {                up+=(ll)dp[st]*f[N-A-B];            }           }        ll g=__gcd(up,down);        printf("%I64d/%I64d\n",up/g,down/g );    }}
原创粉丝点击