Becoming a Millionaire

Bet any fraction of your money each round to maximize the chance of ending with at least one million dollars.

Hard8Dynamic programmingProbabilityMathNo attempts yetTime limit5sMemory limit512 MB

Problem

You are invited to the TV show "Would you like to be a millionaire?". The rules are these.

  • Before the game starts, the host spins a wheel of fortune to fix PP, the probability of winning a single bet.
  • You start with XX dollars.
  • There are MM rounds of betting. In each round you may bet any part of the money you hold, including none of it and all of it. The amount you bet does not have to be a whole number of dollars or cents.
  • If you win the round, your money grows by the amount you bet. If you lose the round, your money shrinks by the amount you bet.
  • After all MM rounds, you keep your money if you hold $1000000 or more, rounded down to whole dollars. If you hold less than $1000000, you get nothing.

You are given MM, PP and XX. Play so that the chance of becoming a millionaire is as large as possible, and compute the probability of ending with $1000000 or more.

Input

The first line contains the number of test cases NN.

Each of the next NN lines contains MM, PP and XX, separated by single spaces.

  • MM is an integer, the number of betting rounds.
  • PP is a real number, the probability of winning a single round.
  • XX is an integer, the starting amount in dollars.

Limits:

  • 1N1001 \le N \le 100
  • 0P10 \le P \le 1, with at most 6 digits after the decimal point. PP may also be written as an integer with no decimal point.
  • 1X10000001 \le X \le 1000000
  • 1M51 \le M \le 5

Output

For each test case, print one line Case #x: y, where xx is the test case number starting at 1 and yy is the probability of becoming a millionaire.

Print yy rounded to six digits after the decimal point, padded with zeros so that there are always exactly six digits. Round up when the seventh digit is 5. A probability of exactly 1 prints as 1.000000 and a probability of exactly 0 prints as 0.000000.

Hint

With M=1M = 1, P=0.5P = 0.5 and X=500000X = 500000, the only way to reach $1000000 is to bet everything in the single round, so the probability is 0.50.5.

With M=3M = 3, P=0.75P = 0.75 and X=600000X = 600000 you can play so that you still reach $1000000 after losing one round. Here is one such plan.

  • You hold $600000 in the first round. Bet $150000.
  • After losing the first round you hold $450000. Bet $100000.
  • After losing the first round and winning the second you hold $550000. Bet $450000.
  • After winning the first round you hold $750000. Bet $250000.
  • After winning the first round and losing the second you hold $500000. Bet $500000.