SRM 708, D2, 250-Pointer (SafeBetting)

James S. Plank

Mon Feb 20 15:23:40 EST 2017
Last revision: Thu Oct 8 03:57:22 EDT 2020

In case Topcoder's servers are down

Here is a summary of the problem:

Examples

#    a    b    c    answer
0   15   20   48    3 - Bet 5 and win to get 25.  Bet 10 and win to get 35.  Bet 13 and win to get 4.  In no case will you have < 15 dollars if you lose.
1   10  400  560    1
2    5    7   21    3
3    5    7   22    4
4   17   30 1000    7

Testing Yourself

If you run tests.sh, your output should be identical to answers.txt.

Hints

Try this on your own. If you get stuck, return here and read what I've written below.



























































The wrong way to solve this is to use math and calculate B. The right way is simply to simulate -- while b is less than c, calculate the maximum bet that you can make, and pretend that you win by adding it to b. Count the number of times that you do this. No math needed except addition and subtraction!

Where the math comes into play is calculating the running time. Note that if you bet x in round 0, then, if you win, you'll bet 2x in round 1. And if you win again, you'll bet 4x in round 2. And 8x in round 3. Do you see how the number of rounds is going to be capped at log2(c)? Who needs math?