Under construction

Still being built. Things may change

Success Probability of the Card-Pile Game

Time limit1sMemory limit128 MB

Summary
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 kk decks, each consisting of nn cards numbered 11 through nn (one card per number). Shuffle all knkn cards well, then deal them into nn piles of kk cards each, laid out in a single horizontal row. The ii-th pile from the left (holding kk cards) is called "pile ii".

Card pile layout

The game starts at pile 11. Draw the top card of the current pile (a drawn card is never returned), and if the number on it is ii, next draw the top card of pile ii. 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.

Game progression

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 mm such restarts are allowed, where mm is 00 or 11. 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 pp that the game succeeds within mm restarts. Write a program that expresses pp as a decimal and prints it to rr decimal places, subject to the following rules.

  • If p×10Kp \times 10^K is an integer for some sufficiently large positive integer KK, the fractional part eventually becomes all zeros; print those zeros as well. For example, if p=3/8=0.375p = 3/8 = 0.375, then for r=5r = 5 print 0.37500, and for r=2r = 2 print 0.37. Likewise, if p=1.0p = 1.0, then for r=3r = 3 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 nn, kk, mm, rr in this order, separated by spaces, where 1≤n≤100001 \le n \le 10000, 1≤k≤1001 \le k \le 100, m=0m = 0 or m=1m = 1, and 1≤r≤100001 \le r \le 10000.

Output

Print pp formatted by the rules above on one line, followed by a newline.

Examples3

  1. Example 1

    Input
    2 1 0 5
    
    Expected output
    0.50000
    
  2. Example 2

    Input
    3 1 1 3
    
    Expected output
    0.833
    
  3. Example 3

    Input
    2 2 1 3
    
    Expected output
    1.000