Problem Statement
You are on a TV game show where two players try to maximize their earnings. Initially, your opponent has X0 dollars and you have Y0 dollars. Before the game starts, your opponent chooses a number A and you choose a number B (A > 0, B > 0).
The game consists of n rounds. Each round of the game proceeds as follows. Let X be the amount of money your opponent has at the beginning of the round, and let Y be the amount of money you have. Your opponent will choose between two options. He can either turn his money into X+A dollars and turn your money into Y*B dollars, or he can turn his money into X*B dollars and turn your money into Y+A dollars. All operations are done with real numbers. The amount of money each player has at the end of n rounds is that player's final earnings.
Your opponent will play in such a way that maximizes his final earnings. If, during a round, both of his options would lead to the same final earnings for himself, he will choose the first option (turn his money into X+A dollars and turn your money into Y*B dollars).
Your only choice in the game is the value of B. Return the maximum possible final earnings you can achieve.
Definition
- Class:
- TVShow
- Method:
- maximalWin
- Parameters:
- int, double, double, double
- Returns:
- double
- Method signature:
- double maximalWin(int n, double X0, double Y0, double A)
- (be sure your method is public)
Notes
- The returned value must have an absolute or relative error less than 1e-9.
Constraints
- n will be between 1 and 100, inclusive.
- X0 will be between 1 and 1000, inclusive.
- Y0 will be between 1 and 1000, inclusive.
- A will be between 1 and 1000, inclusive.
Examples
1
100
100
30
Returns: 130.0
In this case, whatever B you select, your opponent will have the advantage. If you choose B > 1.3, you will get Y+A = 130, and if you choose B <= 1.3, you will get Y*B <= 130. Your maximum possible earnings is therefore 130.0.1
100
200
30
Returns: 260.0
In this case, if you choose B = 1.3, your opponent will choose to turn his money into X+A, so you will get Y*B = 260.0.3
100
140
35
Returns: 259.0
The optimal value for B here is 1.35.10
100
200
30
Returns: 544.912109375