#P5370. [PKUSC2018] 主斗地

[PKUSC2018] 主斗地

Description

If you have taken part in NOIP 2015 and PKUWC 2018, you must have a deep impression of a problem called Dou Dizhu. To pay tribute to the classic, we made another card-related problem. In this problem, the hand types are similar to but not exactly the same as Dou Dizhu. We call the poker game in this problem Zhu Dou Di.

Zhu Dou Di is a poker game played with a total of 54 cards: A to K of spades, hearts, clubs, and diamonds, plus the small joker and the big joker, one each. All other ranks have four copies. In Dou Dizhu, the order of ranks is as follows: 3<4<5<6<7<8<9<10(T)<J<Q<K<A<2<3<4<5<6<7<8<9<10(T)<J<Q<K<A<2< small joker (w)(w) << big joker (W)(W), and suits do not affect the card order. In each game, a hand consists of nn cards. Each time, a player may play cards according to the allowed hand types. The first player to play all their cards wins the game. To simplify the problem, we ignore suits, i.e., all cards of the same rank are considered identical.

In this problem, the allowed hand types are ( this part differs from the traditional Dou Dizhu, please be careful ):

Name Explanation Example Note
Single A single card 6 A single joker is also a single
Pair Two cards of the same rank 66 The two jokers do not form a pair
Triple Three cards of the same rank 666
Triple + Single A triple plus one single card 666w
Triple + Pair A triple plus one pair of a different rank 66699
Straight 55 or more consecutive single cards 3456789 Cannot contain jokers or 2
Consecutive Pairs 33 or more consecutive pairs 33445566
Consecutive Triples Two or more consecutive triples 333444555
Four + Two Four cards of the same rank plus two single cards 444456 444455 Note that you cannot bring two pairs
Plane (single wings) Consecutive triples plus the same number of single cards whose ranks are pairwise different 33344455569J
Plane (double wings) Consecutive triples plus the same number of pairs whose ranks are pairwise different 3334446699

Notes:

  1. There is no “consecutive bombs” hand type, but a hand like 444455556666 is still playable; it will be treated as a Plane (single wings) hand type, namely 444555666 with wings 456.
  2. The big joker and the small joker are different ranks, so it is legal for the plane’s wings to include both jokers, e.g., 333444wW.
  3. It is easy to verify that the above hand type rules are valid: for any legal hand, it has a unique hand type.
  4. There are no bombs. A bomb will be treated as Triple + Single, and it has no bomb effect, i.e., it cannot beat any hand type.
  5. There is no rocket. This means wW is no longer a legal hand type.

Two hands belong to the same hand type if and only if they have the same name and contain the same number of cards. Between hands of the same hand type, there is an order:

  1. For Triple + Single and Triple + Pair, the order depends on the rank of the triple.
  2. For planes, the order depends on the order of the consecutive triples.
  3. For Four + Two, the order depends on the rank of the four-of-a-kind.
  4. For other hand types, the order depends on the largest card in the hand.

Below is the game process of Zhu Dou Di:

  1. In Zhu Dou Di, there are two players, and they are on the same team. The two players must achieve the game goal together. For convenience, assume the first player is Jiutiao Kelian, and the second player is an “×× netizen”.
  2. The game uses only one complete deck of cards. At the start of the game, all 33’s are thrown away first. Then each side randomly draws 1717 cards from the deck, and the remaining 1616 cards are discarded. You may think of it as: after shuffling, Jiutiao Kelian takes the first 1717 cards, the “×× netizen” takes the next 1717 cards, and the remaining 1616 cards are thrown away.
  3. The game consists of several rounds, and each round has two steps:
    1. Step 1: Jiutiao Kelian chooses any legal hand CC of any hand type and any size from their current hand and plays it.
    2. Step 2: The “×× netizen” chooses a hand CC' from their current hand that has the same hand type as CC and is strictly larger in order, and plays it. If no such hand exists, the game fails.
  4. After a round ends, if at least one of Jiutiao Kelian and the “×× netizen” has no cards left, the game ends. If both players have no cards left, the game is a win; otherwise it is a failure.

Here is an example:

Suppose Jiutiao Kelian’s cards are 44445556789TJQKwW, and the “×× netizen” has 666789TJQKAAAA222. One winning strategy is:

  1. Round 1: Jiutiao Kelian plays 4444wW, and the “×× netizen” plays AAAA22.
  2. Round 2: Jiutiao Kelian plays 6789TJQ, and the “×× netizen” plays 789TJQK.
  3. Round 3: Jiutiao Kelian plays 555K, and the “×× netizen” plays 6662.

This game strongly tests the coordination between the two players. However, since Jiutiao Kelian and the “×× netizen” cannot understand each other, they decide to play with open hands, i.e., both sides know the other’s cards. Since both players will act according to the optimal strategy, the outcome of the game is already determined at the moment the cards are dealt.

Now you are given the “×× netizen”’s hand. You need to compute how many different possible hands Jiutiao Kelian can have that allow them to win.

Note: the “×× netizen”’s cards and Jiutiao Kelian’s cards come from the same deck, and there are no 33’s when dealing.

Input Format

For each test case, input one line: a string of length 1717 representing the “×× netizen”’s hand. We use 456789TJQKA2wW to represent each rank of card.

Output Format

For each test case, output an integer representing the answer: the number of Jiutiao Kelian’s hands that satisfy the condition. The answer may be very large, so output it modulo 998244353998244353.

Note: in this problem we ignore suits. If two hands have exactly the same multiset of ranks but different suits, they are still considered the same.

556789TJJQKKAA22w
456789TJJQKKAA22w
456789TJQKKKAAA22
193483
0
613897

Hint

For various reasons, this problem uses bundled testdata. There are 33 subtasks in total:

Subtask ID Score Constraint
1 3030 Each rank appears at most 22 times
2 Each rank appears at most 33 times
3 4040 Each rank appears at most 44 times

Time limit: 2s.

Memory limit: 512M.

Translated by ChatGPT 5