QOJ.ac

QOJ

时间限制: 6 s 内存限制: 256 MB 总分: 100

#12021. Texas Hold'em

统计

Kujou Karen often plays Texas Hold'em with her good friends, but because she is not very clever, she is always beaten by them. Therefore, she wants you to write a program to improve her poker skills.

A deck of cards (excluding jokers) contains 13 different values: $2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A$, which increase in rank from left to right. There are four suits, denoted by $0, 1, 2, 3$, and each suit has 13 cards corresponding to each value. In Texas Hold'em, we only consider these 52 cards.

A hand consists of 5 cards, which may form several types of hands. In descending order of rank, they are:

  1. Straight Flush: A straight (see definition below) of the same suit, for example, $T, J, Q, K, A$ of the same suit.
  2. Four of a Kind: Four cards of the same value, for example, $T, T, T, T, 2$ of any suits.
  3. Full House: Three cards of the same value and two cards of another same value, for example, $T, T, T, J, J$ of any suits.
  4. Flush: Five cards of the same suit, for example, $7, J, Q, K, A$ of the same suit.
  5. Straight: Five cards of consecutive values, for example, $2, 3, 4, 5, 6$ or $T, J, Q, K, A$ of any suits. Specifically, $A, 2, 3, 4, 5$ is also a straight (but $K, A, 2, 3, 4$ is not). Thus, there are 10 different straights, starting with $A, 2, 3, 4, 5, 6, 7, 8, 9, T$ respectively.
  6. Three of a Kind: Three cards of the same value, for example, $T, T, T, J, Q$ of any suits.
  7. Two Pair: Two different pairs (a pair is two cards of the same value), for example, $T, T, Q, Q, K$ of any suits.
  8. Pair: Two cards of the same value, for example, $T, T, J, Q, K$ of any suits.
  9. High Card: A hand that does not satisfy any of the above types.

A hand may satisfy multiple types simultaneously; in this case, the highest-ranking type is considered the hand's type.

We compare two hands as follows:

  • If the hand types are different, the hand with the higher type is better.
  • If both hands are straights or straight flushes, they are compared by the rank of the first card of the straight, ordered from smallest to largest as $A, 2, 3, 4, 5, 6, 7, 8, 9, T$. That is, $A, 2, 3, 4, 5$ is the smallest straight, and $T, J, Q, K, A$ is the largest.
  • Otherwise, we sort the five cards by (frequency, value) in descending order. For example, $K, K, T, T, T$ becomes $T, T, T, K, K$; $2, T, T, K, A$ becomes $T, T, A, K, 2$.
  • Compare the two sequences lexicographically.

Note that the suit only affects the first step (determining the hand type) and does not affect the subsequent comparisons.

Here are some examples:

  • $5, 6, 7, 8, 9$ of the same suit is greater than $A, 2, 3, 4, 5$ of the same suit.
  • $A, 2, 3, 4, 5$ of the same suit is greater than $2, 4, 5, 6, 7$ of the same suit.
  • $3, 3, 8, 8, K$ of any suits is greater than $5, 5, 7, 7, A$ of any suits.
  • $Q, Q, Q, T, T$ of any suits is greater than $J, J, J, A, A$ of any suits.

In Texas Hold'em, a player's "board" consists of 7 cards. These 7 cards can form $\binom{7}{5}$ different hands, and the strength of the player's board is equal to the strength of the best hand among them.

Today, Kujou Karen has brought her good friend Rikka to help her improve her Texas Hold'em skills. Because Karen is quite inexperienced, they start with a simplified version of the rules. The game consists of 5 stages:

  • Preparation Stage:
    • First, Karen and Rikka each draw 2 cards from a deck (excluding jokers) and reveal them (both players can see these 4 cards).
    • The remaining 48 cards are shuffled completely uniformly (i.e., all $48!$ permutations are equally likely).
    • $2b$ chips are added to the pot, and Karen and Rikka each receive $m$ chips.
  • Round 1:
    • Karen and Rikka perform a round of betting (see below). If Rikka chooses to fold, Karen wins and the game ends.
    • The first 3 of the 48 cards are revealed.
  • Round 2:
    • Karen and Rikka perform a round of betting. If Rikka chooses to fold, Karen wins and the game ends.
    • The next 1 of the 45 remaining cards is revealed.
  • Round 3:
    • Karen and Rikka perform a round of betting. If Rikka chooses to fold, Karen wins and the game ends.
    • The next 1 of the 44 remaining cards is revealed.
  • Round 4:
    • Karen and Rikka perform a round of betting. If Rikka chooses to fold, Karen wins and the game ends.
    • Each player's board consists of the 2 cards they drew in the preparation stage plus the 5 revealed community cards. The player with the stronger board wins. If the board strengths are equal, it is a draw. The game ends.

An example of a draw: Suppose Karen and Rikka's hands are $2, 3$ of suit 0 and $2, 3$ of suit 1 respectively, and the community cards are $T, J, Q, K, A$ of suit 2. Both players have a straight flush of $T, J, Q, K, A$, so their board strengths are equal.

The game involves several rounds of betting. To accommodate Karen, they use a betting rule favorable to her:

  • Suppose at the start of a betting round, Karen has $k$ chips. Karen must choose an integer $i$ in $[0, k]$ and put $i$ chips into the pot (Karen then has $k-i$ chips remaining).
  • Rikka can choose to call or fold. If Rikka chooses to call, she must also put $i$ chips into the pot, and the game continues. If Rikka chooses to fold, the game ends and Karen wins.

It is easy to see that after each betting round, if Rikka does not fold, then (1) Rikka and Karen have the same number of chips remaining, and (2) the pot size is even. When the game ends, if one player wins, the winner takes all the chips in the pot; if it is a draw, the chips in the pot are split equally.

You happen to pass by while Karen and Rikka are playing. At this moment:

  • Round $s (s \in \{1, 2, 3\})$ has just begun, and Karen and Rikka have not yet started betting.
  • You see the cards held by both players (two cards each) and the $n$ currently revealed cards (for $s = 1, 2, 3$, $n$ is $0, 3, 4$ respectively).
  • There are $2w$ chips in the pot, and Karen and Rikka each have $m'$ chips.

Assuming that Karen and Rikka both use optimal strategies in the remaining game (both aim to maximize the expected number of chips they hold when the game ends), you need to calculate the expected number of chips Karen will have when the game ends.

Input

The first line contains an integer $T (1 \leq T \leq 10)$ representing the number of test cases.

For each test case, the first line contains three integers $s, w, m' (s \in \{1, 2, 3\}, 0 \leq w, m' \leq 50)$, representing the current round, half the number of chips in the pot, and the number of chips Karen and Rikka each have.

The next $4 + n$ lines (where $n$ is $0, 3, 4$ for $s = 1, 2, 3$ respectively) each contain two integers $c_i, w_i (c_i \in \{0, 1, 2, 3\}, w_i \in \{2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A\})$, describing a card. The first two are Karen's hand, the 3rd-4th are Rikka's hand, and the last $n$ are the currently revealed cards.

It is guaranteed that these $4+n$ cards are distinct.

Output

For each test case, output one line representing the expected number of chips Karen will have at the end of the game. The probability should be output as an irreducible fraction: for example, $3.5$ should be output as $7/2$; $0$ as $0/1$; $1$ as $1/1$; $0.999$ as $999/1000$.

Examples

Input 1

3
3 1 10
0 A
1 A
2 A
3 A
1 K
1 Q
0 J
1 T
3 1 3
0 A
1 A
2 A
3 A
1 K
1 Q
0 J
1 T
2 1 10
0 A
1 A
2 A
3 A
1 K
1 Q
0 J

Output 1

12/1
53/11
23/2

Input 2

3
1 1 10
0 A
1 A
2 A
3 A
1 1 10
0 2
1 2
2 2
3 2
1 1 10
0 A
1 Q
2 K
2 J

Output 2

9547907/856152
1591187/142692
12/1

Subtasks

Subtask 1 (38 points): $s = 3$.

Subtask 2 (41 points): $s = 2$.

Subtask 3 (21 points): $s = 1$.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.