#P5301. Mahjong Winning Hand Maximum Score
Mahjong Winning Hand Maximum Score
Mahjong Winning Hand Maximum Score
In Mahjong a winning hand is one that meets certain composition rules. A Mahjong set has 136 tiles consisting of 34 different types (four copies of each). The 34 tile types are:
- Characters (wan): 1wan, 2wan, …, 9wan
- Bamboos (suo): 1suo, 2suo, …, 9suo
- Dots (tong): 1tong, 2tong, …, 9tong
- Honors: east, south, west, north, zhong, bai, fa
There are several winning hand types. A hand is a collection of tiles chosen from the pool (the set of not‐yet played tiles) and its completion score is defined as:
Score = (\(\prod_{i=0}^{33} C(r_i, m_i)\)) \(\times 2^{m_{bonus}}\) \(\times multiplier\)
where:
- \(r_i\) is the remaining count of tile type i (in the pool);
- \(m_i\) is the number of copies of tile type i in the hand; note that for a valid hand we have \(m_i\u22644\) and additionally for a hand to exist we require \(r_i \ge m_i\) for every \(i\).
- \(C(n,k)\) is the combination number.
- \(m_{bonus}\) is the count in the hand of the designated bonus tile.
- The multiplier is an extra factor: for a hand of seven pairs, multiply by \(7\); for a "Thirteen Orphans" hand (guo shi wu shuang) multiply by \(13\); otherwise the multiplier is \(1\).
The legal winning hands are defined as follows. Let the hand size and its possible partition be:
- 14 tiles:
- Standard hand: Partition into 4 groups called mentsu and 1 pair. A mentsu is either a triplet (3 identical tiles) or a sequence (3 numerically consecutive tiles in the same suit: wan, suo, or tong).
- Seven pairs: Consists of 7 different pairs.
- Thirteen orphans: Composed only of the 13 terminal/honor types (namely: 1wan, 9wan, 1suo, 9suo, 1tong, 9tong, east, south, west, north, zhong, bai, fa) with each appearing at least once, plus one extra copy of one of these 13.
- 15 tiles: Partition into 1 gang (4 identical tiles), 3 mentsu and 1 pair.
- 16 tiles: Partition into 2 gangs, 2 mentsu and 1 pair.
- 17 tiles: Partition into 3 gangs, 1 mentsu and 1 pair.
- 18 tiles: Partition into 4 gangs and 1 pair.
In every hand the bonus tile (dora) is specified. Every occurrence of the bonus tile in the hand multiplies the score by 2 (i.e. \(2^{m_{bonus}}\)).
Your task is: Given the counts of each of the 34 tile types still remaining (in the order listed above) and the bonus tile, determine the maximum completion score among all winning hands that could be formed from these remaining tiles.
Notes:
- This is a combinatorial search problem. For each candidate winning hand (which obeys one of the rules above), its score is computed as the product over all tiles \(i\) of \(C(r_i, m_i)\) multiplied by \(2^{\text{(number of bonus tiles in the hand)}}\) and then multiplied by the extra multiplier if the hand is seven pairs or thirteen orphans.
- You may assume that the remaining counts \(r_i\) are small (each between 0 and 4) and that a valid hand has at most 18 tiles.
inputFormat
The input consists of two lines:
- The first line contains 34 space‐separated nonnegative integers. The i-th number represents \(r_i\), the number of remaining copies for the i-th tile type. The ordering of tile types is as follows: 1wan, 2wan, …, 9wan, 1suo, 2suo, …, 9suo, 1tong, 2tong, …, 9tong, east, south, west, north, zhong, bai, fa.
- The second line contains a string representing the bonus tile. The bonus tile is one of: 1wan, 2wan, …, 9wan, 1suo, …, 9suo, 1tong, …, 9tong, east, south, west, north, zhong, bai, fa.
outputFormat
Output a single integer — the maximum completion score of any winning hand that can be formed from the remaining tiles.
sample
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
9wan
262144
</p>