Jaewoo's Card Gamble
Time limit1sMemory limit1024 MB
Given N pairs of shuffled face-down cards, find the expected number of picks needed to collect all 2N cards under optimal play, modulo 1e9+7.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Probability
- Solved
- No attempts yet
Problem
Jaewoo is addicted to a soccer game. In this game, you collect player cards, build a team from them, and play matches against other players. Player cards can be upgraded to give a player higher stats. An upgrade needs one copy of the same player card as material, and the material card disappears.
Jaewoo decided to open card packs today in search of strong players. He found out that a new event card pack holds 2N cards, not the usual N. Puzzled, he read the event notice, which says the following.
“The new event card pack contains 2 cards each of N different kinds, for 2N cards in total. However, you do not receive all 2N cards at once when you buy the pack. The 2N cards are shuffled, indistinguishable, and placed face down. For 1 EP (event point), you can pick 2 of the face-down cards to check. You do not check two cards at the same time. Instead, you pick one card to check, and then pick another. If the two cards you picked match, we immediately give you both cards. If they differ, both cards are turned face down again and return to their original positions.”
Jaewoo learned that EP is a currency you have to pay for, and he was angry at the game company's tactics. He bought only one pack, and he wants to know the expected EP needed to take every card in the pack. Help him compute this value. Jaewoo has a sharp memory, so he remembers the position and type of every card he has seen. He always tries to take all the cards in the optimal way.
Input
The first line contains an integer (), the number of card pairs.
Output
Print the expected EP needed to take all cards in the pack of 2N cards, modulo . It is given that is a prime.
The fraction (with , , ) modulo is defined as , where is the integer satisfying , that is, the modular multiplicative inverse of modulo . If the value is an integer, then , and the result is . The method for finding the inverse is given in the hint below.
For every input, the answer is uniquely determined. That is, the answer is an integer, or its reduced fraction has a denominator coprime to .
Hint
The modular multiplicative inverse of modulo can be found with Fermat's little theorem or with the extended Euclidean algorithm. Using Fermat's little theorem: for a natural number coprime to , , so . Since the inverse of a number coprime to is unique, . Using exponentiation by squaring, can be computed in .