HDU 5816 状压dp

来源:互联网 发布:java中public的翻译 编辑:程序博客网 时间:2024/05/17 03:59

Hearthstone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


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
题意:对面有p血,你有n张奥术智慧,m张火球术,接下来是每个火球术能造成的伤害,将这些牌打乱放入牌库,现在抽一张牌能斩杀对面的概率是多少。
ps:无限费用,牌有限。
作为一个资深的炉石玩家,这个题是无论如何也要弄懂。
题解:状压dfs,20张牌,可以用状压,处理出已经用掉哪些牌的时候的斩杀的排列个数,然后答案除以(n+m)!(全排列)即可。
#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;typedef long long ll;ll a[25],dp[1110000];ll po[25],p,n,m,geshu;void init(){    ll i;    po[0]=1;    for(i=1;i<=20;i++){        po[i]=i*po[i-1];    }}ll panduan(ll state){    geshu=n+m;    ll num=0,i;    for(i=0;i<n+m;i++){        if(state&(1<<i)){            if(i<=m-1){                num+=a[i];            }            geshu--;        }    }    if(num>=p)return 1;    else return 0;}ll dfs(ll state,ll cishu){    ll i,j;    if(cishu==0){        if(panduan(state))return po[geshu];        else return 0;    }    ll cnt=0;    ll state1;    for(i=0;i<=n+m-1;i++){        if(state&(1<<i ) )continue;        if(i>=m){            state1=(state|(1<<i));            if(dp[state1]!=-1)cnt+=dp[state1];            else{                cnt+=dfs(state1,cishu+1);            }        }        else{            state1=(state|(1<<i));            if(panduan(state1) )cnt+=po[geshu];            else{                if(dp[state1]!=-1)cnt+=dp[state1];                else cnt+=dfs(state1,cishu-1);            }        }    }    dp[state]=cnt;    return cnt;}int main(){    ll t;    init();    scanf("%lld",&t);    while(t--){        ll i,j;        scanf("%lld%lld%lld",&p,&n,&m);        ll zong=(1<<(n+m))-1;        for(i=0;i<m;i++){            scanf("%lld",&a[i]);        }        for(i=0;i<=zong;i++)dp[i]=-1;        ll tot=dfs(0,1);        ll k=__gcd(tot,po[n+m]);        printf("%lld/%lld\n",tot/k,po[n+m]/k);    }    return 0;}



0 0