HDU 5816 Hearthstone (状压dp)

来源:互联网 发布:c 高级编程 第8版 编辑:程序博客网 时间:2024/06/06 11:15

Hearthstone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 779    Accepted Submission(s): 38

Problem 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
23 1 21 23 5 101 1 1 1 1 1 1 1 1 1
Sample Output
1/346/273
Author
SYSU
Source
2016 Multi-University Training Contest 7
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5831 5830 5829 5828 5827 

题意:有两种卡牌,使用A牌能从牌堆摸2张牌,使用B牌能对对方造成 i 点伤害。在你的回合,你从牌堆摸一张牌,问你能对敌方造成p点或以上伤害的概率是多少。 

题解:
状压dp。因为M+N<=20,所以M最大为20。这样的话,O(N*2^M)状态压缩dp的做法就可以AC。
设dp[s]表示已经摸了的牌的集合为s的可行排列方案数。那么通过这个集合我们可以知道还可以摸多少张牌,即2*A -A-B+1.(1表示开始摸的一张牌) = A-B+1。
然后是状态的转移:从小到大枚举更新状态s,不断判断s的方案数不为0且还能摸牌。如果s能造成的伤害已经大于等于p了,那就直接计算这种状态的贡献。最后就把上面几个合法状态的贡献累加,即代码中的dp值 * 还没摸的卡牌的全排列数,即能造成p点以上伤害的牌堆排列数x。再求概率即可,假设全排列数是y。那答案就是x/y。

官方题解:
这题其实有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)!就是答案. 这题因为定义为比较简单的题目,所以M最大只到20,这样O(N*2^M)状态压缩dp的做法也是可以通过。

AC代码:
#include<bits/stdc++.h>#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<cstring>#include<vector>#include<map>#include<queue>#include<set>#include<stack>using namespace std;typedef long long ll;typedef unsigned long long ull;int read(){int v = 0, f = 1;char c =getchar();while( c < 48 || 57 < c ){if(c=='-') f = -1;c = getchar();}while(48 <= c && c <= 57) v = v*10+c-48, c = getchar();return v*f;}ll fact[22]={1};ll dp[1<<20];int p,n,m,N,x[22]; int main(){//freopen("in.txt","r",stdin);    for(int i=1; i<22; ++i) fact[i]=fact[i-1]*i; //全排列数     int t;     t=read();    while(t--){        p=read(),n=read(),m=read();        N=n+m;        for(int i=n; i<N; ++i){                      x[i]=read();        }           memset(dp,0,sizeof(dp));        dp[0]=1;        for(int s=0; s<(1<<N); ++s){            if(dp[s]==0) continue;                        int A=0,B=0,injure=0;            for(int i=n; i<N; ++i){                if((s>>i) & 1){                    injure+=x[i];   //计算伤害值                     ++B;                }            }            if(injure>=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;                dp[s^(1<<i)]+=dp[s];            }        }                       ll xx=0, yy=fact[N];                for(int s=0; s<(1<<N); ++s){            if(dp[s]==0) continue;                        int A=0,B=0,injure=0;            for(int i=n; i<N; ++i){                if((s>>i) & 1){                    injure+=x[i];                    ++B;                }            }            for(int i=0; i<n; ++i){                if((s>>i) & 1) ++A;            }            //s能造成的伤害大于等于p            if(injure>=p){//计算贡献                xx+=dp[s]*fact[N-A-B];            }        }         ll g=__gcd(xx,yy);        printf("%I64d/%I64d\n",xx/g,yy/g);    }    return 0;}

标程:
#include <cstdio>#include <iostream>using namespace std;typedef long long lli;int bc[1<<20], sum[1<<20], tmp[1<<20];int C[21][21];lli f[21][21], fact[21];lli gcd(lli a, lli b){    if (b == 0) return a;    return gcd(b, a % b);}void init(){    int i, j;    //bc[i]表示i的二进制表示中一的个数是多少    bc[0] = 0;    for (i=1; i<(1<<20); i++)        bc[i] = bc[i-(i&-i)] + 1;    //fact[i]表示i的全排列    fact[0] = 1;    for (i=1; i<=20; i++) fact[i] = fact[i-1] * i;    //C表示组合数C[n][m]n表示下面的    C[0][0] = 1;    for (i=1; i<=20; i++)    {        C[i][0] = C[i][i] = 1;        for (j=1; j<i; j++) C[i][j] = C[i-1][j] + C[i-1][j-1];    }    //i张a卡片,j张b卡片正确实现的方案数    //当前的方案数,等于i-1 , j 的方案数 + i,j - 1的方案数    //由于正确的方案数来说 当a为k - 1时,B最多为k,让B更大时的方案数等于为k时的方案数,为了后面的使用(大概吧)    f[0][0] = f[0][1] = 1;    for (i=1; i<=20; i++)    {        f[i][0] = 1;        for (j=1; j<i; j++)        {            f[i][j] = f[i-1][j] + f[i][j-1];        }        f[i][i] = f[i][i+1] = f[i][i-1];    }}int main(){    freopen("in.txt","r",stdin);    lli a, b, d;    int p, n, m, t, i;    init();    scanf("%d", &t);    while (t --)    {        scanf("%d %d %d", &p, &n, &m);        for (i=0; i<m; i++) scanf("%d", &tmp[1<<i]);//tmp存储的时候就是按照1左移存储的        //按照一定规则枚举所有sum的和        sum[0] = 0;        for (i=1; i<(1<<m); i++)//这个遍历不存在一张都不选的这种情况            sum[i] = sum[i-(i&-i)] + tmp[i&-i];//每次都保证减去最低位后的状态是已经求出的,或者状态是空sum[0]。比如从1000开始到1001(1000求出)到1010(1000求出)到1011(减去最低位后为1010也已求出)。那么为什么地位减去后一定是已求出的呢,因为减去小于其本身,sum[i]又是顺序遍历的        //此处还有问题,为什么是不重不漏的呢?每次到临界值(1,10,100,1000)时sum[i]的值都等于tmp[i].当减去最低位后,不肯能是等于它本身的,此时又再加上了另一个数,        //注意此处的1,0是二进制。实际上是sum[1] = tmp[1],sum[10] = tmp[10],sum[11] = sum[10] + temp[1] = temp[10] + temp[1],以此类推sum[1111] = temp[1] + temp[10] + temp[100] + temp[1000]        //其实很简单的啦,每次sum[i]表示的都是对于i的二进制数来说0,代表取到1代表取不到。肯定是全部的状态(不包括都取不到)        for (i=0; i<(1<<m); i++)//k = bc[i].表示从所有b中选取n个b卡片,实际卡片的个数。此时b选择哪几个卡片是确定的,而a选那几个是不确定的,所以需要乘以a的组合数。并对选取的a卡片和b卡片全排列,再将剩下未选择的卡片全排列(因为后面的概率是1了,剩下的全排列除以(n + m)!保证是1)        {            if (sum[i] >= p && bc[i] <= n + 1)            {                a += C[n][bc[i]-1] * f[bc[i]-1][bc[i]] * fact[bc[i]-1] * fact[bc[i]] * fact[n+m-2*bc[i]+1];                //还有一种状态是所有的卡片都取光了,既然都取光了,那么对于A类卡片的组合数一定是1,那么他们的方案数也应该是取n张a卡片,m张b卡片。再分别对他们全排列,而已经取光了,后面就不再取了                //在这种状态中我们可以看到b中的卡片都被取光了,而a中依旧有剩余的卡片,那么剩下的a中剩余的卡片一定会被取光。最终的结果就是a,b都取光了                //在这种情况下我们可以看到b都取关了,而a还有剩余。那就意味着n > m+1,与计算k-1和k,k+1这种情况是不同的                if (bc[i] == m && bc[i] < n + 1) a += f[n][m] * fact[n] * fact[m];            }            //本质上这两种情况都是意味着不能再抽牌了        }        b = fact[n+m];        d = gcd(a, b);        printf("%I64d/%I64d\n", a / d, b / d);    }    return 0;}



2 0