Success Probability of the Card-Pile Game
Time limit1sMemory limit128 MB
Given n decks of k cards each shuffled into n piles, find the probability that the follow-the-number drawing game succeeds within m restarts, printed to r decimals.
- Level
Hard9 of 10
- Topics
- Probability, Combinatorics, Math, Implementation
- Solved
- No attempts yet
Problem
Consider the following game. There are decks, each consisting of cards numbered through (one card per number). Shuffle all cards well, then deal them into piles of cards each, laid out in a single horizontal row. The -th pile from the left (holding cards) is called "pile ".

The game starts at pile . Draw the top card of the current pile (a drawn card is never returned), and if the number on it is , next draw the top card of pile . Repeat: each time, move to the pile whose number equals the value just drawn and take its top card. If every pile becomes empty, the game succeeds. If some pile still has cards but the pile you must draw from next is empty, the game fails.

If the game fails partway through, you may either stop with a failure, or leave the remaining piles as they are (keeping their pile numbers) and restart. On a restart, the first card is drawn from the leftmost pile that still has cards (its top card is drawn first). After restarting you proceed exactly as before: success if all piles become empty, failure if some pile still has cards but the next pile to draw from is empty.
At most such restarts are allowed, where is or . That is, you either never restart, or restart exactly once. The initial layout of the cards depends on the shuffle. Depending on the layout, the game may succeed without restarting, succeed after restarting, or fail even after restarting. Assuming the deck is shuffled well so that every initial layout is equally likely, we want the probability that the game succeeds within restarts. Write a program that expresses as a decimal and prints it to decimal places, subject to the following rules.
- If is an integer for some sufficiently large positive integer , the fractional part eventually becomes all zeros; print those zeros as well. For example, if , then for print 0.37500, and for print 0.37. Likewise, if , then for print 1.000.
- For example, 0.150000… can also be written as the repeating decimal 0.1499999…, but in such cases use the former form (0.150000…).
Input
The first line contains integers , , , in this order, separated by spaces, where , , or , and .
Output
Print formatted by the rules above on one line, followed by a newline.