HDU 5816 Hearthstone (Probability dp, Conbinations)

xiaoxiao2021-02-27  576

Hearthstone

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

Hearthstone

这题其实有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的做法也是可以通过.

  // // dp[i][j] represents the total number of proposals of // take i A cards and j B cards // #include <bits/stdc++.h> #define endl "\n" using namespace std; typedef long long ll; ll dp[24][24], C[24][24], A[24]; int x[24]; int n, m, p; void Init() { A[0] = 1; for(int i = 1; i <= 20; ++i) A[i] = A[i-1] * i; C[0][0] = 1; for(int i = 1; i <= 20; ++i) { C[i][0] = 1; for(int j = 1; j <= i; ++j) { C[i][j] = C[i-1][j] + C[i-1][j-1]; } } memset(dp, 0, sizeof(dp)); dp[0][0] = 0; dp[0][1] = 1; for(int i = 1; i <= 20; ++i) { dp[i][0] = 1; for(int j = 1; j < i; ++j) { dp[i][j] = dp[i-1][j] + dp[i][j-1]; } dp[i][i] = dp[i][i+1] = dp[i][i-1]; } } ll ans; void dfs(int cur, int sum, int k) { // at most n+1 x cards if(k > n+1) return; for(int i = cur; i < m; ++i) { if(sum + x[i] >= p) { if(k == 1) { ans += A[n+m-1]; // rest of n+m-1 cards with random permutation } else { // permutation of cards to be taken and left ans += dp[k-1][k] * C[n][k-1] * A[k-1] * A[k] * A[n+m-2*k+1]; } if(k == m && n >= m) { ans += dp[n][m] * A[n] * A[m]; } } dfs(i+1, sum+x[i], k+1); } } int main() { ios::sync_with_stdio(false); //freopen("out.txt", "w", stdout); cin.tie(NULL); cout.tie(NULL); int T; Init(); cin >> T; while(T--) { cin >> p >> n >> m; for(int i = 0; i < m; ++i) { cin >> x[i]; } ans = 0; dfs(0, 0, 1); ll g = __gcd(ans, A[n+m]); if(ans) cout << ans/g << "/" << A[n+m]/g << endl; else cout << "0/1" << endl; } return 0; }
转载请注明原文地址: https://www.6miu.com/read-29.html

最新回复(0)