#P10457. 占卜DIY

占卜DIY

Problem Description

lyd learned how to use playing cards to do DIY fortune telling. The method is as follows: there is a deck of 5252 cards with both Jokers removed. After shuffling, split them evenly into 1313 piles, numbered 1131 \sim 13. Each pile has 44 cards. The 1313-th pile is called the “life pile”, which means you have 44 lives. Among all cards, the four KK cards are called the Grim Reaper.

In the initial state, all cards are placed face down.

The process is:

  • Draw the top card (the first card) from the life pile.
  • Flip this card face up, and place it on top of the pile whose number matches the card value. (For example, if you draw 22, place it face up on top of pile 22. If you draw JJ, place it on top of pile 1111. Note that it must be placed face up.)
  • From the pile where you just placed the card, draw the bottom card (the last card), and repeat step 22. (For example, last time you drew 22 and placed it on top of pile 22. Now you draw the last card of pile 22 and find it is 88, then you place it on top of pile 88, and so on.)
  • If you draw a KK during the drawing process, it means you lose one life. Discard this KK, then restart from step 11.
  • After all four lives are lost, count how many face-up cards are on top of each pile. If the same value has 44 cards that are face up (for example, four AA cards), then it is called “opening a pair”. Of course, four KK cards do not count.
  • Count how many pairs are opened in total. Opening 00 pairs is called “extremely unlucky”, 121 \sim 2 pairs is “very unlucky”, 33 pairs is “unlucky”, 454 \sim 5 pairs is “slightly unlucky”, 66 pairs is “neutral”, 787 \sim 8 pairs is “slightly lucky”, 99 pairs is “lucky”, 101110 \sim 11 pairs is “very lucky”, and 1212 pairs is “all flowers bloom, extremely lucky”.

If you still do not understand, please refer to the sample.

Input Format

There are 1313 lines in total, each describing what cards are in that pile (suits are ignored; only values are given). For each pile, the input order is from top to bottom.

For easier input, use 00 to represent 1010.

Output Format

Output the total number of opened pairs.

8 5 A A
K 5 3 2
9 6 0 6
3 4 3 4
3 4 4 5
5 6 7 6
8 7 7 7
9 9 8 8
9 0 0 0
K J J J
Q A Q K
J Q 2 2
A K Q 2
9

Hint

Note: After the first life is lost, the first 1212 piles become:

A A A 8
K 5 3 2
3 9 6 0
4 4 3 4
5 5 3 4
6 6 5 6
7 7 7 7 
8 8 8 9
9 9 0 0
0 K J J
J Q A Q//The K drawn here causes a death.
J Q 2 2

The second life is lost immediately because there is a KK in the life pile, so nothing changes.

After the third life is lost, the first 1212 piles become:

A A A A
2 2 2 K
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 7 
8 8 8 8
9 9 9 9
0 0 0 0//The K drawn here causes a death.
J J J Q
Q Q J Q

After the fourth life is lost, the first 1212 piles become:

A A A A
2 2 2 2//The K drawn here causes a death.
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 7 
8 8 8 8
9 9 9 9
0 0 0 0
J J J Q
Q Q J Q

Finally, among all face-up cards, the 99 pairs A 2 3 4 5 6 7 8 10A\ 2\ 3\ 4\ 5\ 6\ 7\ 8\ 10 are opened. (Note: although the last card of pile 99 is also 99, it was not flipped face up, so it cannot be counted.) Therefore, output 99.

Translated by ChatGPT 5