hdu5816Hearthstone(状压dp)

题目链接:点这里!!!!


题意:炉石传说玩过吧,敌方英雄现在有p滴血,你的牌库里有n张A卡,m张B卡,A卡的效果是从牌库里的随机抽两张牌出来,B卡的效果是对敌方英雄造成xi点伤害。问你初始情况下,你从牌库里随机抽出一个张牌,问在一个回合内能打死敌方英雄的概率为多少?

数据范围:p<=1000,xi<=1000,n+m<=20


题解:

1、看到n+m<=20可以很自然的想到,用状压来做这道题。

2、我们设dp[s]为当前手牌为s的方案数。我假设A卡有p1张,B卡有p2张,下个状态的手牌数量为2*p1-(p1+p2)+1。

3、我们判断当前状态能不能打死,如果能打死再乘上后面的排列数记录下来,我们再判断当前状态能不能转移就可以了。


代码;

#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]
#pragma comment(linker, "/STACK:102400000000,102400000000")
typedef  long long LL;
const LL  MOD = 1000000007;
const int N = 55;
const int maxn = 1e6+15;
const int letter = 130;
const int INF = 1e9;
const double pi=acos(-1.0);
const double eps=1e-10;
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int p,n,m;
int a[22];
int p1,p2;
LL fac[22],dp[1<<22];
bool hf(int x){
    p1=0,p2=0;
    for(int i=0;i<n;i++) if(x&(1<<i)) p1++;
    for(int i=0;i<m;i++) if(x&(1<<(i+n)))p2++;
    return p1-p2+1>=0;
}
int get(int x){
    int ans=0;
    for(int i=0;i<m;i++) if(x&(1<<(i+n))) ans+=a[i];
    return ans;
}
LL gcd(LL a,LL b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int main(){
    fac[0]=1ll;
    for(int i=1;i<22;i++) fac[i]=fac[i-1]*i;
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d",&p,&n,&m);
        for(int i=0;i<m;i++) scanf("%d",a+i);
        clr(dp,0);
        dp[0]=1;
        LL ans=0;
        for(int x=0;x<(1<<(n+m));x++){
            if(!dp[x]||!hf(x)) continue;
            if(get(x)>=p){
                ans+=dp[x]*fac[n+m-p1-p2];
                continue;
            }
            if(p1-p2+1==0) continue;
            for(int i=0;i<n+m;i++){
                if(x&(1<<i)) continue;
                dp[x|(1<<i)]+=dp[x];
            }
        }
        LL gg=gcd(ans,fac[n+m]);
        printf("%I64d/%I64d\n",ans/gg,fac[n+m]/gg);
    }
    return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值