zoukankan      html  css  js  c++  java
  • hdu-5816 Hearthstone(状压dp+概率期望)

    题目链接:

    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
    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张A牌,m张B牌,A牌能取两张.B牌能打掉a[i]的血,问打掉至少p点血的概率是多少;
     
    思路:
     
    dp[i]表示i状态下的方案数,所求答案就是大于等于p点的状态的方案数总和与(n+m)!的比值;
    状态的转移的话就是手中的A种牌在这个状态下是否还能拿牌,具体的可以看这个链接
     
    AC代码:
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    //#include <bits/stdc++.h>
    #include <stack>
    #include <map>
     
    using namespace std;
     
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
     
    typedef  long long LL;
     
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
     
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const int inf=1e9;
    const int N=1e5+10;
    const int maxn=(1<<20)+14;
    const double eps=1e-12;
    
    LL dp[maxn],f[23];
    int num[maxn],p,n,m,a[30];
    inline void Init()
    {
        for(int i=0;i<maxn;i++)
        {
            for(int j=0;j<20;j++)
            {
                if(i&(1<<j))num[i]++;
            }
        }
        f[0]=1;
        for(int i=1;i<=20;i++)f[i]=f[i-1]*i;
    }
    int cal(int x)
    {
        int sum=0;
        for(int i=0;i<m;i++)
        {
            if(x&(1<<i))sum+=a[i];
        }
        return sum;
    }
    
    LL gcd(LL x,LL y)
    {
        if(y==0)return x;
        return gcd(y,x%y);
    }
    int counter(int x)
    {
        int sum=0;
        for(int i=0;i<m;i++)
        {
            if(x&(1<<i))sum++;
        }
        return num[x]-2*sum+1;
    }
    int main()
    {
        Init();
        int t;
        read(t);
        while(t--)
        {
            mst(dp,0);
            read(p);read(n);read(m);
            For(i,0,m-1)read(a[i]);
            LL ans=0;
            int tot=(1<<(n+m))-1;
            dp[0]=1;
            for(int i=0;i<=tot;i++)
            {
                if(!dp[i])continue;
                if(i==tot||counter(i)==0)
                {
                    if(cal(i)>=p)ans=ans+dp[i]*f[n+m-num[i]];
                }
                else 
                {
                    for(int j=0;j<n+m;j++)
                    {
                        if(i&(1<<j))continue;
                        dp[i|(1<<j)]+=dp[i];
                    }
                }
            }
            LL g=gcd(ans,f[n+m]);
            cout<<ans/g<<"/"<<f[n+m]/g<<"
    ";
        }
        return 0;
    }
    

      

  • 相关阅读:
    获取类中虚函数地址
    指向成员函数指针,虚函数指针,静态成员函数指针
    桥接模式 Bridge
    装饰模式 Decorate
    享元模式 FlyWeight
    java中的foreach循环
    java可变参数
    java异常处理
    java设计模式之单例设计模式和多例设计模式
    java四种访问控制权限:public ,default,protected,private
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5759311.html
Copyright © 2011-2022 走看看