hdu5816Hearthstone(状压DP)

Hearthstone

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


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
  
  
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
 
这题题意就是你有n张可以抽两张牌的牌,m张可以造成伤害的牌,每张牌的伤害值不一样,问你能否造成K点伤害。

这道题因为m+n<=20那么就可以状态压缩,这里可以剪枝一下,就是如果当前伤害打满了的话,后来就不用去遍历他了。

AC代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<set>
using namespace std;
typedef long long ll;
short a[30],cnt[1<<20];
int n,m,limit;
ll f[25]={1},dp[1<<20];
ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}
void init(){
    for(int i=0;i<=(1<<20);i++)
        for(int j=0;j<20;j++)
            if(i&(1<<j))
                cnt[i]++;
    for(int i=1;i<=20;i++)
        f[i]=f[i-1]*i;
}
int cal(int s){
    int ans=0;
    for(int i=0;i<m;i++){
        if(s&(1<<i)){
            ans+=a[i];
        }    
    }
    return ans;
}
int coun(int s){
    int ans=0;
    for(int i=0;i<m;i++)
        if(s&(1<<i))
            ans++;
    return 2*(cnt[s]-ans)+1-cnt[s];
}
int main(){
    int t;
    init();
    scanf("%d",&t);
    while(t--){
        scanf("%d%d%d",&limit,&n,&m);
        for(int i=0;i<m;i++)
            scanf("%d",&a[i]);
        int all=(1<<(n+m))-1;
        ll ans=0;
        if(cal(all)>=limit){
            memset(dp,0,sizeof(dp));
            dp[0]=1;
            for(int i=0;i<=all;i++){
                if(dp[i]){
                        if(cal(i)>=limit)
                            ans+=dp[i]*f[n+m-cnt[i]];
                    else if(coun(i)>0){
                        for(int j=0;j<(n+m);j++)
                            if(!(i&(1<<j)))
                                dp[i|(1<<j)]+=dp[i];
                    }
                }
            }
        }
        ll gg=gcd(ans,f[n+m]);
        printf("%lld/%lld\n",ans/gg,f[n+m]/gg);
    }
    



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值