#P5301. [GXOI/GZOI2019] 宝牌一大堆
[GXOI/GZOI2019] 宝牌一大堆
Description
Mahjong is a traditional board game played by players, who take turns drawing and discarding tiles. In competitive matches, the two main rulesets are Chinese Official Mahjong (China) and Riichi Mahjong (Japan). This problem uses a special ruleset: “A Pile of Dora Tiles”.
Tile Types
A full mahjong set contains tiles, including different types of tiles, with copies of each type. These types are:
-man to -man, -sou to -sou, -pin to -pin, East, South, West, North, Red, White, Green.

They can form different groups:
- Sequence: consecutive number tiles in manzu, or consecutive number tiles in souzu, or consecutive number tiles in pinzu.
- Triplet: identical tiles.
- Quad: identical tiles.
- Pair: identical tiles.
Sequences and triplets are collectively called melds.
Winning Hand
A hand (the tiles held by one player) that declares victory is called a “winning hand”.
- When a player holds tiles and satisfies one of the following three conditions, it is a winning hand:
- There exists a way to split these tiles into melds and pair, denoted as “”.
- There exists a way to split these tiles into distinct pairs, called “Seven Pairs”.
- These tiles consist only of the following tile types: -man, -man, -sou, -sou, -pin, -pin, East, South, West, North, Red, White, Green; and among these types, each type appears at least once. This is called “Thirteen Orphans”.
- When a player holds tiles and there exists a way to split these tiles into quad, melds, and pair, it is a winning hand.
- When a player holds tiles and there exists a way to split these tiles into quads, melds, and pair, it is a winning hand.
- When a player holds tiles and there exists a way to split these tiles into quads, meld, and pair, it is a winning hand.
- When a player holds tiles and there exists a way to split these tiles into quads and pair, it is a winning hand.
Dora Tiles
In each round, several “dora tiles” are also specified. When you win, each dora tile in your hand doubles the payoff (explained in detail below).
Achieved Score
Since there are many possible winning hands, we define an “achieved score” for each winning hand. This score equals: the number of ways to choose some tiles from all tiles that have not been discarded yet such that they form this hand, multiplied by .
This score considers both the chance to win and the payoff after winning. In theory, it is better to aim for hands with higher scores.
For example, the hand shown below is clearly a winning hand. If there are still copies of -man, copies of -man, and copies of each of -man through -man not yet discarded on the table, and the dora tile is -man, then the achieved score of the hand below is , where denotes the binomial coefficient.

In particular, for a “Seven Pairs” winning hand, the achieved score is additionally multiplied by .
For a “Thirteen Orphans” winning hand, the achieved score is additionally multiplied by .
One day, Xiao L, Xiao Y, Xiao I, and Xiao W were playing mahjong. Yukino Wakana, who happened to pass by, saw all the tiles that had already been discarded, but did not know any player’s hand. Perhaps you have already guessed what happens next—
—Yukino Wakana wants to know, among all tiles not yet discarded, what is the maximum achieved score of any winning hand. But she still needs to watch the mahjong match, so she leaves this problem to you.
Input Format
Each test point contains multiple test cases. The first line is an integer , the number of test cases. Note that the test cases are independent.
Each test case consists of two lines. The first line gives the tiles already discarded on the table, and the second line gives all dora tiles for this round.
We use for manzu, for pinzu, for souzu, for East, South, West, North, and for Red, White, Green. Adjacent tiles are separated by a single space, and each line ends with a standalone indicating the end.
Output Format
The output should contain lines. For each test case, output one integer, the maximum score.
2
0
0
7m 4p 2s 7s 6p 8s 7p 5s 9s 9s 1p 5m 9m 5s 4p 5s E 1p 6s 5p B 4m 6m W 6p 6s E 9s 5p 2s 8s 8p 4m 3s 9m 5p 3s 2s 6s 8s 8p 6p 5m 4s 3m 4s 5s 4s 6m 9s 6p N 5m 7s 4m 2m 2s 6s 3m 7p B B N 1m 3m B 8p F 7p 0
W 4p N 3m 2m B 9m 3p 1p 6p S 4s 5p 8s 4m 5s 2s 3s 0
1308622848
127401984
Hint
Sample Explanation
In the first test case, no tiles have been discarded, and there are no dora tiles. “Thirteen Orphans” has the highest score, which is .
The scores for “” and “Seven Pairs” are and , respectively.
In the second test case, “” has the highest score, which is . One hand that achieves this score is “”.
The score for “Seven Pairs” is , and it is impossible to form “Thirteen Orphans”.
Constraints
It is guaranteed that the discarded tiles are always legal tiles, and for each type there are no more than copies. Dora tiles are given without repetition.
| Test Point ID | Scale of | Discarded Tiles | Number of Dora Tiles |
|---|---|---|---|
| No special restrictions | tiles | ||
| Includes at least all number tiles from to | |||
| At least copies of each type | |||
| At least copies of each type | |||
| No special restrictions | tiles | ||
| tiles | |||
Translated by ChatGPT 5
京公网安备 11011102002149号