#P5645. [PKUWC2018] 斗地主

[PKUWC2018] 斗地主

Background

Dou Dizhu is a poker game played with a full deck of 5454 cards: ranks from A to K in four suits (spades, hearts, clubs, diamonds), plus two Jokers (one small Joker and one big Joker). Each Joker appears once, and every other rank appears four times. In Dou Dizhu, the rank order is defined as follows: 3<4<5<6<7<8<9<10<J<Q<K<A<2<3<4<5<6<7<8<9<10<J<Q<K<A<2< small Joker << big Joker. Suits do not affect the comparison of card ranks. In each game, a hand consists of nn cards. On each turn, a player may play cards according to the allowed patterns. The first player to play all cards in their hand wins. To simplify the problem, we ignore suits, meaning that all cards of the same rank are considered identical.

In this problem, the allowed patterns are ( this part is different from standard Dou Dizhu, please note ):

Name Explanation Example Note
Rocket Two Jokers ♂♀
Bomb Four cards of the same rank 6666
Single One 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 with Single A Triple plus one Single of a different rank 666♂ A Bomb is not a Triple with Single.
Triple with Pair A Triple plus one Pair of a different rank 66699
Straight A sequence of at least 55 consecutive Singles 3456789 Cannot contain Jokers or 2.
Consecutive Pairs A sequence of at least 33 consecutive Pairs 33445566
Consecutive Triples A sequence of at least 22 consecutive Triples 333444555
Four with Two Four cards of the same rank plus two Singles 444456 444455
Plane (single wings) Consecutive Triples plus the same number of Singles whose ranks are pairwise distinct 33344455569J
Plane (double wings) Consecutive Triples plus the same number of Pairs whose ranks are pairwise distinct 3334446699

Notes:

  1. There is no pattern called “consecutive bombs”, but a hand like 444455556666 can still be played. It will be treated as a Plane (single wings): 444555666 with wings 456.
  2. The big Joker and the small Joker are different ranks, so a plane can legally take both Jokers as wings, for example 333444♂♀.
  3. It can be verified that the above rules are well-defined: for any legal play, it has a unique pattern type.

Two plays are of the same pattern type if and only if they have the same name and contain the same number of cards. Plays of the same pattern type can be compared in strength (Rocket is unique and does not need comparison):

  1. For Triple with Single and Triple with Pair, the strength depends on the rank of the Triple.
  2. For Planes, the strength depends on the strength of the Consecutive Triples part.
  3. For Four with Two, the strength depends on the rank of the four-of-a-kind.
  4. For all other pattern types, the strength depends on the maximum rank among the cards in the play.

The gameplay process of Dou Dizhu is described as follows ( this part is exactly the same as standard Dou Dizhu ):

  1. In Dou Dizhu, the three players are split into two sides: one player is the Landlord, and the other two are Farmers. The Landlord has 2020 cards, and each Farmer has 1717 cards (together forming exactly one full deck).
  2. The game proceeds in multiple rounds. In each round, the winner of the previous round plays first (in the first round, the Landlord plays first). Then players take turns in order (you may imagine the three players sitting in a circle, playing clockwise).
  3. The first player of a round may play any legal pattern of any strength. Then, when it is a player’s turn, they may choose:
    1. Play nothing (pass).
    2. Play a strictly stronger play of the same pattern type as the most recent play in this round.
    3. If the most recent play is not a Bomb or Rocket, play any Bomb of any strength.
    4. Play a Rocket.
  4. In a round, if after a player plays, the other two players both choose to pass, the round ends. That player becomes the winner of this round and starts the next round.
  5. At any time, if a player has played all cards in their hand, the game ends and that player wins.
  6. If the Landlord wins and neither Farmer has played any card, then the Landlord is said to have achieved “Spring”.

Problem Description

Now three people are playing Dou Dizhu. If the Landlord achieves Spring, then the Landlord is considered the winner; otherwise, even if the Landlord plays all their cards first, it is still considered a win for the Farmers. Assume all three players act with optimal decisions.

You are given n(0n20)n(0 \leq n\leq 20) cards. Ask how many possible initial hands of the Landlord contain these nn cards, and no matter how the Farmers’ cards are distributed, the Landlord is guaranteed to achieve Spring.

Input Format

The first line contains an integer tt indicating the number of test cases.

For each test case, one line is given. The first integer is nn, the number of fixed cards, followed by nn space-separated integers describing each fixed card.

In particular, we use 11 to represent rank A, 1111 for rank J, 1212 for rank Q, 1313 for rank K, 1414 for the small Joker, and 1515 for the big Joker. The input is guaranteed to be valid, meaning that the count of each rank will not exceed its count in a full deck.

Output Format

For each test case, output one integer, the number of Landlord hands that satisfy the condition. The answer may be very large; 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 considered the same.

6
20 1 2 2 3 4 5 6 7 8 8 9 10 11 11 12 13 13 13 13 15
20 1 1 2 2 3 4 5 6 7 8 9 10 11 12 12 12 12 13 13 14
20 1 2 2 3 3 4 5 6 7 7 7 7 8 9 10 10 11 12 13 15
20 1 2 3 4 4 5 6 7 8 9 10 11 11 12 13 13 13 13 14 15
3 3 3 3
4 3 3 3 3
1
0
1
1
4790
1670

Hint

Sample Explanation

For the first sample, we can see that the Farmers cannot have a Bomb or Rocket, so the Landlord can first play [3,4,5,6,7,8,9,10,J,Q][3,4,5,6,7,8,9,10,J,Q] (clearly neither Farmer can beat it), then play [2,2][2,2], then play the big Joker, then play [K,K,K,K,A,J][K,K,K,K,A,J], and finally play [8][8].

ID nn tt
1 =20=20 =100= 100
2 =18=18
3 =16=16
4 =14=14
5 =12=12
6 =0=0 =1= 1
7
8 [0,20]\in [0,20] =500= 500
9 =1000= 1000
10 =2000= 2000

Translated by ChatGPT 5