#P5301. [GXOI/GZOI2019] 宝牌一大堆

[GXOI/GZOI2019] 宝牌一大堆

Description

Mahjong is a traditional board game played by 44 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 136136 tiles, including 3434 different types of tiles, with 44 copies of each type. These 3434 types are:
11-man to 99-man, 11-sou to 99-sou, 11-pin to 99-pin, East, South, West, North, Red, White, Green.

doraippai1.png

They can form different groups:

  • Sequence: 33 consecutive number tiles in manzu, or 33 consecutive number tiles in souzu, or 33 consecutive number tiles in pinzu.
  • Triplet: 33 identical tiles.
  • Quad: 44 identical tiles.
  • Pair: 22 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 1414 tiles and satisfies one of the following three conditions, it is a winning hand:
    1. There exists a way to split these 1414 tiles into 44 melds and 11 pair, denoted as “3×4+23 \times 4 + 2”.
    2. There exists a way to split these 1414 tiles into 77 distinct pairs, called “Seven Pairs”.
    3. These 1414 tiles consist only of the following 1313 tile types: 11-man, 99-man, 11-sou, 99-sou, 11-pin, 99-pin, East, South, West, North, Red, White, Green; and among these 1313 types, each type appears at least once. This is called “Thirteen Orphans”.
  • When a player holds 1515 tiles and there exists a way to split these 1515 tiles into 11 quad, 33 melds, and 11 pair, it is a winning hand.
  • When a player holds 1616 tiles and there exists a way to split these 1616 tiles into 22 quads, 22 melds, and 11 pair, it is a winning hand.
  • When a player holds 1717 tiles and there exists a way to split these 1717 tiles into 33 quads, 11 meld, and 11 pair, it is a winning hand.
  • When a player holds 1818 tiles and there exists a way to split these 1818 tiles into 44 quads and 11 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 2(number of dora tiles in the hand)2^{(\text{number of dora tiles in the hand})}.
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 33 copies of 11-man, 44 copies of 99-man, and 22 copies of each of 22-man through 88-man not yet discarded on the table, and the dora tile is 99-man, then the achieved score of the hand below is C33C43C22(C21)623=2048C_3^3 C_4^3 C_2^2 (C_2^1)^6 2^3 = 2048, where CC denotes the binomial coefficient.

doraippai2.png

In particular, for a “Seven Pairs” winning hand, the achieved score is additionally multiplied by 77.
For a “Thirteen Orphans” winning hand, the achieved score is additionally multiplied by 1313.

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 TT, 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 1m,2m,,9m\texttt{1m},\texttt{2m},\dots,\texttt{9m} for manzu, 1p,2p,,9p\texttt{1p},\texttt{2p},\dots,\texttt{9p} for pinzu, 1s,2s,,9s\texttt{1s},\texttt{2s},\dots,\texttt{9s} for souzu, E,S,W,N\texttt E,\texttt S,\texttt W,\texttt N for East, South, West, North, and Z,B,F\texttt Z,\texttt B,\texttt F for Red, White, Green. Adjacent tiles are separated by a single space, and each line ends with a standalone 00 indicating the end.

Output Format

The output should contain TT 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 13×6×41213 \times 6 \times 4^{12}.
The scores for “3×4+23 \times 4 + 2” and “Seven Pairs” are 100663296100663296 and 19595521959552, respectively.

In the second test case, “3×4+23 \times 4 + 2” has the highest score, which is 127401984127401984. One hand that achieves this score is “1m2m3m 7m8m9m 1p2p3p 3p4p5p SS\texttt{1m2m3m 7m8m9m 1p2p3p 3p4p5p SS}”.
The score for “Seven Pairs” is 125411328125411328, 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 44 copies. Dora tiles are given without repetition.

Test Point ID Scale of TT Discarded Tiles Number of Dora Tiles
11 T=10T = 10 No special restrictions 20\le 20 tiles
22 T=100T = 100 Includes at least all number tiles from 33 to 77
33 T=500T = 500 At least 22 copies of each type
44 At least 33 copies of each type
55 No special restrictions 00 tiles
66 T=1,000T = 1,000
77 20\le 20 tiles
88 T=1,500T = 1,500
99 T=2,000T = 2,000
1010 T=2,500T = 2,500

Translated by ChatGPT 5